Skip to content

Commit b3f71c5

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@ngtools/webpack): JIT mode CommonJS accessing inexistent default property
With this change we fix an issue in JIT mode were we try to access an inexistent `default` property of styles modules when emitted in CJS. Also, we now emit templates modules without the default property when not targeting ES2015 or greater to normalize the how to `require` templates and styles. Closes #21588
1 parent 4c288b8 commit b3f71c5

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { execute } from '../../index';
10+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
13+
describe('Behavior: "module commonjs"', () => {
14+
it('should work when module is commonjs', async () => {
15+
harness.useTarget('test', {
16+
...BASE_OPTIONS,
17+
});
18+
19+
await harness.modifyFile('src/tsconfig.spec.json', (content) => {
20+
const tsConfig = JSON.parse(content);
21+
tsConfig.compilerOptions.module = 'commonjs';
22+
23+
return JSON.stringify(tsConfig);
24+
});
25+
26+
const { result } = await harness.executeOnce();
27+
28+
expect(result?.success).toBeTrue();
29+
});
30+
});
31+
});

packages/ngtools/webpack/src/loaders/direct-resource.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { LoaderContext } from 'webpack';
10+
911
export const DirectAngularResourceLoaderPath = __filename;
1012

11-
export default function (content: string) {
12-
return `export default ${JSON.stringify(content)};`;
13+
export default function (this: LoaderContext<{ esModule?: 'true' | 'false' }>, content: string) {
14+
const { esModule } = this.getOptions();
15+
16+
return `${esModule === 'false' ? 'module.exports =' : 'export default'} ${JSON.stringify(
17+
content,
18+
)};`;
1319
}

packages/ngtools/webpack/src/transformers/replace_resources.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function visitComponentMetadata(
146146
styleReplacements: ts.Expression[],
147147
directTemplateLoading: boolean,
148148
resourceImportDeclarations: ts.ImportDeclaration[],
149-
moduleKind?: ts.ModuleKind,
149+
moduleKind: ts.ModuleKind = ts.ModuleKind.ES2015,
150150
inlineStyleFileExtension?: string,
151151
): ts.ObjectLiteralElementLike | undefined {
152152
if (!ts.isPropertyAssignment(node) || ts.isComputedPropertyName(node.name)) {
@@ -159,9 +159,10 @@ function visitComponentMetadata(
159159
return undefined;
160160

161161
case 'templateUrl':
162+
const loaderOptions = moduleKind < ts.ModuleKind.ES2015 ? '?esModule=false' : '';
162163
const url = getResourceUrl(
163164
node.initializer,
164-
directTemplateLoading ? `!${DirectAngularResourceLoaderPath}!` : '',
165+
directTemplateLoading ? `!${DirectAngularResourceLoaderPath}${loaderOptions}!` : '',
165166
);
166167
if (!url) {
167168
return node;
@@ -255,14 +256,15 @@ function createResourceImport(
255256
nodeFactory: ts.NodeFactory,
256257
url: string,
257258
resourceImportDeclarations: ts.ImportDeclaration[],
258-
moduleKind = ts.ModuleKind.ES2015,
259+
moduleKind: ts.ModuleKind,
259260
): ts.Identifier | ts.Expression {
260261
const urlLiteral = nodeFactory.createStringLiteral(url);
261262

262263
if (moduleKind < ts.ModuleKind.ES2015) {
263-
return nodeFactory.createPropertyAccessExpression(
264-
nodeFactory.createCallExpression(nodeFactory.createIdentifier('require'), [], [urlLiteral]),
265-
'default',
264+
return nodeFactory.createCallExpression(
265+
nodeFactory.createIdentifier('require'),
266+
[],
267+
[urlLiteral],
266268
);
267269
} else {
268270
const importName = nodeFactory.createIdentifier(

packages/ngtools/webpack/src/transformers/replace_resources_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ describe('@ngtools/webpack transformers', () => {
102102
AppComponent = (0, tslib_1.__decorate)([
103103
(0, core_1.Component)({
104104
selector: 'app-root',
105-
template: require("!${DirectAngularResourceLoaderPath}!./app.component.html").default,
106-
styles: [require("./app.component.css").default, require("./app.component.2.css").default] }) ], AppComponent);
105+
template: require("!${DirectAngularResourceLoaderPath}?esModule=false!./app.component.html"),
106+
styles: [require("./app.component.css"), require("./app.component.2.css")] }) ], AppComponent);
107107
exports.AppComponent = AppComponent;
108108
`;
109109

0 commit comments

Comments
 (0)