-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(@angular-devkit/build-angular): use esbuild as a CSS optimizer f…
…or global styles Esbuild now support CSS sourcemaps which now makes it possible to be used to optimize global CSS. With this change we also reduce the amount of dependencies by removing `css-minimizer-webpack-plugin` which brings in a number of transitive depedencies which we no longer use.
- Loading branch information
1 parent
8954d11
commit cb7d156
Showing
8 changed files
with
124 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/angular_devkit/build_angular/src/webpack/plugins/css-optimizer-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @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 { Message, formatMessages, transform } from 'esbuild'; | ||
import type { Compilation, Compiler, sources } from 'webpack'; | ||
import { addWarning } from '../../utils/webpack-diagnostics'; | ||
/** | ||
* The name of the plugin provided to Webpack when tapping Webpack compiler hooks. | ||
*/ | ||
const PLUGIN_NAME = 'angular-css-optimizer'; | ||
|
||
/** | ||
* A Webpack plugin that provides CSS optimization capabilities. | ||
* | ||
* The plugin uses both `esbuild` to provide both fast and highly-optimized | ||
* code output. | ||
*/ | ||
export class CssOptimizerPlugin { | ||
constructor() {} | ||
|
||
apply(compiler: Compiler) { | ||
const { OriginalSource, SourceMapSource } = compiler.webpack.sources; | ||
|
||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
compilation.hooks.processAssets.tapPromise( | ||
{ | ||
name: PLUGIN_NAME, | ||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, | ||
}, | ||
async (compilationAssets) => { | ||
const cache = compilation.options.cache && compilation.getCache(PLUGIN_NAME); | ||
|
||
for (const assetName of Object.keys(compilationAssets)) { | ||
if (!/\.(?:css|scss|sass|less|styl)$/.test(assetName)) { | ||
continue; | ||
} | ||
|
||
const asset = compilation.getAsset(assetName); | ||
// Skip assets that have already been optimized or are verbatim copies (project assets) | ||
if (!asset || asset.info.minimized || asset.info.copied) { | ||
continue; | ||
} | ||
|
||
const { source: styleAssetSource, name } = asset; | ||
let cacheItem; | ||
|
||
if (cache) { | ||
const eTag = cache.getLazyHashedEtag(styleAssetSource); | ||
cacheItem = cache.getItemCache(name, eTag); | ||
const cachedOutput = await cacheItem.getPromise< | ||
{ source: sources.Source; warnings: Message[] } | undefined | ||
>(); | ||
|
||
if (cachedOutput) { | ||
await this.addWarnings(compilation, cachedOutput.warnings); | ||
compilation.updateAsset(name, cachedOutput.source, { | ||
minimized: true, | ||
}); | ||
continue; | ||
} | ||
} | ||
|
||
const { source, map: inputMap } = styleAssetSource.sourceAndMap(); | ||
let sourceMapLine; | ||
if (inputMap) { | ||
// esbuild will automatically remap the sourcemap if provided | ||
sourceMapLine = `\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from( | ||
JSON.stringify(inputMap), | ||
).toString('base64')} */`; | ||
} | ||
|
||
const input = typeof source === 'string' ? source : source.toString(); | ||
const { code, warnings, map } = await transform( | ||
sourceMapLine ? input + sourceMapLine : input, | ||
{ | ||
loader: 'css', | ||
legalComments: 'inline', | ||
minify: true, | ||
sourcemap: !!inputMap && 'external', | ||
sourcefile: asset.name, | ||
}, | ||
); | ||
|
||
await this.addWarnings(compilation, warnings); | ||
|
||
const optimizedAsset = map | ||
? new SourceMapSource(code, name, map) | ||
: new OriginalSource(code, name); | ||
compilation.updateAsset(name, optimizedAsset, { minimized: true }); | ||
|
||
await cacheItem?.storePromise({ | ||
source: optimizedAsset, | ||
warnings, | ||
}); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
private async addWarnings(compilation: Compilation, warnings: Message[]) { | ||
if (warnings.length > 0) { | ||
for (const warning of await formatMessages(warnings, { kind: 'warning' })) { | ||
addWarning(compilation, warning); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.