diff --git a/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/module-cjs_spec.ts b/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/module-cjs_spec.ts new file mode 100644 index 000000000000..fdd972cb169c --- /dev/null +++ b/packages/angular_devkit/build_angular/src/builders/karma/tests/behavior/module-cjs_spec.ts @@ -0,0 +1,31 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import { execute } from '../../index'; +import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeBuilder } from '../setup'; + +describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => { + describe('Behavior: "module commonjs"', () => { + it('should work when module is commonjs', async () => { + harness.useTarget('test', { + ...BASE_OPTIONS, + }); + + await harness.modifyFile('src/tsconfig.spec.json', (content) => { + const tsConfig = JSON.parse(content); + tsConfig.compilerOptions.module = 'commonjs'; + + return JSON.stringify(tsConfig); + }); + + const { result } = await harness.executeOnce(); + + expect(result?.success).toBeTrue(); + }); + }); +}); diff --git a/packages/ngtools/webpack/src/loaders/direct-resource.ts b/packages/ngtools/webpack/src/loaders/direct-resource.ts index b6f2a2cae111..5c5efda6c7c9 100644 --- a/packages/ngtools/webpack/src/loaders/direct-resource.ts +++ b/packages/ngtools/webpack/src/loaders/direct-resource.ts @@ -6,8 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ +import { LoaderContext } from 'webpack'; + export const DirectAngularResourceLoaderPath = __filename; -export default function (content: string) { - return `export default ${JSON.stringify(content)};`; +export default function (this: LoaderContext<{ esModule?: 'true' | 'false' }>, content: string) { + const { esModule } = this.getOptions(); + + return `${esModule === 'false' ? 'module.exports =' : 'export default'} ${JSON.stringify( + content, + )};`; } diff --git a/packages/ngtools/webpack/src/transformers/replace_resources.ts b/packages/ngtools/webpack/src/transformers/replace_resources.ts index 9eba2512148b..a4f47c78c805 100644 --- a/packages/ngtools/webpack/src/transformers/replace_resources.ts +++ b/packages/ngtools/webpack/src/transformers/replace_resources.ts @@ -146,7 +146,7 @@ function visitComponentMetadata( styleReplacements: ts.Expression[], directTemplateLoading: boolean, resourceImportDeclarations: ts.ImportDeclaration[], - moduleKind?: ts.ModuleKind, + moduleKind: ts.ModuleKind = ts.ModuleKind.ES2015, inlineStyleFileExtension?: string, ): ts.ObjectLiteralElementLike | undefined { if (!ts.isPropertyAssignment(node) || ts.isComputedPropertyName(node.name)) { @@ -159,9 +159,10 @@ function visitComponentMetadata( return undefined; case 'templateUrl': + const loaderOptions = moduleKind < ts.ModuleKind.ES2015 ? '?esModule=false' : ''; const url = getResourceUrl( node.initializer, - directTemplateLoading ? `!${DirectAngularResourceLoaderPath}!` : '', + directTemplateLoading ? `!${DirectAngularResourceLoaderPath}${loaderOptions}!` : '', ); if (!url) { return node; @@ -255,14 +256,15 @@ function createResourceImport( nodeFactory: ts.NodeFactory, url: string, resourceImportDeclarations: ts.ImportDeclaration[], - moduleKind = ts.ModuleKind.ES2015, + moduleKind: ts.ModuleKind, ): ts.Identifier | ts.Expression { const urlLiteral = nodeFactory.createStringLiteral(url); if (moduleKind < ts.ModuleKind.ES2015) { - return nodeFactory.createPropertyAccessExpression( - nodeFactory.createCallExpression(nodeFactory.createIdentifier('require'), [], [urlLiteral]), - 'default', + return nodeFactory.createCallExpression( + nodeFactory.createIdentifier('require'), + [], + [urlLiteral], ); } else { const importName = nodeFactory.createIdentifier( diff --git a/packages/ngtools/webpack/src/transformers/replace_resources_spec.ts b/packages/ngtools/webpack/src/transformers/replace_resources_spec.ts index 59d554c6748b..0f09e6f7283a 100644 --- a/packages/ngtools/webpack/src/transformers/replace_resources_spec.ts +++ b/packages/ngtools/webpack/src/transformers/replace_resources_spec.ts @@ -102,8 +102,8 @@ describe('@ngtools/webpack transformers', () => { AppComponent = (0, tslib_1.__decorate)([ (0, core_1.Component)({ selector: 'app-root', - template: require("!${DirectAngularResourceLoaderPath}!./app.component.html").default, - styles: [require("./app.component.css").default, require("./app.component.2.css").default] }) ], AppComponent); + template: require("!${DirectAngularResourceLoaderPath}?esModule=false!./app.component.html"), + styles: [require("./app.component.css"), require("./app.component.2.css")] }) ], AppComponent); exports.AppComponent = AppComponent; `;