1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.pitch = pitch;
|
---|
7 | exports.default = _default;
|
---|
8 |
|
---|
9 | var _path = _interopRequireDefault(require("path"));
|
---|
10 |
|
---|
11 | var _utils = require("./utils");
|
---|
12 |
|
---|
13 | var _loaderOptions = _interopRequireDefault(require("./loader-options.json"));
|
---|
14 |
|
---|
15 | var _index = _interopRequireWildcard(require("./index"));
|
---|
16 |
|
---|
17 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
---|
18 |
|
---|
19 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
---|
20 |
|
---|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
22 |
|
---|
23 | function hotLoader(content, context) {
|
---|
24 | const accept = context.locals ? "" : "module.hot.accept(undefined, cssReload);";
|
---|
25 | return `${content}
|
---|
26 | if(module.hot) {
|
---|
27 | // ${Date.now()}
|
---|
28 | var cssReload = require(${(0, _utils.stringifyRequest)(context.context, _path.default.join(__dirname, "hmr/hotModuleReplacement.js"))})(module.id, ${JSON.stringify({ ...context.options,
|
---|
29 | locals: !!context.locals
|
---|
30 | })});
|
---|
31 | module.hot.dispose(cssReload);
|
---|
32 | ${accept}
|
---|
33 | }
|
---|
34 | `;
|
---|
35 | }
|
---|
36 |
|
---|
37 | function pitch(request) {
|
---|
38 | const options = this.getOptions(_loaderOptions.default);
|
---|
39 | const callback = this.async();
|
---|
40 | const optionsFromPlugin = this[_index.pluginSymbol];
|
---|
41 |
|
---|
42 | if (!optionsFromPlugin) {
|
---|
43 | callback(new Error("You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"));
|
---|
44 | return;
|
---|
45 | }
|
---|
46 |
|
---|
47 | const {
|
---|
48 | webpack
|
---|
49 | } = this._compiler;
|
---|
50 |
|
---|
51 | const handleExports = (originalExports, compilation, assets, assetsInfo) => {
|
---|
52 | let locals;
|
---|
53 | let namedExport;
|
---|
54 | const esModule = typeof options.esModule !== "undefined" ? options.esModule : true;
|
---|
55 |
|
---|
56 | const addDependencies = dependencies => {
|
---|
57 | if (!Array.isArray(dependencies) && dependencies != null) {
|
---|
58 | throw new Error(`Exported value was not extracted as an array: ${JSON.stringify(dependencies)}`);
|
---|
59 | }
|
---|
60 |
|
---|
61 | const identifierCountMap = new Map();
|
---|
62 | const emit = typeof options.emit !== "undefined" ? options.emit : true;
|
---|
63 | let lastDep;
|
---|
64 |
|
---|
65 | for (const dependency of dependencies) {
|
---|
66 | if (!dependency.identifier || !emit) {
|
---|
67 | // eslint-disable-next-line no-continue
|
---|
68 | continue;
|
---|
69 | }
|
---|
70 |
|
---|
71 | const count = identifierCountMap.get(dependency.identifier) || 0;
|
---|
72 |
|
---|
73 | const CssDependency = _index.default.getCssDependency(webpack);
|
---|
74 |
|
---|
75 | this._module.addDependency(lastDep = new CssDependency(dependency, dependency.context, count));
|
---|
76 |
|
---|
77 | identifierCountMap.set(dependency.identifier, count + 1);
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (lastDep && assets) {
|
---|
81 | lastDep.assets = assets;
|
---|
82 | lastDep.assetsInfo = assetsInfo;
|
---|
83 | }
|
---|
84 | };
|
---|
85 |
|
---|
86 | try {
|
---|
87 | // eslint-disable-next-line no-underscore-dangle
|
---|
88 | const exports = originalExports.__esModule ? originalExports.default : originalExports;
|
---|
89 | namedExport = // eslint-disable-next-line no-underscore-dangle
|
---|
90 | originalExports.__esModule && !("locals" in originalExports.default);
|
---|
91 |
|
---|
92 | if (namedExport) {
|
---|
93 | Object.keys(originalExports).forEach(key => {
|
---|
94 | if (key !== "default") {
|
---|
95 | if (!locals) {
|
---|
96 | locals = {};
|
---|
97 | }
|
---|
98 |
|
---|
99 | locals[key] = originalExports[key];
|
---|
100 | }
|
---|
101 | });
|
---|
102 | } else {
|
---|
103 | locals = exports && exports.locals;
|
---|
104 | }
|
---|
105 |
|
---|
106 | let dependencies;
|
---|
107 |
|
---|
108 | if (!Array.isArray(exports)) {
|
---|
109 | dependencies = [[null, exports]];
|
---|
110 | } else {
|
---|
111 | dependencies = exports.map(([id, content, media, sourceMap]) => {
|
---|
112 | let identifier = id;
|
---|
113 | let context;
|
---|
114 |
|
---|
115 | if (compilation) {
|
---|
116 | const module = (0, _utils.findModuleById)(compilation, id);
|
---|
117 | identifier = module.identifier();
|
---|
118 | ({
|
---|
119 | context
|
---|
120 | } = module);
|
---|
121 | } else {
|
---|
122 | // TODO check if this context is used somewhere
|
---|
123 | context = this.rootContext;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return {
|
---|
127 | identifier,
|
---|
128 | context,
|
---|
129 | content: Buffer.from(content),
|
---|
130 | media,
|
---|
131 | sourceMap: sourceMap ? Buffer.from(JSON.stringify(sourceMap)) : // eslint-disable-next-line no-undefined
|
---|
132 | undefined
|
---|
133 | };
|
---|
134 | });
|
---|
135 | }
|
---|
136 |
|
---|
137 | addDependencies(dependencies);
|
---|
138 | } catch (e) {
|
---|
139 | return callback(e);
|
---|
140 | }
|
---|
141 |
|
---|
142 | const result = locals ? namedExport ? Object.keys(locals).map(key => `\nexport var ${key} = ${JSON.stringify(locals[key])};`).join("") : `\n${esModule ? "export default" : "module.exports ="} ${JSON.stringify(locals)};` : esModule ? `\nexport {};` : "";
|
---|
143 | let resultSource = `// extracted by ${_index.pluginName}`;
|
---|
144 | resultSource += this.hot ? hotLoader(result, {
|
---|
145 | context: this.context,
|
---|
146 | options,
|
---|
147 | locals
|
---|
148 | }) : result;
|
---|
149 | return callback(null, resultSource);
|
---|
150 | };
|
---|
151 |
|
---|
152 | let {
|
---|
153 | publicPath
|
---|
154 | } = this._compilation.outputOptions;
|
---|
155 |
|
---|
156 | if (typeof options.publicPath === "string") {
|
---|
157 | // eslint-disable-next-line prefer-destructuring
|
---|
158 | publicPath = options.publicPath;
|
---|
159 | } else if (typeof options.publicPath === "function") {
|
---|
160 | publicPath = options.publicPath(this.resourcePath, this.rootContext);
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (publicPath === "auto") {
|
---|
164 | publicPath = _utils.AUTO_PUBLIC_PATH;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (optionsFromPlugin.experimentalUseImportModule) {
|
---|
168 | if (!this.importModule) {
|
---|
169 | callback(new Error("You are using experimentalUseImportModule but 'this.importModule' is not available in loader context. You need to have at least webpack 5.33.2."));
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | this.importModule(`${this.resourcePath}.webpack[javascript/auto]!=!${request}`, {
|
---|
174 | layer: options.layer,
|
---|
175 | publicPath
|
---|
176 | }, (err, exports) => {
|
---|
177 | if (err) {
|
---|
178 | callback(err);
|
---|
179 | return;
|
---|
180 | }
|
---|
181 |
|
---|
182 | handleExports(exports);
|
---|
183 | });
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | const loaders = this.loaders.slice(this.loaderIndex + 1);
|
---|
188 | this.addDependency(this.resourcePath);
|
---|
189 | const childFilename = "*";
|
---|
190 | const outputOptions = {
|
---|
191 | filename: childFilename,
|
---|
192 | publicPath
|
---|
193 | };
|
---|
194 |
|
---|
195 | const childCompiler = this._compilation.createChildCompiler(`${_index.pluginName} ${request}`, outputOptions); // The templates are compiled and executed by NodeJS - similar to server side rendering
|
---|
196 | // Unfortunately this causes issues as some loaders require an absolute URL to support ES Modules
|
---|
197 | // The following config enables relative URL support for the child compiler
|
---|
198 |
|
---|
199 |
|
---|
200 | childCompiler.options.module = { ...childCompiler.options.module
|
---|
201 | };
|
---|
202 | childCompiler.options.module.parser = { ...childCompiler.options.module.parser
|
---|
203 | };
|
---|
204 | childCompiler.options.module.parser.javascript = { ...childCompiler.options.module.parser.javascript,
|
---|
205 | url: "relative"
|
---|
206 | };
|
---|
207 | const {
|
---|
208 | NodeTemplatePlugin
|
---|
209 | } = webpack.node;
|
---|
210 | const {
|
---|
211 | NodeTargetPlugin
|
---|
212 | } = webpack.node;
|
---|
213 | new NodeTemplatePlugin(outputOptions).apply(childCompiler);
|
---|
214 | new NodeTargetPlugin().apply(childCompiler);
|
---|
215 | const {
|
---|
216 | EntryOptionPlugin
|
---|
217 | } = webpack;
|
---|
218 | const {
|
---|
219 | library: {
|
---|
220 | EnableLibraryPlugin
|
---|
221 | }
|
---|
222 | } = webpack;
|
---|
223 | new EnableLibraryPlugin("commonjs2").apply(childCompiler);
|
---|
224 | EntryOptionPlugin.applyEntryOption(childCompiler, this.context, {
|
---|
225 | child: {
|
---|
226 | library: {
|
---|
227 | type: "commonjs2"
|
---|
228 | },
|
---|
229 | import: [`!!${request}`]
|
---|
230 | }
|
---|
231 | });
|
---|
232 | const {
|
---|
233 | LimitChunkCountPlugin
|
---|
234 | } = webpack.optimize;
|
---|
235 | new LimitChunkCountPlugin({
|
---|
236 | maxChunks: 1
|
---|
237 | }).apply(childCompiler);
|
---|
238 | const {
|
---|
239 | NormalModule
|
---|
240 | } = webpack;
|
---|
241 | childCompiler.hooks.thisCompilation.tap(`${_index.pluginName} loader`, compilation => {
|
---|
242 | const normalModuleHook = NormalModule.getCompilationHooks(compilation).loader;
|
---|
243 | normalModuleHook.tap(`${_index.pluginName} loader`, (loaderContext, module) => {
|
---|
244 | if (module.request === request) {
|
---|
245 | // eslint-disable-next-line no-param-reassign
|
---|
246 | module.loaders = loaders.map(loader => {
|
---|
247 | return {
|
---|
248 | loader: loader.path,
|
---|
249 | options: loader.options,
|
---|
250 | ident: loader.ident
|
---|
251 | };
|
---|
252 | });
|
---|
253 | }
|
---|
254 | });
|
---|
255 | });
|
---|
256 | let source;
|
---|
257 | childCompiler.hooks.compilation.tap(_index.pluginName, compilation => {
|
---|
258 | compilation.hooks.processAssets.tap(_index.pluginName, () => {
|
---|
259 | source = compilation.assets[childFilename] && compilation.assets[childFilename].source(); // Remove all chunk assets
|
---|
260 |
|
---|
261 | compilation.chunks.forEach(chunk => {
|
---|
262 | chunk.files.forEach(file => {
|
---|
263 | compilation.deleteAsset(file);
|
---|
264 | });
|
---|
265 | });
|
---|
266 | });
|
---|
267 | });
|
---|
268 | childCompiler.runAsChild((error, entries, compilation) => {
|
---|
269 | if (error) {
|
---|
270 | return callback(error);
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (compilation.errors.length > 0) {
|
---|
274 | return callback(compilation.errors[0]);
|
---|
275 | }
|
---|
276 |
|
---|
277 | const assets = Object.create(null);
|
---|
278 | const assetsInfo = new Map();
|
---|
279 |
|
---|
280 | for (const asset of compilation.getAssets()) {
|
---|
281 | assets[asset.name] = asset.source;
|
---|
282 | assetsInfo.set(asset.name, asset.info);
|
---|
283 | }
|
---|
284 |
|
---|
285 | compilation.fileDependencies.forEach(dep => {
|
---|
286 | this.addDependency(dep);
|
---|
287 | }, this);
|
---|
288 | compilation.contextDependencies.forEach(dep => {
|
---|
289 | this.addContextDependency(dep);
|
---|
290 | }, this);
|
---|
291 |
|
---|
292 | if (!source) {
|
---|
293 | return callback(new Error("Didn't get a result from child compiler"));
|
---|
294 | }
|
---|
295 |
|
---|
296 | let originalExports;
|
---|
297 |
|
---|
298 | try {
|
---|
299 | originalExports = (0, _utils.evalModuleCode)(this, source, request);
|
---|
300 | } catch (e) {
|
---|
301 | return callback(e);
|
---|
302 | }
|
---|
303 |
|
---|
304 | return handleExports(originalExports, compilation, assets, assetsInfo);
|
---|
305 | });
|
---|
306 | } // eslint-disable-next-line func-names
|
---|
307 |
|
---|
308 |
|
---|
309 | function _default() {} |
---|