mirror of https://github.com/webpack/webpack.git
Merge pull request #14117 from webpack/feature/import-module
add importModule to loader context by default
This commit is contained in:
commit
41e3136c83
|
@ -1106,10 +1106,6 @@ export interface Experiments {
|
|||
* Build http(s): urls using a lockfile and resource content cache.
|
||||
*/
|
||||
buildHttp?: boolean | HttpUriOptions;
|
||||
/**
|
||||
* Enable build-time execution of modules from the module graph for plugins and loaders.
|
||||
*/
|
||||
executeModule?: boolean;
|
||||
/**
|
||||
* Enable module and chunk layers.
|
||||
*/
|
||||
|
|
|
@ -305,9 +305,7 @@ class WebpackOptionsApply extends OptionsApply {
|
|||
new RequireJsStuffPlugin().apply(compiler);
|
||||
}
|
||||
new CommonJsPlugin().apply(compiler);
|
||||
new LoaderPlugin({
|
||||
enableExecuteModule: options.experiments.executeModule
|
||||
}).apply(compiler);
|
||||
new LoaderPlugin({}).apply(compiler);
|
||||
if (options.node !== false) {
|
||||
const NodeStuffPlugin = require("./NodeStuffPlugin");
|
||||
new NodeStuffPlugin(options.node).apply(compiler);
|
||||
|
|
|
@ -259,7 +259,6 @@ const applyExperimentsDefaults = (experiments, { production, development }) => {
|
|||
D(experiments, "asyncWebAssembly", false);
|
||||
D(experiments, "outputModule", false);
|
||||
D(experiments, "asset", false);
|
||||
D(experiments, "executeModule", false);
|
||||
D(experiments, "layers", false);
|
||||
D(experiments, "lazyCompilation", false);
|
||||
D(experiments, "buildHttp", false);
|
||||
|
|
|
@ -37,11 +37,9 @@ const LoaderImportDependency = require("./LoaderImportDependency");
|
|||
class LoaderPlugin {
|
||||
/**
|
||||
* @param {Object} options options
|
||||
* @param {boolean=} options.enableExecuteModule execute module enabled
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
this._enableExecuteModule = !!options.enableExecuteModule;
|
||||
}
|
||||
constructor(options = {}) {}
|
||||
|
||||
/**
|
||||
* Apply the plugin
|
||||
* @param {Compiler} compiler the compiler instance
|
||||
|
@ -155,106 +153,104 @@ class LoaderPlugin {
|
|||
);
|
||||
};
|
||||
|
||||
if (this._enableExecuteModule) {
|
||||
/**
|
||||
* @param {string} request the request string to load the module from
|
||||
* @param {ImportModuleOptions=} options options
|
||||
* @param {ImportModuleCallback=} callback callback returning the exports
|
||||
* @returns {void}
|
||||
*/
|
||||
const importModule = (request, options, callback) => {
|
||||
const dep = new LoaderImportDependency(request);
|
||||
dep.loc = {
|
||||
name: request
|
||||
};
|
||||
const factory = compilation.dependencyFactories.get(
|
||||
/** @type {DepConstructor} */ (dep.constructor)
|
||||
/**
|
||||
* @param {string} request the request string to load the module from
|
||||
* @param {ImportModuleOptions=} options options
|
||||
* @param {ImportModuleCallback=} callback callback returning the exports
|
||||
* @returns {void}
|
||||
*/
|
||||
const importModule = (request, options, callback) => {
|
||||
const dep = new LoaderImportDependency(request);
|
||||
dep.loc = {
|
||||
name: request
|
||||
};
|
||||
const factory = compilation.dependencyFactories.get(
|
||||
/** @type {DepConstructor} */ (dep.constructor)
|
||||
);
|
||||
if (factory === undefined) {
|
||||
return callback(
|
||||
new Error(
|
||||
`No module factory available for dependency type: ${dep.constructor.name}`
|
||||
)
|
||||
);
|
||||
if (factory === undefined) {
|
||||
return callback(
|
||||
new Error(
|
||||
`No module factory available for dependency type: ${dep.constructor.name}`
|
||||
)
|
||||
}
|
||||
compilation.buildQueue.increaseParallelism();
|
||||
compilation.handleModuleCreation(
|
||||
{
|
||||
factory,
|
||||
dependencies: [dep],
|
||||
originModule: loaderContext._module,
|
||||
contextInfo: {
|
||||
issuerLayer: options.layer
|
||||
},
|
||||
context: loaderContext.context,
|
||||
connectOrigin: false
|
||||
},
|
||||
err => {
|
||||
compilation.buildQueue.decreaseParallelism();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const referencedModule = moduleGraph.getModule(dep);
|
||||
if (!referencedModule) {
|
||||
return callback(new Error("Cannot load the module"));
|
||||
}
|
||||
compilation.executeModule(
|
||||
referencedModule,
|
||||
{
|
||||
entryOptions: {
|
||||
publicPath: options.publicPath
|
||||
}
|
||||
},
|
||||
(err, result) => {
|
||||
if (err) return callback(err);
|
||||
for (const d of result.fileDependencies) {
|
||||
loaderContext.addDependency(d);
|
||||
}
|
||||
for (const d of result.contextDependencies) {
|
||||
loaderContext.addContextDependency(d);
|
||||
}
|
||||
for (const d of result.missingDependencies) {
|
||||
loaderContext.addMissingDependency(d);
|
||||
}
|
||||
for (const d of result.buildDependencies) {
|
||||
loaderContext.addBuildDependency(d);
|
||||
}
|
||||
if (result.cacheable === false)
|
||||
loaderContext.cacheable(false);
|
||||
for (const [name, { source, info }] of result.assets) {
|
||||
const { buildInfo } = loaderContext._module;
|
||||
if (!buildInfo.assets) {
|
||||
buildInfo.assets = Object.create(null);
|
||||
buildInfo.assetsInfo = new Map();
|
||||
}
|
||||
buildInfo.assets[name] = source;
|
||||
buildInfo.assetsInfo.set(name, info);
|
||||
}
|
||||
callback(null, result.exports);
|
||||
}
|
||||
);
|
||||
}
|
||||
compilation.buildQueue.increaseParallelism();
|
||||
compilation.handleModuleCreation(
|
||||
{
|
||||
factory,
|
||||
dependencies: [dep],
|
||||
originModule: loaderContext._module,
|
||||
contextInfo: {
|
||||
issuerLayer: options.layer
|
||||
},
|
||||
context: loaderContext.context,
|
||||
connectOrigin: false
|
||||
},
|
||||
err => {
|
||||
compilation.buildQueue.decreaseParallelism();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const referencedModule = moduleGraph.getModule(dep);
|
||||
if (!referencedModule) {
|
||||
return callback(new Error("Cannot load the module"));
|
||||
}
|
||||
compilation.executeModule(
|
||||
referencedModule,
|
||||
{
|
||||
entryOptions: {
|
||||
publicPath: options.publicPath
|
||||
}
|
||||
},
|
||||
(err, result) => {
|
||||
if (err) return callback(err);
|
||||
for (const d of result.fileDependencies) {
|
||||
loaderContext.addDependency(d);
|
||||
}
|
||||
for (const d of result.contextDependencies) {
|
||||
loaderContext.addContextDependency(d);
|
||||
}
|
||||
for (const d of result.missingDependencies) {
|
||||
loaderContext.addMissingDependency(d);
|
||||
}
|
||||
for (const d of result.buildDependencies) {
|
||||
loaderContext.addBuildDependency(d);
|
||||
}
|
||||
if (result.cacheable === false)
|
||||
loaderContext.cacheable(false);
|
||||
for (const [name, { source, info }] of result.assets) {
|
||||
const { buildInfo } = loaderContext._module;
|
||||
if (!buildInfo.assets) {
|
||||
buildInfo.assets = Object.create(null);
|
||||
buildInfo.assetsInfo = new Map();
|
||||
}
|
||||
buildInfo.assets[name] = source;
|
||||
buildInfo.assetsInfo.set(name, info);
|
||||
}
|
||||
callback(null, result.exports);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} request the request string to load the module from
|
||||
* @param {ImportModuleOptions} options options
|
||||
* @param {ImportModuleCallback=} callback callback returning the exports
|
||||
* @returns {Promise<any> | void} exports
|
||||
*/
|
||||
loaderContext.importModule = (request, options, callback) => {
|
||||
if (!callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
importModule(request, options || {}, (err, result) => {
|
||||
if (err) reject(err);
|
||||
else resolve(result);
|
||||
});
|
||||
/**
|
||||
* @param {string} request the request string to load the module from
|
||||
* @param {ImportModuleOptions} options options
|
||||
* @param {ImportModuleCallback=} callback callback returning the exports
|
||||
* @returns {Promise<any> | void} exports
|
||||
*/
|
||||
loaderContext.importModule = (request, options, callback) => {
|
||||
if (!callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
importModule(request, options || {}, (err, result) => {
|
||||
if (err) reject(err);
|
||||
else resolve(result);
|
||||
});
|
||||
}
|
||||
return importModule(request, options || {}, callback);
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
return importModule(request, options || {}, callback);
|
||||
};
|
||||
}
|
||||
);
|
||||
});
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -699,10 +699,6 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"executeModule": {
|
||||
"description": "Enable build-time execution of modules from the module graph for plugins and loaders.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"layers": {
|
||||
"description": "Enable module and chunk layers.",
|
||||
"type": "boolean"
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -524,19 +524,6 @@ Object {
|
|||
"multiple": false,
|
||||
"simpleType": "boolean",
|
||||
},
|
||||
"experiments-execute-module": Object {
|
||||
"configs": Array [
|
||||
Object {
|
||||
"description": "Enable build-time execution of modules from the module graph for plugins and loaders.",
|
||||
"multiple": false,
|
||||
"path": "experiments.executeModule",
|
||||
"type": "boolean",
|
||||
},
|
||||
],
|
||||
"description": "Enable build-time execution of modules from the module graph for plugins and loaders.",
|
||||
"multiple": false,
|
||||
"simpleType": "boolean",
|
||||
},
|
||||
"experiments-layers": Object {
|
||||
"configs": Array [
|
||||
Object {
|
||||
|
|
|
@ -41,9 +41,6 @@ module.exports = {
|
|||
}
|
||||
]
|
||||
},
|
||||
experiments: {
|
||||
executeModule: true
|
||||
},
|
||||
plugins: [
|
||||
compiler =>
|
||||
compiler.hooks.done.tap("test case", stats => {
|
||||
|
|
|
@ -14,9 +14,6 @@ module.exports = {
|
|||
}
|
||||
]
|
||||
},
|
||||
experiments: {
|
||||
executeModule: true
|
||||
},
|
||||
plugins: [
|
||||
compiler =>
|
||||
compiler.hooks.done.tap("test case", stats => {
|
||||
|
|
|
@ -8,8 +8,5 @@ module.exports = {
|
|||
type: "json"
|
||||
}
|
||||
]
|
||||
},
|
||||
experiments: {
|
||||
executeModule: true
|
||||
}
|
||||
};
|
||||
|
|
|
@ -3282,11 +3282,6 @@ declare interface Experiments {
|
|||
*/
|
||||
buildHttp?: boolean | HttpUriOptions;
|
||||
|
||||
/**
|
||||
* Enable build-time execution of modules from the module graph for plugins and loaders.
|
||||
*/
|
||||
executeModule?: boolean;
|
||||
|
||||
/**
|
||||
* Enable module and chunk layers.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue