Skip to content

Commit bef1b58

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-optimizer): fix relative pure imports
1 parent 5da7b91 commit bef1b58

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

packages/angular_devkit/build_optimizer/src/purify/purify.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export interface Insert {
1919

2020
export function purifyReplacements(content: string) {
2121

22-
const pureImportMatches = getMatches(content, importCommentRegex, 1).join('|');
22+
const pureImportMatches = getMatches(content, importCommentRegex, 1)
23+
// Remove dots at the start of matches.
24+
// Older versions of Purify added dots for relative imports.
25+
.map(match => match.replace(/^\.+/, ''))
26+
.join('|');
27+
2328
if (!pureImportMatches) {
2429
return [];
2530
}

packages/angular_devkit/build_optimizer/src/purify/purify_spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,32 @@ describe('purify', () => {
3131
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_Subject___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_4_rxjs_Subject__);
3232
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_zone_js___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_zone_js__);
3333
/** PURE_IMPORTS_START rxjs_Subject,zone_js PURE_IMPORTS_END */
34-
`;
34+
`;
3535
const output = tags.stripIndent`
3636
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_Subject__ = /*@__PURE__*/__webpack_require__("rlar");
3737
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4_rxjs_Subject___default = /*@__PURE__*/__webpack_require__.n(__WEBPACK_IMPORTED_MODULE_4_rxjs_Subject__);
3838
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_zone_js___default = /*@__PURE__*/__webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_zone_js__);
3939
/** PURE_IMPORTS_START rxjs_Subject,zone_js PURE_IMPORTS_END */
40+
`;
41+
42+
expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);
43+
});
44+
45+
// Older versions of Purify added dots for relative imports. We should be backwards compatible.
46+
it('finds old matches that started with dots', () => {
47+
const input = tags.stripIndent`
48+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__util_root__ = __webpack_require__(13);
49+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__util_toSubscriber__ = __webpack_require__(67);
50+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__symbol_observable__ = __webpack_require__(45);
51+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__util_pipe__ = __webpack_require__(71);
52+
/** PURE_IMPORTS_START ._util_root,._util_toSubscriber,.._symbol_observable,._util_pipe PURE_IMPORTS_END */
53+
`;
54+
const output = tags.stripIndent`
55+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__util_root__ = /*@__PURE__*/__webpack_require__(13);
56+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__util_toSubscriber__ = /*@__PURE__*/__webpack_require__(67);
57+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__symbol_observable__ = /*@__PURE__*/__webpack_require__(45);
58+
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__util_pipe__ = /*@__PURE__*/__webpack_require__(71);
59+
/** PURE_IMPORTS_START ._util_root,._util_toSubscriber,.._symbol_observable,._util_pipe PURE_IMPORTS_END */
4060
`;
4161

4262
expect(tags.oneLine`${purify(input)}`).toEqual(tags.oneLine`${output}`);

packages/angular_devkit/build_optimizer/src/transforms/prefix-functions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ export function findPureImports(parentNode: ts.Node): string[] {
101101
if (node.kind === ts.SyntaxKind.ImportDeclaration
102102
&& (node as ts.ImportDeclaration).importClause) {
103103

104-
// Save the path of the import transformed into snake case
104+
// Save the path of the import transformed into snake case and remove relative paths.
105105
const moduleSpecifier = (node as ts.ImportDeclaration).moduleSpecifier as ts.StringLiteral;
106-
pureImports.push(moduleSpecifier.text.replace(/[\/@\-]/g, '_'));
106+
const pureImport = moduleSpecifier.text
107+
.replace(/[\/@\-]/g, '_')
108+
.replace(/^\.+/, '');
109+
pureImports.push(pureImport);
107110
}
108111

109112
ts.forEachChild(node, cb);

packages/angular_devkit/build_optimizer/src/transforms/prefix-functions_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ describe('prefix-functions', () => {
2121
it('adds import list', () => {
2222
const input = tags.stripIndent`
2323
import { Injectable } from '@angular/core';
24+
import { Something } from './relative/pure_import';
2425
var foo = Injectable;
26+
var bar = Something;
2527
`;
2628
const output = tags.stripIndent`
27-
/** PURE_IMPORTS_START _angular_core PURE_IMPORTS_END */
29+
/** PURE_IMPORTS_START _angular_core,_relative_pure_import PURE_IMPORTS_END */
2830
${input}
2931
`;
3032

0 commit comments

Comments
 (0)