Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to set web/webworker and node environments #18336

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions declarations/WebpackOptions.d.ts
Expand Up @@ -2316,6 +2316,10 @@ export interface Environment {
* The environment supports EcmaScript Module syntax to import EcmaScript modules (import ... from '...').
*/
module?: boolean;
/**
* The environment is Node.js.
*/
node?: boolean;
/**
* The environment supports `node:` prefix for Node.js core modules.
*/
Expand All @@ -2328,6 +2332,14 @@ export interface Environment {
* The environment supports template literals.
*/
templateLiteral?: boolean;
/**
* The environment is web.
*/
web?: boolean;
/**
* The environment is Web Worker.
*/
webworker?: boolean;
}
/**
* Use a Trusted Types policy to create urls for chunks.
Expand Down
29 changes: 23 additions & 6 deletions lib/config/defaults.js
Expand Up @@ -1074,6 +1074,26 @@ const applyOutputDefaults = (
*/
const conditionallyOptimistic = (v, c) => (v === undefined && c) || v;

F(
environment,
"document",
() => tp && /** @type {boolean | undefined} */ (tp.document)
);
F(
environment,
"web",
() => tp && /** @type {boolean | undefined} */ (tp.web)
);
F(
environment,
"node",
() => tp && /** @type {boolean | undefined} */ (tp.node)
);
F(
environment,
"webworker",
() => tp && /** @type {boolean | undefined} */ (tp.webworker)
);
F(
environment,
"globalThis",
Expand All @@ -1082,7 +1102,9 @@ const applyOutputDefaults = (
F(
environment,
"bigIntLiteral",
() => /** @type {boolean | undefined} */ (tp && tp.bigIntLiteral)
() =>
tp &&
optimistic(/** @type {boolean | undefined} */ (tp && tp.bigIntLiteral))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the same for const/forOf/etc, so let's use the same here when no targets avaliable

);
F(
environment,
Expand Down Expand Up @@ -1151,11 +1173,6 @@ const applyOutputDefaults = (
output.module
)
);
F(
environment,
"document",
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.document))
);

const { trustedTypes } = output;
if (trustedTypes) {
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -889,6 +889,10 @@
"description": "The environment supports EcmaScript Module syntax to import EcmaScript modules (import ... from '...').",
"type": "boolean"
},
"node": {
"description": "The environment is Node.js.",
"type": "boolean"
},
"nodePrefixForCoreModules": {
"description": "The environment supports `node:` prefix for Node.js core modules.",
"type": "boolean"
Expand All @@ -900,6 +904,14 @@
"templateLiteral": {
"description": "The environment supports template literals.",
"type": "boolean"
},
"web": {
"description": "The environment is web.",
"type": "boolean"
},
"webworker": {
"description": "The environment is Web Worker.",
"type": "boolean"
}
}
},
Expand Down
84 changes: 74 additions & 10 deletions test/Defaults.unittest.js
Expand Up @@ -120,7 +120,7 @@ describe("snapshots", () => {
"environment": Object {
"arrowFunction": true,
"asyncFunction": true,
"bigIntLiteral": undefined,
"bigIntLiteral": true,
"const": true,
"destructuring": true,
"document": true,
Expand All @@ -129,9 +129,12 @@ describe("snapshots", () => {
"forOf": true,
"globalThis": undefined,
"module": undefined,
"node": false,
"nodePrefixForCoreModules": true,
"optionalChaining": true,
"templateLiteral": true,
"web": true,
"webworker": null,
},
"target": "web",
},
Expand Down Expand Up @@ -346,7 +349,7 @@ describe("snapshots", () => {
"environment": Object {
"arrowFunction": true,
"asyncFunction": true,
"bigIntLiteral": undefined,
"bigIntLiteral": true,
"const": true,
"destructuring": true,
"document": true,
Expand All @@ -355,9 +358,12 @@ describe("snapshots", () => {
"forOf": true,
"globalThis": undefined,
"module": undefined,
"node": false,
"nodePrefixForCoreModules": true,
"optionalChaining": true,
"templateLiteral": true,
"web": true,
"webworker": null,
},
"filename": "[name].js",
"globalObject": "self",
Expand Down Expand Up @@ -1317,6 +1323,14 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "web": true,
- "webworker": null,
+ "web": false,
+ "webworker": false,
@@ ... @@
- "target": "web",
+ "target": "node",
@@ ... @@
Expand Down Expand Up @@ -1346,6 +1360,14 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "web": true,
- "webworker": null,
+ "web": false,
+ "webworker": false,
@@ ... @@
- "globalObject": "self",
+ "globalObject": "global",
@@ ... @@
Expand Down Expand Up @@ -1440,6 +1462,9 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "webworker": null,
+ "webworker": true,
@@ ... @@
- "chunkLoading": "jsonp",
+ "chunkLoading": "import-scripts",
@@ ... @@
Expand All @@ -1448,6 +1473,9 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "webworker": null,
+ "webworker": true,
@@ ... @@
+ "worker",
@@ ... @@
- "target": "web",
Expand All @@ -1474,6 +1502,14 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "web": true,
- "webworker": null,
+ "web": false,
+ "webworker": false,
@@ ... @@
- "target": "web",
+ "target": "electron-main",
@@ ... @@
Expand Down Expand Up @@ -1503,6 +1539,14 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "web": true,
- "webworker": null,
+ "web": false,
+ "webworker": false,
@@ ... @@
- "globalObject": "self",
+ "globalObject": "global",
@@ ... @@
Expand Down Expand Up @@ -1607,6 +1651,12 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "target": "web",
+ "target": "electron-preload",
@@ ... @@
Expand Down Expand Up @@ -1636,6 +1686,12 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "globalObject": "self",
+ "globalObject": "global",
@@ ... @@
Expand Down Expand Up @@ -2060,7 +2116,7 @@ describe("snapshots", () => {
@@ ... @@
- "arrowFunction": true,
- "asyncFunction": true,
- "bigIntLiteral": undefined,
- "bigIntLiteral": true,
- "const": true,
- "destructuring": true,
+ "arrowFunction": false,
Expand All @@ -2074,18 +2130,22 @@ describe("snapshots", () => {
- "forOf": true,
- "globalThis": undefined,
- "module": undefined,
- "nodePrefixForCoreModules": true,
- "optionalChaining": true,
- "templateLiteral": true,
+ "dynamicImport": false,
+ "dynamicImportInWorker": false,
+ "forOf": false,
+ "globalThis": false,
+ "module": false,
@@ ... @@
- "nodePrefixForCoreModules": true,
- "optionalChaining": true,
- "templateLiteral": true,
+ "nodePrefixForCoreModules": false,
+ "optionalChaining": false,
+ "templateLiteral": false,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "chunkLoadingGlobal": "webpackChunkwebpack",
+ "chunkLoadingGlobal": "webpackChunkbrowserslist_test",
@@ ... @@
Expand All @@ -2094,7 +2154,7 @@ describe("snapshots", () => {
@@ ... @@
- "arrowFunction": true,
- "asyncFunction": true,
- "bigIntLiteral": undefined,
- "bigIntLiteral": true,
- "const": true,
- "destructuring": true,
+ "arrowFunction": false,
Expand All @@ -2108,18 +2168,22 @@ describe("snapshots", () => {
- "forOf": true,
- "globalThis": undefined,
- "module": undefined,
- "nodePrefixForCoreModules": true,
- "optionalChaining": true,
- "templateLiteral": true,
+ "dynamicImport": false,
+ "dynamicImportInWorker": false,
+ "forOf": false,
+ "globalThis": false,
+ "module": false,
@@ ... @@
- "nodePrefixForCoreModules": true,
- "optionalChaining": true,
- "templateLiteral": true,
+ "nodePrefixForCoreModules": false,
+ "optionalChaining": false,
+ "templateLiteral": false,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "hotUpdateGlobal": "webpackHotUpdatewebpack",
+ "hotUpdateGlobal": "webpackHotUpdatebrowserslist_test",
@@ ... @@
Expand Down
39 changes: 39 additions & 0 deletions test/__snapshots__/Cli.basictest.js.snap
Expand Up @@ -6361,6 +6361,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"output-environment-node": Object {
"configs": Array [
Object {
"description": "The environment is Node.js.",
"multiple": false,
"path": "output.environment.node",
"type": "boolean",
},
],
"description": "The environment is Node.js.",
"multiple": false,
"simpleType": "boolean",
},
"output-environment-node-prefix-for-core-modules": Object {
"configs": Array [
Object {
Expand Down Expand Up @@ -6400,6 +6413,32 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"output-environment-web": Object {
"configs": Array [
Object {
"description": "The environment is web.",
"multiple": false,
"path": "output.environment.web",
"type": "boolean",
},
],
"description": "The environment is web.",
"multiple": false,
"simpleType": "boolean",
},
"output-environment-webworker": Object {
"configs": Array [
Object {
"description": "The environment is Web Worker.",
"multiple": false,
"path": "output.environment.webworker",
"type": "boolean",
},
],
"description": "The environment is Web Worker.",
"multiple": false,
"simpleType": "boolean",
},
"output-filename": Object {
"configs": Array [
Object {
Expand Down