UNPKG

@eaglesong/task-panorama

Version:
56 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const loader_utils_1 = require("loader-utils"); const upath_1 = tslib_1.__importDefault(require("upath")); const SingleEntryPlugin_1 = tslib_1.__importDefault(require("webpack/lib/SingleEntryPlugin")); function pitch(request) { var _a, _b, _c; this.addDependency(request); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const target = (_a = loader_utils_1.getOptions(this), (_a !== null && _a !== void 0 ? _a : {})); const compiler = createCompiler(this, loader_utils_1.interpolateName(this, (_b = target.filename, (_b !== null && _b !== void 0 ? _b : '[path][name].js')), { context: this.rootContext }), (_c = target.plugins, (_c !== null && _c !== void 0 ? _c : []))); runCompiler(compiler, this.async()); } exports.pitch = pitch; function runCompiler(compiler, callback) { // @ts-ignore compiler.runAsChild((error, chunks) => { if (error) { callback(error); } else if (chunks.length > 0) { const url = chunks[0].files[0]; callback(null, `module.exports = __webpack_public_path__ + ${JSON.stringify(url)};`); } else { callback(null, ''); } }); } function createCompiler(loader, filename, pluginsOption) { const oldCompilation = loader._compilation; const oldCompiler = loader._compiler; const oldOutputOptions = oldCompiler.options.output; const allowedPlugins = pluginsOption.filter((x) => typeof x === 'string'); const plugins = [ ...oldCompiler.options.plugins.filter(p => allowedPlugins.includes(p.constructor.name)), ...pluginsOption.filter((x) => typeof x !== 'string'), ]; // @ts-ignore const compilerName = upath_1.default.relative(oldCompiler.context, loader.resourcePath); // @ts-ignore const childCompiler = oldCompilation.createChildCompiler(compilerName, { ...oldOutputOptions, filename }, plugins); // eslint-disable-next-line prefer-destructuring const rawRequest = loader._module.rawRequest; new SingleEntryPlugin_1.default(loader.context, rawRequest, 'main').apply(childCompiler); childCompiler.hooks.compilation.tap('entry-loader', childCompilation => { if (!childCompilation.cache) return; if (!childCompilation.cache[compilerName]) childCompilation.cache[compilerName] = {}; childCompilation.cache = childCompilation.cache[compilerName]; }); return childCompiler; } //# sourceMappingURL=entry-loader.js.map