UNPKG

2.46 kBJavaScriptView Raw
1// Remove .js files from entry points consisting entirely of .css|scss|sass|less|styl.
2// To be used together with ExtractTextPlugin.
3"use strict";
4Object.defineProperty(exports, "__esModule", { value: true });
5class SuppressExtractedTextChunksWebpackPlugin {
6 constructor() { }
7 apply(compiler) {
8 compiler.plugin('compilation', function (compilation) {
9 // find which chunks have css only entry points
10 const cssOnlyChunks = [];
11 const entryPoints = compilation.options.entry;
12 // determine which entry points are composed entirely of css files
13 for (let entryPoint of Object.keys(entryPoints)) {
14 if (entryPoints[entryPoint].every((el) => el.match(/\.(css|scss|sass|less|styl)$/))) {
15 cssOnlyChunks.push(entryPoint);
16 }
17 }
18 // Remove the js file for supressed chunks
19 compilation.plugin('after-seal', (callback) => {
20 compilation.chunks
21 .filter((chunk) => cssOnlyChunks.indexOf(chunk.name) !== -1)
22 .forEach((chunk) => {
23 let newFiles = [];
24 chunk.files.forEach((file) => {
25 if (file.match(/\.js(\.map)?$/)) {
26 // remove js files
27 delete compilation.assets[file];
28 }
29 else {
30 newFiles.push(file);
31 }
32 });
33 chunk.files = newFiles;
34 });
35 callback();
36 });
37 // Remove scripts tags with a css file as source, because HtmlWebpackPlugin will use
38 // a css file as a script for chunks without js files.
39 compilation.plugin('html-webpack-plugin-alter-asset-tags', (htmlPluginData, callback) => {
40 const filterFn = (tag) => !(tag.tagName === 'script' && tag.attributes.src.match(/\.css$/));
41 htmlPluginData.head = htmlPluginData.head.filter(filterFn);
42 htmlPluginData.body = htmlPluginData.body.filter(filterFn);
43 callback(null, htmlPluginData);
44 });
45 });
46 }
47}
48exports.SuppressExtractedTextChunksWebpackPlugin = SuppressExtractedTextChunksWebpackPlugin;
49//# sourceMappingURL=/users/hansl/sources/angular-cli/plugins/suppress-entry-chunks-webpack-plugin.js.map
\No newline at end of file