fix: non amd externals

This commit is contained in:
ahabhgk 2023-07-06 20:33:30 +08:00
parent 6be4065ade
commit 19028dcadc
8 changed files with 73 additions and 5 deletions

View File

@ -87,7 +87,11 @@ class AmdLibraryPlugin extends AbstractLibraryPlugin {
const modern = runtimeTemplate.supportsArrowFunction();
const modules = chunkGraph
.getChunkModules(chunk)
.filter(m => m instanceof ExternalModule);
.filter(
m =>
m instanceof ExternalModule &&
(m.externalType === "amd" || m.externalType === "amd-require")
);
const externals = /** @type {ExternalModule[]} */ (modules);
const externalsDepsArray = JSON.stringify(
externals.map(m =>

View File

@ -0,0 +1,33 @@
var fs = require("fs");
var path = require("path");
var dependencyArrayRegex = /define\((\[[^\]]*\]), (function)?\(/;
var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
var [, deps] = dependencyArrayRegex.exec(source);
it("should correctly import a AMD external", function() {
var external = require("external0");
expect(external).toBe("module 0");
});
it("should contain the AMD external in the dependency array", function() {
expect(deps).toContain("\"external0\"");
});
it("should correctly import a non-AMD external", function() {
var external = require("external1");
expect(external).toBe("abc");
});
it("should not contain the non-AMD external in the dependency array", function() {
expect(deps).not.toContain("\"external1\"");
});
it("should correctly import a asset external", function() {
var asset = new URL("#hash", import.meta.url);
expect(asset.href).toBe(__webpack_base_uri__ + "#hash");
});
it("should not contain asset external in the dependency array", function() {
expect(deps).not.toContain("\"#hash\"");
});

View File

@ -0,0 +1,5 @@
module.exports = {
modules: {
external0: "module 0"
}
};

View File

@ -0,0 +1,26 @@
const webpack = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
libraryTarget: "amd"
},
externals: {
external0: "external0",
external1: "var 'abc'"
},
node: {
__dirname: false,
__filename: false
},
target: "web",
externalsPresets: {
node: true
},
plugins: [
new webpack.BannerPlugin({
raw: true,
banner:
"function define(deps, fn) { fn(...deps.map(dep => require(dep))); }\n"
})
]
};

View File

@ -4,5 +4,5 @@ it("should name define", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");
expect(source).toMatch(/window\['clientContainer'\]\.define\(\[[^\]]*\], (function)?\(/);
expect(source).toMatch(/window\['clientContainer'\]\.define\((function)?\(/);
});

View File

@ -15,7 +15,7 @@ module.exports = {
new webpack.BannerPlugin({
raw: true,
banner:
"function define(deps, fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
"function define(fn) { fn(); }\nconst window = {};\nwindow['clientContainer'] = { define };\n"
})
]
};

View File

@ -4,5 +4,5 @@ it("should name define", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");
expect(source).toMatch(/define\(\[[^\]]*\], (function)?\(/);
expect(source).toMatch(/define\((function)?\(/);
});

View File

@ -11,7 +11,7 @@ module.exports = {
plugins: [
new webpack.BannerPlugin({
raw: true,
banner: "function define(deps, fn) { fn(); }\n"
banner: "function define(fn) { fn(); }\n"
})
]
};