Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

refactor(@angular-devkit/build-optimizer): optimize purify plugin #304

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions packages/angular_devkit/build_optimizer/src/purify/purify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { RawSource, ReplaceSource } from 'webpack-sources';
// This matches a comment left by the build-optimizer that contains pure import paths
const importCommentRegex = /\/\*\* PURE_IMPORTS_START (\S+) PURE_IMPORTS_END \*\//mg;

// Replacements are meant to be used with Webpack's ReplaceSource.
export interface Replacement {
start: number;
end: number;
// Insertion are meant to be used with Webpack's ReplaceSource.
export interface Insert {
pos: number;
content: string;
}

Expand All @@ -25,48 +24,33 @@ export function purifyReplacements(content: string) {
return [];
}

const replacements: Replacement[] = [];
const addReplacement = (start: number, length: number, content: string) =>
replacements.push({
start,
end: start + length - 1,
content,
});
const inserts: Insert[] = [];

/* Prefix safe imports with pure */
content.replace(
new RegExp(`(_(${pureImportMatches})__ = )(__webpack_require__\\(\\S+\\);)`, 'mg'),
(match, p1, _p2, p3, offset) => {
const newContent = `${p1}/*@__PURE__*/${p3}`;
addReplacement(offset, match.length, newContent);

return newContent;
},
const regex = new RegExp(
`(_(${pureImportMatches})__(_default)? = )(__webpack_require__(\\.\\w)?\\(\\S+\\);)`,
'mg',
);

/* Prefix default safe imports with pure */
content.replace(
new RegExp(
`(_(${pureImportMatches})___default = )(__webpack_require__\\.\\w\\(\\S+\\);)`, 'mg',
),
(match, p1, _p2, p3, offset) => {
const newContent = `${p1}/*@__PURE__*/${p3}`;
addReplacement(offset, match.length, newContent);

return newContent;
},
);
let match;
// tslint:disable-next-line:no-conditional-assignment
while (match = regex.exec(content)) {
inserts.push({
pos: match.index + match[1].length,
content: '/*@__PURE__*/',
});
}

return replacements;
return inserts;
}

export function purify(content: string) {
const rawSource = new RawSource(content);
const replaceSource = new ReplaceSource(rawSource, 'file.js');

const replacements = purifyReplacements(content);
replacements.forEach((replacement) => {
replaceSource.replace(replacement.start, replacement.end, replacement.content);
const inserts = purifyReplacements(content);
inserts.forEach((insert) => {
replaceSource.insert(insert.pos, insert.content);
});

return replaceSource.source();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export class PurifyPlugin {
chunk.files
.filter((fileName: string) => fileName.endsWith('.js'))
.forEach((fileName: string) => {
const replacements = purifyReplacements(compilation.assets[fileName].source());
const inserts = purifyReplacements(compilation.assets[fileName].source());

if (replacements.length > 0) {
if (inserts.length > 0) {
const replaceSource = new ReplaceSource(compilation.assets[fileName], fileName);
replacements.forEach((replacement) => {
replaceSource.replace(replacement.start, replacement.end, replacement.content);
inserts.forEach((insert) => {
replaceSource.insert(insert.pos, insert.content);
});
compilation.assets[fileName] = replaceSource;
}
Expand Down