1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const util = require("util");
|
9 | const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
|
10 | const Compiler = require("./Compiler");
|
11 | const MultiCompiler = require("./MultiCompiler");
|
12 | const WebpackOptionsApply = require("./WebpackOptionsApply");
|
13 | const {
|
14 | applyWebpackOptionsDefaults,
|
15 | applyWebpackOptionsBaseDefaults
|
16 | } = require("./config/defaults");
|
17 | const { getNormalizedWebpackOptions } = require("./config/normalization");
|
18 | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
19 | const validateSchema = require("./validateSchema");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const createMultiCompiler = childOptions => {
|
41 | const compilers = childOptions.map(options => createCompiler(options));
|
42 | const compiler = new MultiCompiler(compilers);
|
43 | for (const childCompiler of compilers) {
|
44 | if (childCompiler.options.dependencies) {
|
45 | compiler.setDependencies(
|
46 | childCompiler,
|
47 | childCompiler.options.dependencies
|
48 | );
|
49 | }
|
50 | }
|
51 | return compiler;
|
52 | };
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | const createCompiler = rawOptions => {
|
59 | const options = getNormalizedWebpackOptions(rawOptions);
|
60 | applyWebpackOptionsBaseDefaults(options);
|
61 | const compiler = new Compiler(options.context);
|
62 | compiler.options = options;
|
63 | new NodeEnvironmentPlugin({
|
64 | infrastructureLogging: options.infrastructureLogging
|
65 | }).apply(compiler);
|
66 | if (Array.isArray(options.plugins)) {
|
67 | for (const plugin of options.plugins) {
|
68 | if (typeof plugin === "function") {
|
69 | plugin.call(compiler, compiler);
|
70 | } else {
|
71 | plugin.apply(compiler);
|
72 | }
|
73 | }
|
74 | }
|
75 | applyWebpackOptionsDefaults(options);
|
76 | compiler.hooks.environment.call();
|
77 | compiler.hooks.afterEnvironment.call();
|
78 | new WebpackOptionsApply().process(options, compiler);
|
79 | compiler.hooks.initialize.call();
|
80 | return compiler;
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | const webpack = ((
|
98 | options,
|
99 | callback
|
100 | ) => {
|
101 | const create = () => {
|
102 | validateSchema(webpackOptionsSchema, options);
|
103 |
|
104 | let compiler;
|
105 | let watch = false;
|
106 |
|
107 | let watchOptions;
|
108 | if (Array.isArray(options)) {
|
109 |
|
110 | compiler = createMultiCompiler(options);
|
111 | watch = options.some(options => options.watch);
|
112 | watchOptions = options.map(options => options.watchOptions || {});
|
113 | } else {
|
114 |
|
115 | compiler = createCompiler(options);
|
116 | watch = options.watch;
|
117 | watchOptions = options.watchOptions || {};
|
118 | }
|
119 | return { compiler, watch, watchOptions };
|
120 | };
|
121 | if (callback) {
|
122 | try {
|
123 | const { compiler, watch, watchOptions } = create();
|
124 | if (watch) {
|
125 | compiler.watch(watchOptions, callback);
|
126 | } else {
|
127 | compiler.run((err, stats) => {
|
128 | compiler.close(err2 => {
|
129 | callback(err || err2, stats);
|
130 | });
|
131 | });
|
132 | }
|
133 | return compiler;
|
134 | } catch (err) {
|
135 | process.nextTick(() => callback(err));
|
136 | return null;
|
137 | }
|
138 | } else {
|
139 | const { compiler, watch } = create();
|
140 | if (watch) {
|
141 | util.deprecate(
|
142 | () => {},
|
143 | "A 'callback' argument need to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback.",
|
144 | "DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
|
145 | )();
|
146 | }
|
147 | return compiler;
|
148 | }
|
149 | });
|
150 |
|
151 | module.exports = webpack;
|