feat: allow to set `web`/`webworker` and `node` environments

This commit is contained in:
alexander.akait 2024-04-17 15:11:10 +03:00
parent b630e21e10
commit 7125cc6884
7 changed files with 176 additions and 17 deletions

View File

@ -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.
*/
@ -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.

View File

@ -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",
@ -1082,7 +1102,9 @@ const applyOutputDefaults = (
F(
environment,
"bigIntLiteral",
() => /** @type {boolean | undefined} */ (tp && tp.bigIntLiteral)
() =>
tp &&
optimistic(/** @type {boolean | undefined} */ (tp && tp.bigIntLiteral))
);
F(
environment,
@ -1151,11 +1173,6 @@ const applyOutputDefaults = (
output.module
)
);
F(
environment,
"document",
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.document))
);
const { trustedTypes } = output;
if (trustedTypes) {

File diff suppressed because one or more lines are too long

View File

@ -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"
@ -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"
}
}
},

View File

@ -120,7 +120,7 @@ describe("snapshots", () => {
"environment": Object {
"arrowFunction": true,
"asyncFunction": true,
"bigIntLiteral": undefined,
"bigIntLiteral": true,
"const": true,
"destructuring": true,
"document": true,
@ -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",
},
@ -346,7 +349,7 @@ describe("snapshots", () => {
"environment": Object {
"arrowFunction": true,
"asyncFunction": true,
"bigIntLiteral": undefined,
"bigIntLiteral": true,
"const": true,
"destructuring": true,
"document": true,
@ -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",
@ -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",
@@ ... @@
@ -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",
@@ ... @@
@ -1440,6 +1462,9 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "webworker": null,
+ "webworker": true,
@@ ... @@
- "chunkLoading": "jsonp",
+ "chunkLoading": "import-scripts",
@@ ... @@
@ -1448,6 +1473,9 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "webworker": null,
+ "webworker": true,
@@ ... @@
+ "worker",
@@ ... @@
- "target": "web",
@ -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",
@@ ... @@
@ -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",
@@ ... @@
@ -1607,6 +1651,12 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "target": "web",
+ "target": "electron-preload",
@@ ... @@
@ -1636,6 +1686,12 @@ describe("snapshots", () => {
- "document": true,
+ "document": false,
@@ ... @@
- "node": false,
+ "node": true,
@@ ... @@
- "webworker": null,
+ "webworker": false,
@@ ... @@
- "globalObject": "self",
+ "globalObject": "global",
@@ ... @@
@ -2060,7 +2116,7 @@ describe("snapshots", () => {
@@ ... @@
- "arrowFunction": true,
- "asyncFunction": true,
- "bigIntLiteral": undefined,
- "bigIntLiteral": true,
- "const": true,
- "destructuring": true,
+ "arrowFunction": false,
@ -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",
@@ ... @@
@ -2094,7 +2154,7 @@ describe("snapshots", () => {
@@ ... @@
- "arrowFunction": true,
- "asyncFunction": true,
- "bigIntLiteral": undefined,
- "bigIntLiteral": true,
- "const": true,
- "destructuring": true,
+ "arrowFunction": false,
@ -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",
@@ ... @@

View File

@ -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 {
@ -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 {

15
types.d.ts vendored
View File

@ -3948,6 +3948,11 @@ declare interface Environment {
*/
module?: boolean;
/**
* The environment is Node.js.
*/
node?: boolean;
/**
* The environment supports `node:` prefix for Node.js core modules.
*/
@ -3962,6 +3967,16 @@ declare interface Environment {
* The environment supports template literals.
*/
templateLiteral?: boolean;
/**
* The environment is web.
*/
web?: boolean;
/**
* The environment is Web Worker.
*/
webworker?: boolean;
}
declare class EnvironmentPlugin {
constructor(...keys: any[]);