Skip to content

Commit e7ec034

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): avoid attempting to optimize copied JavaScript assets
When in watch mode (`ng serve`/`ng build --watch`), Webpack's `copy-webpack-plugin` is currently used to implement the project `assets` option within `angular.json`. Files specified by the `assets` option are intended to be copied to the output unmodified and in their original form. However, if any JavaScript assets were present they previously would have been unintentionally subject to their respective optimization plugins which would result in modified asset outputs and the potential for breakage of the assets. `ng build` outside of watch mode uses a direct file copy with copy-on-write (where supported) to process assets and is therefore not affected by this situation. When the `copy-webpack-plugin` is used, it adds a `copied` flag to an asset's info metadata. This flag is now used to exclude all such copied JavaScript assets from optimization. (cherry picked from commit f5d019f)
1 parent 8773854 commit e7ec034

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts

+27-25
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,36 @@ export class JavaScriptOptimizerPlugin {
9696
}
9797

9898
const scriptAsset = compilation.getAsset(assetName);
99+
// Skip assets that have already been optimized or are verbatim copies (project assets)
100+
if (!scriptAsset || scriptAsset.info.minimized || scriptAsset.info.copied) {
101+
continue;
102+
}
99103

100-
if (scriptAsset && !scriptAsset.info.minimized) {
101-
const { source: scriptAssetSource, name } = scriptAsset;
102-
let cacheItem;
103-
104-
if (cache) {
105-
const eTag = cache.getLazyHashedEtag(scriptAssetSource);
106-
cacheItem = cache.getItemCache(name, eTag);
107-
const cachedOutput = await cacheItem.getPromise<
108-
{ source: sources.Source } | undefined
109-
>();
110-
111-
if (cachedOutput) {
112-
compilation.updateAsset(name, cachedOutput.source, {
113-
minimized: true,
114-
});
115-
continue;
116-
}
104+
const { source: scriptAssetSource, name } = scriptAsset;
105+
let cacheItem;
106+
107+
if (cache) {
108+
const eTag = cache.getLazyHashedEtag(scriptAssetSource);
109+
cacheItem = cache.getItemCache(name, eTag);
110+
const cachedOutput = await cacheItem.getPromise<
111+
{ source: sources.Source } | undefined
112+
>();
113+
114+
if (cachedOutput) {
115+
compilation.updateAsset(name, cachedOutput.source, {
116+
minimized: true,
117+
});
118+
continue;
117119
}
118-
119-
const { source, map } = scriptAssetSource.sourceAndMap();
120-
scriptsToOptimize.push({
121-
name: scriptAsset.name,
122-
code: typeof source === 'string' ? source : source.toString(),
123-
map,
124-
cacheItem,
125-
});
126120
}
121+
122+
const { source, map } = scriptAssetSource.sourceAndMap();
123+
scriptsToOptimize.push({
124+
name: scriptAsset.name,
125+
code: typeof source === 'string' ? source : source.toString(),
126+
map,
127+
cacheItem,
128+
});
127129
}
128130

129131
if (scriptsToOptimize.length === 0) {

0 commit comments

Comments
 (0)