Skip to content

Commit 6d8d96b

Browse files
authored
Merge pull request #17281 from webpack/feat-environment-to-loader-context
feat: support `environment` in loader context
2 parents e0cd928 + 9c7693b commit 6d8d96b

16 files changed

Lines changed: 201 additions & 9 deletions

File tree

declarations/LoaderContext.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
ImportModuleOptions
1414
} from "../lib/dependencies/LoaderPlugin";
1515
import type { Resolver } from "enhanced-resolve";
16+
import type { Environment } from "./WebpackOptions";
1617

1718
type ResolveCallback = Parameters<Resolver["resolve"]>[4];
1819
type Schema = Parameters<typeof validate>[0];
@@ -219,6 +220,12 @@ export interface LoaderRunnerLoaderContext<OptionsType> {
219220
* Example: "web"
220221
*/
221222
target: string;
223+
224+
/**
225+
* Tell what kind of ES-features may be used in the generated runtime-code.
226+
* Example: { arrowFunction: true }
227+
*/
228+
environment: Environment;
222229
}
223230

224231
type AdditionalData = {

declarations/WebpackOptions.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,10 +2246,18 @@ export interface Environment {
22462246
* The environment supports an async import() function to import EcmaScript modules.
22472247
*/
22482248
dynamicImport?: boolean;
2249+
/**
2250+
* The environment supports an async import() is available when creating a worker.
2251+
*/
2252+
dynamicImportInWorker?: boolean;
22492253
/**
22502254
* The environment supports 'for of' iteration ('for (const x of array) { ... }').
22512255
*/
22522256
forOf?: boolean;
2257+
/**
2258+
* The environment supports 'globalThis'.
2259+
*/
2260+
globalThis?: boolean;
22532261
/**
22542262
* The environment supports EcmaScript Module syntax to import EcmaScript modules (import ... from '...').
22552263
*/

lib/config/defaults.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const applyWebpackOptionsDefaults = options => {
248248

249249
applyLoaderDefaults(
250250
/** @type {NonNullable<WebpackOptions["loader"]>} */ (options.loader),
251-
{ targetProperties }
251+
{ targetProperties, environment: options.output.environment }
252252
);
253253

254254
F(options, "externalsType", () => {
@@ -1036,6 +1036,22 @@ const applyOutputDefaults = (
10361036
* @returns {boolean | undefined} true, when v is truthy or undefined, or c is truthy
10371037
*/
10381038
const conditionallyOptimistic = (v, c) => (v === undefined && c) || v;
1039+
1040+
F(
1041+
environment,
1042+
"globalThis",
1043+
() => /** @type {boolean | undefined} */ (tp && tp.globalThis)
1044+
);
1045+
F(
1046+
environment,
1047+
"bigIntLiteral",
1048+
() => /** @type {boolean | undefined} */ (tp && tp.bigIntLiteral)
1049+
);
1050+
F(
1051+
environment,
1052+
"const",
1053+
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.const))
1054+
);
10391055
F(
10401056
environment,
10411057
"arrowFunction",
@@ -1044,8 +1060,8 @@ const applyOutputDefaults = (
10441060
);
10451061
F(
10461062
environment,
1047-
"const",
1048-
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.const))
1063+
"forOf",
1064+
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.forOf))
10491065
);
10501066
F(
10511067
environment,
@@ -1055,20 +1071,28 @@ const applyOutputDefaults = (
10551071
);
10561072
F(
10571073
environment,
1058-
"forOf",
1059-
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.forOf))
1074+
"optionalChaining",
1075+
() =>
1076+
tp && optimistic(/** @type {boolean | undefined} */ (tp.optionalChaining))
10601077
);
10611078
F(
10621079
environment,
1063-
"bigIntLiteral",
1064-
() => /** @type {boolean | undefined} */ (tp && tp.bigIntLiteral)
1080+
"templateLiteral",
1081+
() =>
1082+
tp && optimistic(/** @type {boolean | undefined} */ (tp.templateLiteral))
10651083
);
10661084
F(environment, "dynamicImport", () =>
10671085
conditionallyOptimistic(
10681086
/** @type {boolean | undefined} */ (tp && tp.dynamicImport),
10691087
output.module
10701088
)
10711089
);
1090+
F(environment, "dynamicImportInWorker", () =>
1091+
conditionallyOptimistic(
1092+
/** @type {boolean | undefined} */ (tp && tp.dynamicImportInWorker),
1093+
output.module
1094+
)
1095+
);
10721096
F(environment, "module", () =>
10731097
conditionallyOptimistic(
10741098
/** @type {boolean | undefined} */ (tp && tp.module),
@@ -1215,9 +1239,10 @@ const applyExternalsPresetsDefaults = (
12151239
* @param {Loader} loader options
12161240
* @param {Object} options options
12171241
* @param {TargetProperties | false} options.targetProperties target properties
1242+
* @param {Environment} options.environment environment
12181243
* @returns {void}
12191244
*/
1220-
const applyLoaderDefaults = (loader, { targetProperties }) => {
1245+
const applyLoaderDefaults = (loader, { targetProperties, environment }) => {
12211246
F(loader, "target", () => {
12221247
if (targetProperties) {
12231248
if (targetProperties.electron) {
@@ -1231,6 +1256,7 @@ const applyLoaderDefaults = (loader, { targetProperties }) => {
12311256
if (targetProperties.web) return "web";
12321257
}
12331258
});
1259+
D(loader, "environment", environment);
12341260
};
12351261

12361262
/**

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,18 @@
744744
"description": "The environment supports an async import() function to import EcmaScript modules.",
745745
"type": "boolean"
746746
},
747+
"dynamicImportInWorker": {
748+
"description": "The environment supports an async import() is available when creating a worker.",
749+
"type": "boolean"
750+
},
747751
"forOf": {
748752
"description": "The environment supports 'for of' iteration ('for (const x of array) { ... }').",
749753
"type": "boolean"
750754
},
755+
"globalThis": {
756+
"description": "The environment supports 'globalThis'.",
757+
"type": "boolean"
758+
},
751759
"module": {
752760
"description": "The environment supports EcmaScript Module syntax to import EcmaScript modules (import ... from '...').",
753761
"type": "boolean"

test/Defaults.unittest.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ describe("snapshots", () => {
117117
"ignoreWarnings": undefined,
118118
"infrastructureLogging": Object {},
119119
"loader": Object {
120+
"environment": Object {
121+
"arrowFunction": true,
122+
"bigIntLiteral": undefined,
123+
"const": true,
124+
"destructuring": true,
125+
"dynamicImport": undefined,
126+
"dynamicImportInWorker": undefined,
127+
"forOf": true,
128+
"globalThis": undefined,
129+
"module": undefined,
130+
"optionalChaining": true,
131+
"templateLiteral": true,
132+
},
120133
"target": "web",
121134
},
122135
"mode": "none",
@@ -331,8 +344,12 @@ describe("snapshots", () => {
331344
"const": true,
332345
"destructuring": true,
333346
"dynamicImport": undefined,
347+
"dynamicImportInWorker": undefined,
334348
"forOf": true,
349+
"globalThis": undefined,
335350
"module": undefined,
351+
"optionalChaining": true,
352+
"templateLiteral": true,
336353
},
337354
"filename": "[name].js",
338355
"globalObject": "self",
@@ -885,11 +902,21 @@ describe("snapshots", () => {
885902
- "externalsType": "var",
886903
+ "externalsType": "module",
887904
@@ ... @@
905+
- "dynamicImport": undefined,
906+
- "dynamicImportInWorker": undefined,
907+
+ "dynamicImport": true,
908+
+ "dynamicImportInWorker": true,
909+
@@ ... @@
910+
- "module": undefined,
911+
+ "module": true,
912+
@@ ... @@
888913
- "chunkFilename": "[name].js",
889914
+ "chunkFilename": "[name].mjs",
890915
@@ ... @@
891916
- "dynamicImport": undefined,
917+
- "dynamicImportInWorker": undefined,
892918
+ "dynamicImport": true,
919+
+ "dynamicImportInWorker": true,
893920
@@ ... @@
894921
- "module": undefined,
895922
+ "module": true,
@@ -1991,6 +2018,29 @@ describe("snapshots", () => {
19912018
- "context": "<cwd>",
19922019
+ "context": "<cwd>/test/fixtures/browserslist",
19932020
@@ ... @@
2021+
- "arrowFunction": true,
2022+
- "bigIntLiteral": undefined,
2023+
- "const": true,
2024+
- "destructuring": true,
2025+
- "dynamicImport": undefined,
2026+
- "dynamicImportInWorker": undefined,
2027+
- "forOf": true,
2028+
- "globalThis": undefined,
2029+
- "module": undefined,
2030+
- "optionalChaining": true,
2031+
- "templateLiteral": true,
2032+
+ "arrowFunction": false,
2033+
+ "bigIntLiteral": false,
2034+
+ "const": false,
2035+
+ "destructuring": false,
2036+
+ "dynamicImport": false,
2037+
+ "dynamicImportInWorker": false,
2038+
+ "forOf": false,
2039+
+ "globalThis": false,
2040+
+ "module": false,
2041+
+ "optionalChaining": false,
2042+
+ "templateLiteral": false,
2043+
@@ ... @@
19942044
- "chunkLoadingGlobal": "webpackChunkwebpack",
19952045
+ "chunkLoadingGlobal": "webpackChunkbrowserslist_test",
19962046
@@ ... @@
@@ -2002,15 +2052,23 @@ describe("snapshots", () => {
20022052
- "const": true,
20032053
- "destructuring": true,
20042054
- "dynamicImport": undefined,
2055+
- "dynamicImportInWorker": undefined,
20052056
- "forOf": true,
2057+
- "globalThis": undefined,
20062058
- "module": undefined,
2059+
- "optionalChaining": true,
2060+
- "templateLiteral": true,
20072061
+ "arrowFunction": false,
20082062
+ "bigIntLiteral": false,
20092063
+ "const": false,
20102064
+ "destructuring": false,
20112065
+ "dynamicImport": false,
2066+
+ "dynamicImportInWorker": false,
20122067
+ "forOf": false,
2068+
+ "globalThis": false,
20132069
+ "module": false,
2070+
+ "optionalChaining": false,
2071+
+ "templateLiteral": false,
20142072
@@ ... @@
20152073
- "hotUpdateGlobal": "webpackHotUpdatewebpack",
20162074
+ "hotUpdateGlobal": "webpackHotUpdatebrowserslist_test",

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5946,6 +5946,19 @@ Object {
59465946
"multiple": false,
59475947
"simpleType": "boolean",
59485948
},
5949+
"output-environment-dynamic-import-in-worker": Object {
5950+
"configs": Array [
5951+
Object {
5952+
"description": "The environment supports an async import() is available when creating a worker.",
5953+
"multiple": false,
5954+
"path": "output.environment.dynamicImportInWorker",
5955+
"type": "boolean",
5956+
},
5957+
],
5958+
"description": "The environment supports an async import() is available when creating a worker.",
5959+
"multiple": false,
5960+
"simpleType": "boolean",
5961+
},
59495962
"output-environment-for-of": Object {
59505963
"configs": Array [
59515964
Object {
@@ -5959,6 +5972,19 @@ Object {
59595972
"multiple": false,
59605973
"simpleType": "boolean",
59615974
},
5975+
"output-environment-global-this": Object {
5976+
"configs": Array [
5977+
Object {
5978+
"description": "The environment supports 'globalThis'.",
5979+
"multiple": false,
5980+
"path": "output.environment.globalThis",
5981+
"type": "boolean",
5982+
},
5983+
],
5984+
"description": "The environment supports 'globalThis'.",
5985+
"multiple": false,
5986+
"simpleType": "boolean",
5987+
},
59625988
"output-environment-module": Object {
59635989
"configs": Array [
59645990
Object {

test/configCases/ecmaVersion/browserslist-config-env/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ module.exports = {
1313
"const": false,
1414
"destructuring": false,
1515
"dynamicImport": false,
16+
"dynamicImportInWorker": false,
1617
"forOf": false,
18+
"globalThis": false,
1719
"module": false,
20+
"optionalChaining": false,
21+
"templateLiteral": false,
1822
}
1923
`);
2024
expect(compilation.options.externalsPresets).toMatchInlineSnapshot(`

test/configCases/ecmaVersion/browserslist-config/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ module.exports = {
1313
"const": false,
1414
"destructuring": false,
1515
"dynamicImport": false,
16+
"dynamicImportInWorker": false,
1617
"forOf": false,
18+
"globalThis": false,
1719
"module": false,
20+
"optionalChaining": false,
21+
"templateLiteral": false,
1822
}
1923
`);
2024
expect(compilation.options.externalsPresets).toMatchInlineSnapshot(`

test/configCases/ecmaVersion/browserslist-query/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ module.exports = {
1111
"const": false,
1212
"destructuring": false,
1313
"dynamicImport": false,
14+
"dynamicImportInWorker": false,
1415
"forOf": false,
16+
"globalThis": false,
1517
"module": false,
18+
"optionalChaining": false,
19+
"templateLiteral": false,
1620
}
1721
`);
1822
expect(compilation.options.externalsPresets).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)