DllReferencePlugin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
  7. const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
  8. const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
  9. class DllReferencePlugin {
  10. constructor(options) {
  11. this.options = options;
  12. }
  13. apply(compiler) {
  14. compiler.plugin("compilation", (compilation, params) => {
  15. const normalModuleFactory = params.normalModuleFactory;
  16. compilation.dependencyFactories.set(DelegatedSourceDependency, normalModuleFactory);
  17. });
  18. compiler.plugin("before-compile", (params, callback) => {
  19. const manifest = this.options.manifest;
  20. if(typeof manifest === "string") {
  21. params.compilationDependencies.push(manifest);
  22. compiler.inputFileSystem.readFile(manifest, function(err, result) {
  23. if(err) return callback(err);
  24. params["dll reference " + manifest] = JSON.parse(result.toString("utf-8"));
  25. return callback();
  26. });
  27. } else {
  28. return callback();
  29. }
  30. });
  31. compiler.plugin("compile", (params) => {
  32. let manifest = this.options.manifest;
  33. if(typeof manifest === "string") {
  34. manifest = params["dll reference " + manifest];
  35. }
  36. const name = this.options.name || manifest.name;
  37. const sourceType = this.options.sourceType || "var";
  38. const externals = {};
  39. const source = "dll-reference " + name;
  40. externals[source] = name;
  41. params.normalModuleFactory.apply(new ExternalModuleFactoryPlugin(sourceType, externals));
  42. params.normalModuleFactory.apply(new DelegatedModuleFactoryPlugin({
  43. source: source,
  44. type: this.options.type,
  45. scope: this.options.scope,
  46. context: this.options.context || compiler.options.context,
  47. content: this.options.content || manifest.content,
  48. extensions: this.options.extensions
  49. }));
  50. });
  51. }
  52. }
  53. module.exports = DllReferencePlugin;