WebpackOptionsApply.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const OptionsApply = require("./OptionsApply");
  7. const LoaderTargetPlugin = require("./LoaderTargetPlugin");
  8. const FunctionModulePlugin = require("./FunctionModulePlugin");
  9. const EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
  10. const SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
  11. const EvalSourceMapDevToolPlugin = require("./EvalSourceMapDevToolPlugin");
  12. const EntryOptionPlugin = require("./EntryOptionPlugin");
  13. const RecordIdsPlugin = require("./RecordIdsPlugin");
  14. const APIPlugin = require("./APIPlugin");
  15. const ConstPlugin = require("./ConstPlugin");
  16. const RequireJsStuffPlugin = require("./RequireJsStuffPlugin");
  17. const NodeStuffPlugin = require("./NodeStuffPlugin");
  18. const CompatibilityPlugin = require("./CompatibilityPlugin");
  19. const TemplatedPathPlugin = require("./TemplatedPathPlugin");
  20. const WarnCaseSensitiveModulesPlugin = require("./WarnCaseSensitiveModulesPlugin");
  21. const UseStrictPlugin = require("./UseStrictPlugin");
  22. const LoaderPlugin = require("./dependencies/LoaderPlugin");
  23. const CommonJsPlugin = require("./dependencies/CommonJsPlugin");
  24. const HarmonyModulesPlugin = require("./dependencies/HarmonyModulesPlugin");
  25. const SystemPlugin = require("./dependencies/SystemPlugin");
  26. const ImportPlugin = require("./dependencies/ImportPlugin");
  27. const AMDPlugin = require("./dependencies/AMDPlugin");
  28. const RequireContextPlugin = require("./dependencies/RequireContextPlugin");
  29. const RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
  30. const RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
  31. const EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
  32. const RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
  33. const RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
  34. const MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
  35. const FlagIncludedChunksPlugin = require("./optimize/FlagIncludedChunksPlugin");
  36. const OccurrenceOrderPlugin = require("./optimize/OccurrenceOrderPlugin");
  37. const FlagDependencyUsagePlugin = require("./FlagDependencyUsagePlugin");
  38. const FlagDependencyExportsPlugin = require("./FlagDependencyExportsPlugin");
  39. const SizeLimitsPlugin = require("./performance/SizeLimitsPlugin");
  40. const ResolverFactory = require("enhanced-resolve").ResolverFactory;
  41. class WebpackOptionsApply extends OptionsApply {
  42. constructor() {
  43. super();
  44. }
  45. process(options, compiler) {
  46. let ExternalsPlugin;
  47. compiler.outputPath = options.output.path;
  48. compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
  49. compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
  50. compiler.name = options.name;
  51. compiler.dependencies = options.dependencies;
  52. if(typeof options.target === "string") {
  53. let JsonpTemplatePlugin;
  54. let NodeSourcePlugin;
  55. let NodeTargetPlugin;
  56. let NodeTemplatePlugin;
  57. switch(options.target) {
  58. case "web":
  59. JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
  60. NodeSourcePlugin = require("./node/NodeSourcePlugin");
  61. compiler.apply(
  62. new JsonpTemplatePlugin(options.output),
  63. new FunctionModulePlugin(options.output),
  64. new NodeSourcePlugin(options.node),
  65. new LoaderTargetPlugin(options.target)
  66. );
  67. break;
  68. case "webworker":
  69. {
  70. let WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
  71. NodeSourcePlugin = require("./node/NodeSourcePlugin");
  72. compiler.apply(
  73. new WebWorkerTemplatePlugin(),
  74. new FunctionModulePlugin(options.output),
  75. new NodeSourcePlugin(options.node),
  76. new LoaderTargetPlugin(options.target)
  77. );
  78. break;
  79. }
  80. case "node":
  81. case "async-node":
  82. NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
  83. NodeTargetPlugin = require("./node/NodeTargetPlugin");
  84. compiler.apply(
  85. new NodeTemplatePlugin({
  86. asyncChunkLoading: options.target === "async-node"
  87. }),
  88. new FunctionModulePlugin(options.output),
  89. new NodeTargetPlugin(),
  90. new LoaderTargetPlugin("node")
  91. );
  92. break;
  93. case "node-webkit":
  94. JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
  95. NodeTargetPlugin = require("./node/NodeTargetPlugin");
  96. ExternalsPlugin = require("./ExternalsPlugin");
  97. compiler.apply(
  98. new JsonpTemplatePlugin(options.output),
  99. new FunctionModulePlugin(options.output),
  100. new NodeTargetPlugin(),
  101. new ExternalsPlugin("commonjs", "nw.gui"),
  102. new LoaderTargetPlugin(options.target)
  103. );
  104. break;
  105. case "atom":
  106. case "electron":
  107. case "electron-main":
  108. NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
  109. NodeTargetPlugin = require("./node/NodeTargetPlugin");
  110. ExternalsPlugin = require("./ExternalsPlugin");
  111. compiler.apply(
  112. new NodeTemplatePlugin({
  113. asyncChunkLoading: true
  114. }),
  115. new FunctionModulePlugin(options.output),
  116. new NodeTargetPlugin(),
  117. new ExternalsPlugin("commonjs", [
  118. "app",
  119. "auto-updater",
  120. "browser-window",
  121. "content-tracing",
  122. "dialog",
  123. "electron",
  124. "global-shortcut",
  125. "ipc",
  126. "ipc-main",
  127. "menu",
  128. "menu-item",
  129. "power-monitor",
  130. "power-save-blocker",
  131. "protocol",
  132. "session",
  133. "web-contents",
  134. "tray",
  135. "clipboard",
  136. "crash-reporter",
  137. "native-image",
  138. "screen",
  139. "shell"
  140. ]),
  141. new LoaderTargetPlugin(options.target)
  142. );
  143. break;
  144. case "electron-renderer":
  145. JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
  146. NodeTargetPlugin = require("./node/NodeTargetPlugin");
  147. ExternalsPlugin = require("./ExternalsPlugin");
  148. compiler.apply(
  149. new JsonpTemplatePlugin(options.output),
  150. new FunctionModulePlugin(options.output),
  151. new NodeTargetPlugin(),
  152. new ExternalsPlugin("commonjs", [
  153. "desktop-capturer",
  154. "electron",
  155. "ipc",
  156. "ipc-renderer",
  157. "remote",
  158. "web-frame",
  159. "clipboard",
  160. "crash-reporter",
  161. "native-image",
  162. "screen",
  163. "shell"
  164. ]),
  165. new LoaderTargetPlugin(options.target)
  166. );
  167. break;
  168. default:
  169. throw new Error("Unsupported target '" + options.target + "'.");
  170. }
  171. } else if(options.target !== false) {
  172. options.target(compiler);
  173. } else {
  174. throw new Error("Unsupported target '" + options.target + "'.");
  175. }
  176. if(options.output.library || options.output.libraryTarget !== "var") {
  177. let LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
  178. compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || "", options.output.libraryExport));
  179. }
  180. if(options.externals) {
  181. ExternalsPlugin = require("./ExternalsPlugin");
  182. compiler.apply(new ExternalsPlugin(options.output.libraryTarget, options.externals));
  183. }
  184. let noSources;
  185. let legacy;
  186. let modern;
  187. let comment;
  188. if(options.devtool && (options.devtool.indexOf("sourcemap") >= 0 || options.devtool.indexOf("source-map") >= 0)) {
  189. const hidden = options.devtool.indexOf("hidden") >= 0;
  190. const inline = options.devtool.indexOf("inline") >= 0;
  191. const evalWrapped = options.devtool.indexOf("eval") >= 0;
  192. const cheap = options.devtool.indexOf("cheap") >= 0;
  193. const moduleMaps = options.devtool.indexOf("module") >= 0;
  194. noSources = options.devtool.indexOf("nosources") >= 0;
  195. legacy = options.devtool.indexOf("@") >= 0;
  196. modern = options.devtool.indexOf("#") >= 0;
  197. comment = legacy && modern ? "\n/*\n//@ source" + "MappingURL=[url]\n//# source" + "MappingURL=[url]\n*/" :
  198. legacy ? "\n/*\n//@ source" + "MappingURL=[url]\n*/" :
  199. modern ? "\n//# source" + "MappingURL=[url]" :
  200. null;
  201. let Plugin = evalWrapped ? EvalSourceMapDevToolPlugin : SourceMapDevToolPlugin;
  202. compiler.apply(new Plugin({
  203. filename: inline ? null : options.output.sourceMapFilename,
  204. moduleFilenameTemplate: options.output.devtoolModuleFilenameTemplate,
  205. fallbackModuleFilenameTemplate: options.output.devtoolFallbackModuleFilenameTemplate,
  206. append: hidden ? false : comment,
  207. module: moduleMaps ? true : cheap ? false : true,
  208. columns: cheap ? false : true,
  209. lineToLine: options.output.devtoolLineToLine,
  210. noSources: noSources,
  211. }));
  212. } else if(options.devtool && options.devtool.indexOf("eval") >= 0) {
  213. legacy = options.devtool.indexOf("@") >= 0;
  214. modern = options.devtool.indexOf("#") >= 0;
  215. comment = legacy && modern ? "\n//@ sourceURL=[url]\n//# sourceURL=[url]" :
  216. legacy ? "\n//@ sourceURL=[url]" :
  217. modern ? "\n//# sourceURL=[url]" :
  218. null;
  219. compiler.apply(new EvalDevToolModulePlugin(comment, options.output.devtoolModuleFilenameTemplate));
  220. }
  221. compiler.apply(new EntryOptionPlugin());
  222. compiler.applyPluginsBailResult("entry-option", options.context, options.entry);
  223. compiler.apply(
  224. new CompatibilityPlugin(),
  225. new HarmonyModulesPlugin(options.module),
  226. new AMDPlugin(options.module, options.amd || {}),
  227. new CommonJsPlugin(options.module),
  228. new LoaderPlugin(),
  229. new NodeStuffPlugin(options.node),
  230. new RequireJsStuffPlugin(),
  231. new APIPlugin(),
  232. new ConstPlugin(),
  233. new UseStrictPlugin(),
  234. new RequireIncludePlugin(),
  235. new RequireEnsurePlugin(),
  236. new RequireContextPlugin(options.resolve.modules, options.resolve.extensions, options.resolve.mainFiles),
  237. new ImportPlugin(options.module),
  238. new SystemPlugin(options.module)
  239. );
  240. compiler.apply(
  241. new EnsureChunkConditionsPlugin(),
  242. new RemoveParentModulesPlugin(),
  243. new RemoveEmptyChunksPlugin(),
  244. new MergeDuplicateChunksPlugin(),
  245. new FlagIncludedChunksPlugin(),
  246. new OccurrenceOrderPlugin(true),
  247. new FlagDependencyExportsPlugin(),
  248. new FlagDependencyUsagePlugin()
  249. );
  250. if(options.performance) {
  251. compiler.apply(new SizeLimitsPlugin(options.performance));
  252. }
  253. compiler.apply(new TemplatedPathPlugin());
  254. compiler.apply(new RecordIdsPlugin());
  255. compiler.apply(new WarnCaseSensitiveModulesPlugin());
  256. if(options.cache) {
  257. let CachePlugin = require("./CachePlugin");
  258. compiler.apply(new CachePlugin(typeof options.cache === "object" ? options.cache : null));
  259. }
  260. compiler.applyPlugins("after-plugins", compiler);
  261. if(!compiler.inputFileSystem) throw new Error("No input filesystem provided");
  262. compiler.resolvers.normal = ResolverFactory.createResolver(Object.assign({
  263. fileSystem: compiler.inputFileSystem
  264. }, options.resolve));
  265. compiler.resolvers.context = ResolverFactory.createResolver(Object.assign({
  266. fileSystem: compiler.inputFileSystem,
  267. resolveToContext: true
  268. }, options.resolve));
  269. compiler.resolvers.loader = ResolverFactory.createResolver(Object.assign({
  270. fileSystem: compiler.inputFileSystem
  271. }, options.resolveLoader));
  272. compiler.applyPlugins("after-resolvers", compiler);
  273. return options;
  274. }
  275. }
  276. module.exports = WebpackOptionsApply;