1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const util = require("util");
|
---|
9 | const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check.js");
|
---|
10 | const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
|
---|
11 | const Compiler = require("./Compiler");
|
---|
12 | const MultiCompiler = require("./MultiCompiler");
|
---|
13 | const WebpackOptionsApply = require("./WebpackOptionsApply");
|
---|
14 | const {
|
---|
15 | applyWebpackOptionsDefaults,
|
---|
16 | applyWebpackOptionsBaseDefaults
|
---|
17 | } = require("./config/defaults");
|
---|
18 | const { getNormalizedWebpackOptions } = require("./config/normalization");
|
---|
19 | const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
|
---|
20 | const memoize = require("./util/memoize");
|
---|
21 |
|
---|
22 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
|
---|
23 | /** @typedef {import("./Compiler").WatchOptions} WatchOptions */
|
---|
24 | /** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
|
---|
25 | /** @typedef {import("./MultiStats")} MultiStats */
|
---|
26 | /** @typedef {import("./Stats")} Stats */
|
---|
27 |
|
---|
28 | const getValidateSchema = memoize(() => require("./validateSchema"));
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @template T
|
---|
32 | * @callback Callback
|
---|
33 | * @param {Error=} err
|
---|
34 | * @param {T=} stats
|
---|
35 | * @returns {void}
|
---|
36 | */
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @param {ReadonlyArray<WebpackOptions>} childOptions options array
|
---|
40 | * @param {MultiCompilerOptions} options options
|
---|
41 | * @returns {MultiCompiler} a multi-compiler
|
---|
42 | */
|
---|
43 | const createMultiCompiler = (childOptions, options) => {
|
---|
44 | const compilers = childOptions.map(options => createCompiler(options));
|
---|
45 | const compiler = new MultiCompiler(compilers, options);
|
---|
46 | for (const childCompiler of compilers) {
|
---|
47 | if (childCompiler.options.dependencies) {
|
---|
48 | compiler.setDependencies(
|
---|
49 | childCompiler,
|
---|
50 | childCompiler.options.dependencies
|
---|
51 | );
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return compiler;
|
---|
55 | };
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * @param {WebpackOptions} rawOptions options object
|
---|
59 | * @returns {Compiler} a compiler
|
---|
60 | */
|
---|
61 | const createCompiler = rawOptions => {
|
---|
62 | const options = getNormalizedWebpackOptions(rawOptions);
|
---|
63 | applyWebpackOptionsBaseDefaults(options);
|
---|
64 | const compiler = new Compiler(options.context);
|
---|
65 | compiler.options = options;
|
---|
66 | new NodeEnvironmentPlugin({
|
---|
67 | infrastructureLogging: options.infrastructureLogging
|
---|
68 | }).apply(compiler);
|
---|
69 | if (Array.isArray(options.plugins)) {
|
---|
70 | for (const plugin of options.plugins) {
|
---|
71 | if (typeof plugin === "function") {
|
---|
72 | plugin.call(compiler, compiler);
|
---|
73 | } else {
|
---|
74 | plugin.apply(compiler);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | applyWebpackOptionsDefaults(options);
|
---|
79 | compiler.hooks.environment.call();
|
---|
80 | compiler.hooks.afterEnvironment.call();
|
---|
81 | new WebpackOptionsApply().process(options, compiler);
|
---|
82 | compiler.hooks.initialize.call();
|
---|
83 | return compiler;
|
---|
84 | };
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @callback WebpackFunctionSingle
|
---|
88 | * @param {WebpackOptions} options options object
|
---|
89 | * @param {Callback<Stats>=} callback callback
|
---|
90 | * @returns {Compiler} the compiler object
|
---|
91 | */
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * @callback WebpackFunctionMulti
|
---|
95 | * @param {ReadonlyArray<WebpackOptions> & MultiCompilerOptions} options options objects
|
---|
96 | * @param {Callback<MultiStats>=} callback callback
|
---|
97 | * @returns {MultiCompiler} the multi compiler object
|
---|
98 | */
|
---|
99 |
|
---|
100 | const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
|
---|
101 | /**
|
---|
102 | * @param {WebpackOptions | (ReadonlyArray<WebpackOptions> & MultiCompilerOptions)} options options
|
---|
103 | * @param {Callback<Stats> & Callback<MultiStats>=} callback callback
|
---|
104 | * @returns {Compiler | MultiCompiler}
|
---|
105 | */
|
---|
106 | (options, callback) => {
|
---|
107 | const create = () => {
|
---|
108 | if (!webpackOptionsSchemaCheck(options)) {
|
---|
109 | getValidateSchema()(webpackOptionsSchema, options);
|
---|
110 | }
|
---|
111 | /** @type {MultiCompiler|Compiler} */
|
---|
112 | let compiler;
|
---|
113 | let watch = false;
|
---|
114 | /** @type {WatchOptions|WatchOptions[]} */
|
---|
115 | let watchOptions;
|
---|
116 | if (Array.isArray(options)) {
|
---|
117 | /** @type {MultiCompiler} */
|
---|
118 | compiler = createMultiCompiler(
|
---|
119 | options,
|
---|
120 | /** @type {MultiCompilerOptions} */ (options)
|
---|
121 | );
|
---|
122 | watch = options.some(options => options.watch);
|
---|
123 | watchOptions = options.map(options => options.watchOptions || {});
|
---|
124 | } else {
|
---|
125 | const webpackOptions = /** @type {WebpackOptions} */ (options);
|
---|
126 | /** @type {Compiler} */
|
---|
127 | compiler = createCompiler(webpackOptions);
|
---|
128 | watch = webpackOptions.watch;
|
---|
129 | watchOptions = webpackOptions.watchOptions || {};
|
---|
130 | }
|
---|
131 | return { compiler, watch, watchOptions };
|
---|
132 | };
|
---|
133 | if (callback) {
|
---|
134 | try {
|
---|
135 | const { compiler, watch, watchOptions } = create();
|
---|
136 | if (watch) {
|
---|
137 | compiler.watch(watchOptions, callback);
|
---|
138 | } else {
|
---|
139 | compiler.run((err, stats) => {
|
---|
140 | compiler.close(err2 => {
|
---|
141 | callback(err || err2, stats);
|
---|
142 | });
|
---|
143 | });
|
---|
144 | }
|
---|
145 | return compiler;
|
---|
146 | } catch (err) {
|
---|
147 | process.nextTick(() => callback(err));
|
---|
148 | return null;
|
---|
149 | }
|
---|
150 | } else {
|
---|
151 | const { compiler, watch } = create();
|
---|
152 | if (watch) {
|
---|
153 | util.deprecate(
|
---|
154 | () => {},
|
---|
155 | "A 'callback' argument needs 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.",
|
---|
156 | "DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
|
---|
157 | )();
|
---|
158 | }
|
---|
159 | return compiler;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | );
|
---|
163 |
|
---|
164 | module.exports = webpack;
|
---|