refactor: code

This commit is contained in:
alexander.akait 2024-11-20 18:35:15 +03:00
parent 171bae7912
commit 8a04c64e5e
8 changed files with 88 additions and 64 deletions

View File

@ -0,0 +1,19 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Arka Pratim Chaudhuri @arkapratimc
*/
"use strict";
const WebpackError = require("./WebpackError");
class FalseIIFEUmdWarning extends WebpackError {
constructor() {
super();
this.name = "FalseIIFEUmdWarning";
this.message =
"Configuration:\nSetting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\nLearn more: https://webpack.js.org/configuration/output/";
}
}
module.exports = FalseIIFEUmdWarning;

View File

@ -1,31 +0,0 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Arka Pratim Chaudhuri @arkapratimc
*/
"use strict";
const WebpackError = require("./WebpackError");
class FalseIifeUmdWarning extends WebpackError {
constructor() {
super();
this.name = "FalseIifeUmdWarning";
this.message =
"configuration\n" +
"Setting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\nLearn more: https://webpack.js.org/configuration/output/";
}
}
class WarnFalseIifeUmdPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"WarnFalseIifeUmdPlugin",
compilation => {
compilation.warnings.push(new FalseIifeUmdWarning());
}
);
}
}
module.exports = WarnFalseIifeUmdPlugin;

View File

@ -209,15 +209,6 @@ class WebpackOptionsApply extends OptionsApply {
} }
} }
if (
options.output.iife === false &&
options.output.library &&
options.output.library.type === "umd"
) {
const WarnFalseIifeUmdPlugin = require("./WarnFalseIifeUmdPlugin");
new WarnFalseIifeUmdPlugin().apply(compiler);
}
const enabledChunkLoadingTypes = const enabledChunkLoadingTypes =
/** @type {NonNullable<WebpackOptions["output"]["enabledChunkLoadingTypes"]>} */ /** @type {NonNullable<WebpackOptions["output"]["enabledChunkLoadingTypes"]>} */
(options.output.enabledChunkLoadingTypes); (options.output.enabledChunkLoadingTypes);

View File

@ -748,10 +748,7 @@ class JavascriptModulesPlugin {
const { chunk, chunkGraph, runtimeTemplate } = renderContext; const { chunk, chunkGraph, runtimeTemplate } = renderContext;
const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk); const runtimeRequirements = chunkGraph.getTreeRuntimeRequirements(chunk);
const iife = const iife = runtimeTemplate.isIIFE();
runtimeTemplate.isIIFE() ||
(runtimeTemplate.outputOptions.library &&
runtimeTemplate.outputOptions.library.type === "umd");
const bootstrap = this.renderBootstrap(renderContext, hooks); const bootstrap = this.renderBootstrap(renderContext, hooks);
const useSourceMap = hooks.useSourceMap.call(chunk, renderContext); const useSourceMap = hooks.useSourceMap.call(chunk, renderContext);

View File

@ -208,6 +208,23 @@ class EnableLibraryPlugin {
} }
case "umd": case "umd":
case "umd2": { case "umd2": {
if (compiler.options.output.iife === false) {
compiler.options.output.iife = true;
class WarnFalseIifeUmdPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"WarnFalseIifeUmdPlugin",
compilation => {
const FalseIIFEUmdWarning = require("../FalseIIFEUmdWarning");
compilation.warnings.push(new FalseIIFEUmdWarning());
}
);
}
}
new WarnFalseIifeUmdPlugin().apply(compiler);
}
enableExportProperty(); enableExportProperty();
const UmdLibraryPlugin = require("./UmdLibraryPlugin"); const UmdLibraryPlugin = require("./UmdLibraryPlugin");
new UmdLibraryPlugin({ new UmdLibraryPlugin({

View File

@ -385,8 +385,8 @@ it("should emit warning when 'output.iife'=false is used with 'output.library.ty
"errors": Array [], "errors": Array [],
"warnings": Array [ "warnings": Array [
Object { Object {
"message": "configuration\\nSetting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\\nLearn more: https://webpack.js.org/configuration/output/", "message": "Configuration:\\nSetting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\\nLearn more: https://webpack.js.org/configuration/output/",
"stack": "FalseIifeUmdWarning: configuration\\nSetting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\\nLearn more: https://webpack.js.org/configuration/output/", "stack": "FalseIIFEUmdWarning: Configuration:\\nSetting 'output.iife' to 'false' is incompatible with 'output.library.type' set to 'umd'. This configuration may cause unexpected behavior, as UMD libraries are expected to use an IIFE (Immediately Invoked Function Expression) to support various module formats. Consider setting 'output.iife' to 'true' or choosing a different 'library.type' to ensure compatibility.\\nLearn more: https://webpack.js.org/configuration/output/",
}, },
], ],
} }

View File

@ -153,22 +153,6 @@ module.exports = (env, { testPath }) => [
} }
} }
}, },
{
output: {
uniqueName: "false-iife-umd",
filename: "false-iife-umd.js",
library: {
type: "umd"
},
iife: false
},
resolve: {
alias: {
external: "./non-external"
}
},
ignoreWarnings: [error => error.name === "FalseIifeUmdWarning"]
},
{ {
output: { output: {
uniqueName: "true-iife-umd", uniqueName: "true-iife-umd",
@ -184,6 +168,38 @@ module.exports = (env, { testPath }) => [
} }
} }
}, },
{
output: {
uniqueName: "false-iife-umd",
filename: "false-iife-umd.js",
library: {
type: "umd"
},
iife: false
},
resolve: {
alias: {
external: "./non-external"
}
},
ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"]
},
{
output: {
uniqueName: "false-iife-umd2",
filename: "false-iife-umd2.js",
library: {
type: "umd2"
},
iife: false
},
resolve: {
alias: {
external: "./non-external"
}
},
ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"]
},
{ {
output: { output: {
uniqueName: "umd-default", uniqueName: "umd-default",

View File

@ -165,6 +165,18 @@ module.exports = (env, { testPath }) => [
}) })
] ]
}, },
{
resolve: {
alias: {
library: path.resolve(testPath, "../0-create-library/true-iife-umd.js")
}
},
plugins: [
new webpack.DefinePlugin({
NAME: JSON.stringify("true-iife-umd")
})
]
},
{ {
resolve: { resolve: {
alias: { alias: {
@ -180,12 +192,15 @@ module.exports = (env, { testPath }) => [
{ {
resolve: { resolve: {
alias: { alias: {
library: path.resolve(testPath, "../0-create-library/true-iife-umd.js") library: path.resolve(
testPath,
"../0-create-library/false-iife-umd2.js"
)
} }
}, },
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
NAME: JSON.stringify("true-iife-umd") NAME: JSON.stringify("false-iife-umd2")
}) })
] ]
}, },