forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new external type "module-import"
- Loading branch information
Showing
19 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Ivan Kopeykin @vankop | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const makeSerializable = require("../util/makeSerializable"); | ||
const CachedConstDependency = require("./CachedConstDependency"); | ||
const ModuleDependency = require("./ModuleDependency"); | ||
|
||
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ | ||
/** @typedef {import("../Dependency")} Dependency */ | ||
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */ | ||
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ | ||
/** @typedef {import("../javascript/JavascriptModulesPlugin").ChunkRenderContext} ChunkRenderContext */ | ||
/** @typedef {import("../javascript/JavascriptParser").Range} Range */ | ||
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ | ||
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ | ||
/** @typedef {import("../util/Hash")} Hash */ | ||
|
||
class ExternalModuleImportDependency extends ModuleDependency { | ||
/** | ||
* @param {string} request the request | ||
* @param {string| string[]} targetRequest the original request | ||
* @param {Range} range expression range | ||
*/ | ||
constructor(request, targetRequest, range) { | ||
super(request); | ||
this.targetRequest = targetRequest; | ||
this.range = range; | ||
} | ||
|
||
get type() { | ||
return "external module-import"; | ||
} | ||
|
||
/** | ||
* @param {ObjectSerializerContext} context context | ||
*/ | ||
serialize(context) { | ||
const { write } = context; | ||
write(this.targetRequest); | ||
super.serialize(context); | ||
} | ||
|
||
/** | ||
* @param {ObjectDeserializerContext} context context | ||
*/ | ||
deserialize(context) { | ||
const { read } = context; | ||
this.targetRequest = read(); | ||
super.deserialize(context); | ||
} | ||
} | ||
|
||
makeSerializable( | ||
ExternalModuleImportDependency, | ||
"webpack/lib/dependencies/ExternalModuleImportDependency" | ||
); | ||
|
||
ExternalModuleImportDependency.Template = class ExternalModuleImportDependencyTemplate extends ( | ||
CachedConstDependency.Template | ||
) { | ||
/** | ||
* @param {Dependency} dependency the dependency for which the template should be applied | ||
* @param {ReplaceSource} source the current replace source which can be modified | ||
* @param {DependencyTemplateContext} templateContext the context object | ||
* @returns {void} | ||
*/ | ||
apply(dependency, source, templateContext) { | ||
const dep = /** @type {ExternalModuleImportDependency} */ (dependency); | ||
const content = JSON.stringify(dep.targetRequest); | ||
source.replace(dep.range[0], dep.range[1] - 1, `import(${content})`); | ||
} | ||
}; | ||
|
||
module.exports = ExternalModuleImportDependency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1285,6 +1285,7 @@ | |
"system", | ||
"promise", | ||
"import", | ||
"module-import", | ||
"script", | ||
"node-commonjs" | ||
] | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"system", | ||
"promise", | ||
"import", | ||
"module-import", | ||
"script", | ||
"node-commonjs" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,7 @@ | |
"system", | ||
"promise", | ||
"import", | ||
"module-import", | ||
"script", | ||
"node-commonjs" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1030,6 +1030,7 @@ Object { | |
"system", | ||
"promise", | ||
"import", | ||
"module-import", | ||
"script", | ||
"node-commonjs", | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
it("should allow async externals", async () => { | ||
const fs1 = await import("fs"); | ||
const fs2 = await import("node:fs"); | ||
const fs3 = await import("node-fs"); | ||
|
||
expect(fs1).toStrictEqual(fs2); | ||
expect(fs1).toStrictEqual(fs3); | ||
}); |
1 change: 1 addition & 0 deletions
1
test/configCases/externals/module-import/module-import-external.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 42; |
23 changes: 23 additions & 0 deletions
23
test/configCases/externals/module-import/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
externals: { | ||
fs: "module-import fs", | ||
"node:fs": "module-import node:fs", | ||
"node-fs": "module-import fs" | ||
}, | ||
output: { | ||
module: true, | ||
library: { | ||
type: "module" | ||
} | ||
}, | ||
target: ["es2020"], | ||
experiments: { | ||
outputModule: true | ||
}, | ||
optimization: { | ||
concatenateModules: true, | ||
usedExports: true, | ||
providedExports: true, | ||
mangleExports: true | ||
} | ||
}; |
Oops, something went wrong.