support arrow function in umd

This commit is contained in:
Ivan Kopeykin 2022-03-18 18:53:27 +03:00
parent 86a8bd9618
commit 806ee08c3f
5 changed files with 44 additions and 4 deletions

View File

@ -310,9 +310,11 @@ class UmdLibraryPlugin extends AbstractLibraryPlugin {
: " var a = factory();\n") +
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
" }\n") +
`})(${
runtimeTemplate.outputOptions.globalObject
}, function(${externalsArguments(externals)}) {\nreturn `,
`})(${runtimeTemplate.outputOptions.globalObject}, ${
runtimeTemplate.supportsArrowFunction()
? `(${externalsArguments(externals)}) =>`
: `function(${externalsArguments(externals)})`
} {\nreturn `,
"webpack/universalModuleDefinition"
),
source,

View File

@ -619,7 +619,12 @@ const describeCases = config => {
const fn = runInNewContext
? vm.runInNewContext(code, globalContext, p)
: vm.runInThisContext(code, p);
fn.call(m.exports, ...argValues);
fn.call(
testConfig.nonEsmThis
? testConfig.nonEsmThis(module)
: m.exports,
...argValues
);
document.currentScript = oldCurrentScript;
return m.exports;
}

View File

@ -0,0 +1,5 @@
it("should compile and run", () => {
expect(hello()).toBe("hello");
});
export function hello() { return "hello"; }

View File

@ -0,0 +1,9 @@
const CONTEXT = {};
module.exports = {
nonEsmThis(module) {
return CONTEXT;
},
findBundle() {
return ["./runtime.js", "./main.js"];
}
};

View File

@ -0,0 +1,19 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
mode: "production",
entry: {
main: "./index.js"
},
output: {
filename: "[name].js",
library: "MyLibrary",
libraryTarget: "umd",
chunkLoading: "jsonp",
chunkFormat: "array-push",
globalObject: "this"
},
optimization: {
minimize: false,
runtimeChunk: "single"
}
};