source: imaps-frontend/node_modules/webpack/lib/webpack.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[79a0317]1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const webpackOptionsSchemaCheck = require("../schemas/WebpackOptions.check.js");
10const webpackOptionsSchema = require("../schemas/WebpackOptions.json");
11const Compiler = require("./Compiler");
12const MultiCompiler = require("./MultiCompiler");
13const WebpackOptionsApply = require("./WebpackOptionsApply");
14const {
15 applyWebpackOptionsDefaults,
16 applyWebpackOptionsBaseDefaults
17} = require("./config/defaults");
18const { getNormalizedWebpackOptions } = require("./config/normalization");
19const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
20const memoize = require("./util/memoize");
21
22/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
23/** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
24/** @typedef {import("./Compiler").WatchOptions} WatchOptions */
25/** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
26/** @typedef {import("./MultiStats")} MultiStats */
27/** @typedef {import("./Stats")} Stats */
28
29const getValidateSchema = memoize(() => require("./validateSchema"));
30
31/**
32 * @template T
33 * @callback Callback
34 * @param {Error | null} err
35 * @param {T=} stats
36 * @returns {void}
37 */
38
39/**
40 * @param {ReadonlyArray<WebpackOptions>} childOptions options array
41 * @param {MultiCompilerOptions} options options
42 * @returns {MultiCompiler} a multi-compiler
43 */
44const createMultiCompiler = (childOptions, options) => {
45 const compilers = childOptions.map((options, index) =>
46 createCompiler(options, index)
47 );
48 const compiler = new MultiCompiler(compilers, options);
49 for (const childCompiler of compilers) {
50 if (childCompiler.options.dependencies) {
51 compiler.setDependencies(
52 childCompiler,
53 childCompiler.options.dependencies
54 );
55 }
56 }
57 return compiler;
58};
59
60/**
61 * @param {WebpackOptions} rawOptions options object
62 * @param {number} [compilerIndex] index of compiler
63 * @returns {Compiler} a compiler
64 */
65const createCompiler = (rawOptions, compilerIndex) => {
66 const options = getNormalizedWebpackOptions(rawOptions);
67 applyWebpackOptionsBaseDefaults(options);
68 const compiler = new Compiler(
69 /** @type {string} */ (options.context),
70 options
71 );
72 new NodeEnvironmentPlugin({
73 infrastructureLogging: options.infrastructureLogging
74 }).apply(compiler);
75 if (Array.isArray(options.plugins)) {
76 for (const plugin of options.plugins) {
77 if (typeof plugin === "function") {
78 /** @type {WebpackPluginFunction} */
79 (plugin).call(compiler, compiler);
80 } else if (plugin) {
81 plugin.apply(compiler);
82 }
83 }
84 }
85 const resolvedDefaultOptions = applyWebpackOptionsDefaults(
86 options,
87 compilerIndex
88 );
89 if (resolvedDefaultOptions.platform) {
90 compiler.platform = resolvedDefaultOptions.platform;
91 }
92 compiler.hooks.environment.call();
93 compiler.hooks.afterEnvironment.call();
94 new WebpackOptionsApply().process(options, compiler);
95 compiler.hooks.initialize.call();
96 return compiler;
97};
98
99/**
100 * @callback WebpackFunctionSingle
101 * @param {WebpackOptions} options options object
102 * @param {Callback<Stats>=} callback callback
103 * @returns {Compiler} the compiler object
104 */
105
106/**
107 * @callback WebpackFunctionMulti
108 * @param {ReadonlyArray<WebpackOptions> & MultiCompilerOptions} options options objects
109 * @param {Callback<MultiStats>=} callback callback
110 * @returns {MultiCompiler} the multi compiler object
111 */
112
113/**
114 * @template T
115 * @param {Array<T> | T} options options
116 * @returns {Array<T>} array of options
117 */
118const asArray = options =>
119 Array.isArray(options) ? Array.from(options) : [options];
120
121const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
122 /**
123 * @param {WebpackOptions | (ReadonlyArray<WebpackOptions> & MultiCompilerOptions)} options options
124 * @param {Callback<Stats> & Callback<MultiStats>=} callback callback
125 * @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
126 */
127 (options, callback) => {
128 const create = () => {
129 if (!asArray(options).every(webpackOptionsSchemaCheck)) {
130 getValidateSchema()(webpackOptionsSchema, options);
131 util.deprecate(
132 () => {},
133 "webpack bug: Pre-compiled schema reports error while real schema is happy. This has performance drawbacks.",
134 "DEP_WEBPACK_PRE_COMPILED_SCHEMA_INVALID"
135 )();
136 }
137 /** @type {MultiCompiler|Compiler} */
138 let compiler;
139 /** @type {boolean | undefined} */
140 let watch = false;
141 /** @type {WatchOptions|WatchOptions[]} */
142 let watchOptions;
143 if (Array.isArray(options)) {
144 /** @type {MultiCompiler} */
145 compiler = createMultiCompiler(
146 options,
147 /** @type {MultiCompilerOptions} */ (options)
148 );
149 watch = options.some(options => options.watch);
150 watchOptions = options.map(options => options.watchOptions || {});
151 } else {
152 const webpackOptions = /** @type {WebpackOptions} */ (options);
153 /** @type {Compiler} */
154 compiler = createCompiler(webpackOptions);
155 watch = webpackOptions.watch;
156 watchOptions = webpackOptions.watchOptions || {};
157 }
158 return { compiler, watch, watchOptions };
159 };
160 if (callback) {
161 try {
162 const { compiler, watch, watchOptions } = create();
163 if (watch) {
164 compiler.watch(watchOptions, callback);
165 } else {
166 compiler.run((err, stats) => {
167 compiler.close(err2 => {
168 callback(
169 err || err2,
170 /** @type {options extends WebpackOptions ? Stats : MultiStats} */
171 (stats)
172 );
173 });
174 });
175 }
176 return compiler;
177 } catch (err) {
178 process.nextTick(() => callback(/** @type {Error} */ (err)));
179 return null;
180 }
181 } else {
182 const { compiler, watch } = create();
183 if (watch) {
184 util.deprecate(
185 () => {},
186 "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.",
187 "DEP_WEBPACK_WATCH_WITHOUT_CALLBACK"
188 )();
189 }
190 return compiler;
191 }
192 }
193);
194
195module.exports = webpack;
Note: See TracBrowser for help on using the repository browser.