-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Labels
angular/build:applicationarea: @angular/buildfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix
Description
Command
build
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
Defines are only applied to the main
esbuild compilation and not others, like global scripts
.
Note: Happy to try to fix this if this is indeed a bug.
Related issue:
- Provide the ability to remove code needed only in dev mode from the production bundle angular#51175 - I was trying to work around
ngDevMode
not being applied to global scripts since it's private
Minimal Reproduction
# Create a new project. Use the default values.
npx @angular/cli new no_define
cd no_define
# USER: Download the patch (below) to `patch`
git apply < patch
$ yarn build --configuration development
[...]
# My comments are prefixed with `###`
$ grep -B1 -r REPRO dist/*/browser/*.js
dist/no_define/browser/main.js-if (true) {
### Expected: REPRO got replaced with `true` in development (no optimization)
dist/no_define/browser/main.js: console.log("MAIN_REPRO");
--
dist/no_define/browser/scripts.js:if (REPRO) {
### Unexpected: REPRO is undefined in the global scope and should be `true` (like main.js)
dist/no_define/browser/scripts.js: console.log("SCRIPT_REPRO");
$ yarn build
[...]
$ grep -B1 -r REPRO dist/*/browser/*.js
dist/no_define/browser/scripts-VXXF5WSM.js:REPRO&&console.log("SCRIPT_REPRO");
### Unexpected: REPRO is undefined in the global scope and the statement was not fully optimized
Patch to apply
diff --git a/angular.json b/angular.json
index 97abfc6..aab5772 100644
--- a/angular.json
+++ b/angular.json
@@ -29,7 +29,10 @@
"styles": [
"src/styles.css"
],
- "scripts": []
+ "scripts": ["src/repro.js"],
+ "define": {
+ "REPRO": "true"
+ }
},
"configurations": {
"production": {
@@ -45,7 +48,10 @@
"maximumError": "8kB"
}
],
- "outputHashing": "all"
+ "outputHashing": "all",
+ "define": {
+ "REPRO": "false"
+ }
},
"development": {
"optimization": false,
diff --git a/src/main.ts b/src/main.ts
index 35b00f3..8e77aac 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -2,5 +2,10 @@ import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
+// @ts-expect-error
+if (REPRO) {
+ console.log('MAIN_REPRO');
+}
+
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));
diff --git a/src/repro.js b/src/repro.js
new file mode 100644
index 0000000..1062bc6
--- /dev/null
+++ b/src/repro.js
@@ -0,0 +1,3 @@
+if (REPRO) {
+ console.log('SCRIPT_REPRO');
+}
Exception or Error
Your Environment
$ ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 19.0.2
Node: 18.20.5
Package Manager: npm 10.8.2
OS: linux x64
Angular: 19.0.1
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1900.2
@angular-devkit/build-angular 19.0.2
@angular-devkit/core 19.0.2
@angular-devkit/schematics 19.0.2
@angular/cli 19.0.2
@schematics/angular 19.0.2
rxjs 7.8.1
typescript 5.6.3
zone.js 0.15.0
Anything else relevant?
No response
Metadata
Metadata
Assignees
Labels
angular/build:applicationarea: @angular/buildfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix