Skip to content

Commit ec0d1bf

Browse files
JoostKAndrewKushnir
authored andcommitted
fix(compiler-cli): insert constant statements after the first group of imports (#56431)
The linker inserts the constant statements that are needed to support compiled templates after the import statements of an ESM file, but it failed to account for import statements that are not at the top of the file. This is typically seen in FESM files where multiple individual ESMs have been concatenated into a single ESM file, with imports in various places. The linker would then find the very last import statement to insert the constant statements after, but this may result in TDZ errors for component templates that have been emitted earlier in the file. This commit updates the Babel linker plugin to insert constant statements after the last import of the first import group, therefore avoiding the TDZ error. Fixes #56403 PR Close #56431
1 parent 571d06b commit ec0d1bf

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎packages/compiler-cli/linker/babel/src/es2015_linker_plugin.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ function insertIntoFunction(
134134
*/
135135
function insertIntoProgram(program: NodePath<t.Program>, statements: t.Statement[]): void {
136136
const body = program.get('body');
137-
const importStatements = body.filter((statement) => statement.isImportDeclaration());
138-
if (importStatements.length === 0) {
137+
const insertBeforeIndex = body.findIndex((statement) => !statement.isImportDeclaration());
138+
139+
if (insertBeforeIndex === -1) {
139140
program.unshiftContainer('body', statements);
140141
} else {
141-
importStatements[importStatements.length - 1].insertAfter(statements);
142+
body[insertBeforeIndex].insertBefore(statements);
142143
}
143144
}
144145

‎packages/compiler-cli/linker/babel/test/es2015_linker_plugin_spec.ts‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('createEs2015LinkerPlugin()', () => {
6969
);
7070
const fileSystem = new MockFileSystemNative();
7171
const logger = new MockLogger();
72-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
7372

7473
babel.transformSync(
7574
[
@@ -115,7 +114,6 @@ describe('createEs2015LinkerPlugin()', () => {
115114
);
116115
const fileSystem = new MockFileSystemNative();
117116
const logger = new MockLogger();
118-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
119117
const result = babel.transformSync(
120118
[
121119
'var core;',
@@ -138,7 +136,6 @@ describe('createEs2015LinkerPlugin()', () => {
138136
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
139137
const fileSystem = new MockFileSystemNative();
140138
const logger = new MockLogger();
141-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
142139
const result = babel.transformSync(
143140
[
144141
"import * as core from 'some-module';",
@@ -159,11 +156,35 @@ describe('createEs2015LinkerPlugin()', () => {
159156
);
160157
});
161158

159+
it('should return a Babel plugin that adds shared statements after the first group of imports', () => {
160+
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
161+
const fileSystem = new MockFileSystemNative();
162+
const logger = new MockLogger();
163+
const result = babel.transformSync(
164+
[
165+
"import * as core from 'some-module';",
166+
"import {id} from 'other-module';",
167+
`ɵɵngDeclareDirective({minVersion: '0.0.0-PLACEHOLDER', version: '0.0.0-PLACEHOLDER', ngImport: core})`,
168+
`ɵɵngDeclareDirective({minVersion: '0.0.0-PLACEHOLDER', version: '0.0.0-PLACEHOLDER', ngImport: core})`,
169+
`ɵɵngDeclareDirective({minVersion: '0.0.0-PLACEHOLDER', version: '0.0.0-PLACEHOLDER', ngImport: core})`,
170+
"import {second} from 'second-module';",
171+
].join('\n'),
172+
{
173+
plugins: [createEs2015LinkerPlugin({fileSystem, logger})],
174+
filename: '/test.js',
175+
parserOpts: {sourceType: 'unambiguous'},
176+
generatorOpts: {compact: true},
177+
},
178+
);
179+
expect(result!.code).toEqual(
180+
'import*as core from\'some-module\';import{id}from\'other-module\';const _c0=[1];const _c1=[2];const _c2=[3];"REPLACEMENT";"REPLACEMENT";"REPLACEMENT";import{second}from\'second-module\';',
181+
);
182+
});
183+
162184
it('should return a Babel plugin that adds shared statements at the start of the program if it is an ECMAScript Module and there are no imports', () => {
163185
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
164186
const fileSystem = new MockFileSystemNative();
165187
const logger = new MockLogger();
166-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
167188
const result = babel.transformSync(
168189
[
169190
'var core;',
@@ -188,7 +209,6 @@ describe('createEs2015LinkerPlugin()', () => {
188209
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
189210
const fileSystem = new MockFileSystemNative();
190211
const logger = new MockLogger();
191-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
192212
const result = babel.transformSync(
193213
[
194214
'function run(core) {',
@@ -213,7 +233,6 @@ describe('createEs2015LinkerPlugin()', () => {
213233
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
214234
const fileSystem = new MockFileSystemNative();
215235
const logger = new MockLogger();
216-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
217236
const result = babel.transformSync(
218237
[
219238
'function run() {',
@@ -244,7 +263,6 @@ describe('createEs2015LinkerPlugin()', () => {
244263
spyOnLinkPartialDeclarationWithConstants(o.fn([], [], null, null, 'FOO'));
245264
const fileSystem = new MockFileSystemNative();
246265
const logger = new MockLogger();
247-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
248266
const result = babel.transformSync(
249267
[
250268
`ɵɵngDeclareDirective({minVersion: '0.0.0-PLACEHOLDER', version: '0.0.0-PLACEHOLDER', ngImport: core}); FOO;`,
@@ -298,7 +316,6 @@ describe('createEs2015LinkerPlugin()', () => {
298316

299317
const fileSystem = new MockFileSystemNative();
300318
const logger = new MockLogger();
301-
const plugin = createEs2015LinkerPlugin({fileSystem, logger});
302319
const result = babel.transformSync(
303320
[
304321
"import * as core from 'some-module';",

0 commit comments

Comments
 (0)