Skip to content

Commit 019fe8c

Browse files
committed
bug #440 updating css-loader dep and ts-loader dev dep (weaverryan)
This PR was squashed before being merged into the master branch (closes #440). Discussion ---------- updating css-loader dep and ts-loader dev dep Fixes #438 and fixes #431 and I believe fixes #432 This PR is more interesting than I expected! The latest version of `css-loader` (1.0) does not have the `minimize` option. Even though (until now) the older version was locked on our `package.json`, it may have been possible for users to be using a new version (I believe that should not be possible, but node deps can do strange things). Commits ------- 8f7ac59 updating css-loader dep and ts-loader dev dep
2 parents 4a64ceb + 8f7ac59 commit 019fe8c

9 files changed

+711
-462
lines changed

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,28 @@ class Encore {
226226
return this;
227227
}
228228

229+
/**
230+
* Allows you to configure the options passed to the optimize-css-assets-webpack-plugin.
231+
* A list of available options can be found at https://github.com/NMFR/optimize-css-assets-webpack-plugin
232+
*
233+
* For example:
234+
*
235+
* Encore.configureOptimizeCssPlugin((options) => {
236+
* options.cssProcessor = require('cssnano');
237+
* options.cssProcessorPluginOptions = {
238+
* preset: ['default', { discardComments: { removeAll: true } }],
239+
* }
240+
* })
241+
*
242+
* @param {function} optimizeCssPluginOptionsCallback
243+
* @returns {Encore}
244+
*/
245+
configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback = () => {}) {
246+
webpackConfig.configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback);
247+
248+
return this;
249+
}
250+
229251
/**
230252
* Adds a JavaScript file that should be webpacked:
231253
*

lib/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class WebpackConfig {
110110
this.loaderOptionsPluginOptionsCallback = () => {};
111111
this.manifestPluginOptionsCallback = () => {};
112112
this.terserPluginOptionsCallback = () => {};
113+
this.optimizeCssPluginOptionsCallback = () => {};
113114
this.notifierPluginOptionsCallback = () => {};
114115
}
115116

@@ -217,6 +218,14 @@ class WebpackConfig {
217218
this.terserPluginOptionsCallback = terserPluginOptionsCallback;
218219
}
219220

221+
configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback = () => {}) {
222+
if (typeof optimizeCssPluginOptionsCallback !== 'function') {
223+
throw new Error('Argument 1 to configureOptimizeCssPlugin() must be a callback function');
224+
}
225+
226+
this.optimizeCssPluginOptionsCallback = optimizeCssPluginOptionsCallback;
227+
}
228+
220229
/**
221230
* Returns the value that should be used as the publicPath,
222231
* which can be overridden by enabling the webpackDevServer

lib/config-generator.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const variableProviderPluginUtil = require('./plugins/variable-provider');
3333
const cleanPluginUtil = require('./plugins/clean');
3434
const definePluginUtil = require('./plugins/define');
3535
const terserPluginUtil = require('./plugins/terser');
36+
const optimizeCssAssetsUtil = require('./plugins/optimize-css-assets');
3637
const vuePluginUtil = require('./plugins/vue');
3738
const friendlyErrorPluginUtil = require('./plugins/friendly-errors');
3839
const assetOutputDisplay = require('./plugins/asset-output-display');
@@ -388,7 +389,8 @@ class ConfigGenerator {
388389

389390
if (this.webpackConfig.isProduction()) {
390391
optimization.minimizer = [
391-
terserPluginUtil(this.webpackConfig)
392+
terserPluginUtil(this.webpackConfig),
393+
optimizeCssAssetsUtil(this.webpackConfig)
392394
];
393395
} else {
394396
// see versioning.js: this gives us friendly module names,

lib/plugins/optimize-css-assets.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
13+
const applyOptionsCallback = require('../utils/apply-options-callback');
14+
15+
/**
16+
* @param {WebpackConfig} webpackConfig
17+
* @return {object}
18+
*/
19+
module.exports = function(webpackConfig) {
20+
const optimizePluginOptions = {
21+
// see: https://github.com/NMFR/optimize-css-assets-webpack-plugin/issues/53#issuecomment-400294569
22+
// we always use annotations: true, which is the setting if you're
23+
// outputting to a separate file because this plugin is only
24+
// used in production, and, in production, we always use the
25+
// source-map option (a separate file) in config-generator.
26+
cssProcessorOptions: {
27+
map: {
28+
inline: false,
29+
annotation: true,
30+
}
31+
}
32+
};
33+
34+
return new OptimizeCSSAssetsPlugin(
35+
applyOptionsCallback(webpackConfig.optimizeCssPluginOptionsCallback, optimizePluginOptions)
36+
);
37+
};

lib/webpack/delete-unused-entries-js-plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ DeleteUnusedEntriesJSPlugin.prototype.apply = function(compiler) {
2222
let fileDeleteCount = 0;
2323

2424
// loop over the output files and find the 1 that ends in .js
25-
chunk.files.forEach((filename) => {
25+
// loop in reverse, to avoid issues as we remove items from chunk.files
26+
for (let i = chunk.files.length - 1; i >= 0; --i) {
27+
let filename = chunk.files[i];
2628
if (/\.js(\.map)?(\?[^.]*)?$/.test(filename)) {
2729
fileDeleteCount++;
2830
// remove the output file
2931
delete compilation.assets[filename];
3032
// remove the file, so that it does not dump in the manifest
3133
chunk.files.splice(chunk.files.indexOf(filename), 1);
3234
}
33-
});
35+
}
3436

3537
// sanity check: make sure 1 or 2 files were deleted
3638
// if there's some edge case where more .js files

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
"babel-loader": "^8.0.0",
3333
"chalk": "^2.4.1",
3434
"clean-webpack-plugin": "^0.1.19",
35-
"css-loader": "^0.28.10",
35+
"css-loader": "^1.0.0",
3636
"fast-levenshtein": "^2.0.6",
3737
"file-loader": "^1.1.10",
3838
"friendly-errors-webpack-plugin": "^1.7.0",
3939
"fs-extra": "^2.0.0",
4040
"loader-utils": "^1.1.0",
4141
"lodash": ">=3.5 <5",
4242
"mini-css-extract-plugin": ">=0.4.0 <0.4.3",
43+
"optimize-css-assets-webpack-plugin": "^5.0.1",
4344
"pkg-up": "^1.0.0",
4445
"pretty-error": "^2.1.1",
4546
"resolve-url-loader": "^2.3.0",
@@ -82,7 +83,7 @@
8283
"sinon": "^2.3.4",
8384
"stylus": "^0.54.5",
8485
"stylus-loader": "^3.0.2",
85-
"ts-loader": "^4.3.0",
86+
"ts-loader": "^5.3.0",
8687
"typescript": "^2.3.4",
8788
"url-loader": "^1.0.1",
8889
"vue": "^2.3.4",

test/WebpackConfig.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ describe('WebpackConfig object', () => {
299299
});
300300
});
301301

302+
describe('configureOptimizeCssPlugin', () => {
303+
it('Setting callback', () => {
304+
const config = createConfig();
305+
const callback = () => {};
306+
config.configureOptimizeCssPlugin(callback);
307+
308+
expect(config.optimizeCssPluginOptionsCallback).to.equal(callback);
309+
});
310+
311+
it('Setting invalid callback argument', () => {
312+
const config = createConfig();
313+
314+
expect(() => {
315+
config.configureOptimizeCssPlugin('foo');
316+
}).to.throw('Argument 1 to configureOptimizeCssPlugin() must be a callback function');
317+
});
318+
});
319+
302320
describe('addEntry', () => {
303321
it('Calling with a duplicate name throws an error', () => {
304322
const config = createConfig();

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ describe('Public API', () => {
405405

406406
});
407407

408+
describe('configureOptimizeCssPlugin', () => {
409+
410+
it('should return the API object', () => {
411+
const returnedValue = api.configureOptimizeCssPlugin(() => {});
412+
expect(returnedValue).to.equal(api);
413+
});
414+
415+
});
416+
408417
describe('Runtime environment proxy', () => {
409418
beforeEach(() => {
410419
api.clearRuntimeEnvironment();

0 commit comments

Comments
 (0)