Skip to content

updating css-loader dep and ts-loader dev dep #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,28 @@ class Encore {
return this;
}

/**
* Allows you to configure the options passed to the optimize-css-assets-webpack-plugin.
* A list of available options can be found at https://github.com/NMFR/optimize-css-assets-webpack-plugin
*
* For example:
*
* Encore.configureOptimizeCssPlugin((options) => {
* options.cssProcessor = require('cssnano');
* options.cssProcessorPluginOptions = {
* preset: ['default', { discardComments: { removeAll: true } }],
* }
* })
*
* @param {function} optimizeCssPluginOptionsCallback
* @returns {Encore}
*/
configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback = () => {}) {
webpackConfig.configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback);

return this;
}

/**
* Adds a JavaScript file that should be webpacked:
*
Expand Down
9 changes: 9 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class WebpackConfig {
this.loaderOptionsPluginOptionsCallback = () => {};
this.manifestPluginOptionsCallback = () => {};
this.terserPluginOptionsCallback = () => {};
this.optimizeCssPluginOptionsCallback = () => {};
this.notifierPluginOptionsCallback = () => {};
}

Expand Down Expand Up @@ -217,6 +218,14 @@ class WebpackConfig {
this.terserPluginOptionsCallback = terserPluginOptionsCallback;
}

configureOptimizeCssPlugin(optimizeCssPluginOptionsCallback = () => {}) {
if (typeof optimizeCssPluginOptionsCallback !== 'function') {
throw new Error('Argument 1 to configureOptimizeCssPlugin() must be a callback function');
}

this.optimizeCssPluginOptionsCallback = optimizeCssPluginOptionsCallback;
}

/**
* Returns the value that should be used as the publicPath,
* which can be overridden by enabling the webpackDevServer
Expand Down
4 changes: 3 additions & 1 deletion lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const variableProviderPluginUtil = require('./plugins/variable-provider');
const cleanPluginUtil = require('./plugins/clean');
const definePluginUtil = require('./plugins/define');
const terserPluginUtil = require('./plugins/terser');
const optimizeCssAssetsUtil = require('./plugins/optimize-css-assets');
const vuePluginUtil = require('./plugins/vue');
const friendlyErrorPluginUtil = require('./plugins/friendly-errors');
const assetOutputDisplay = require('./plugins/asset-output-display');
Expand Down Expand Up @@ -388,7 +389,8 @@ class ConfigGenerator {

if (this.webpackConfig.isProduction()) {
optimization.minimizer = [
terserPluginUtil(this.webpackConfig)
terserPluginUtil(this.webpackConfig),
optimizeCssAssetsUtil(this.webpackConfig)
];
} else {
// see versioning.js: this gives us friendly module names,
Expand Down
37 changes: 37 additions & 0 deletions lib/plugins/optimize-css-assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const applyOptionsCallback = require('../utils/apply-options-callback');

/**
* @param {WebpackConfig} webpackConfig
* @return {object}
*/
module.exports = function(webpackConfig) {
const optimizePluginOptions = {
// see: https://github.com/NMFR/optimize-css-assets-webpack-plugin/issues/53#issuecomment-400294569
// we always use annotations: true, which is the setting if you're
// outputting to a separate file because this plugin is only
// used in production, and, in production, we always use the
// source-map option (a separate file) in config-generator.
cssProcessorOptions: {
map: {
inline: false,
annotation: true,
}
}
};

return new OptimizeCSSAssetsPlugin(
applyOptionsCallback(webpackConfig.optimizeCssPluginOptionsCallback, optimizePluginOptions)
);
};
6 changes: 4 additions & 2 deletions lib/webpack/delete-unused-entries-js-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ DeleteUnusedEntriesJSPlugin.prototype.apply = function(compiler) {
let fileDeleteCount = 0;

// loop over the output files and find the 1 that ends in .js
chunk.files.forEach((filename) => {
// loop in reverse, to avoid issues as we remove items from chunk.files
for (let i = chunk.files.length - 1; i >= 0; --i) {
let filename = chunk.files[i];
if (/\.js(\.map)?(\?[^.]*)?$/.test(filename)) {
fileDeleteCount++;
// remove the output file
delete compilation.assets[filename];
// remove the file, so that it does not dump in the manifest
chunk.files.splice(chunk.files.indexOf(filename), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chunk.files.indexOf(filename) could be i instead

}
});
}

// sanity check: make sure 1 or 2 files were deleted
// if there's some edge case where more .js files
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
"babel-loader": "^8.0.0",
"chalk": "^2.4.1",
"clean-webpack-plugin": "^0.1.19",
"css-loader": "^0.28.10",
"css-loader": "^1.0.0",
"fast-levenshtein": "^2.0.6",
"file-loader": "^1.1.10",
"friendly-errors-webpack-plugin": "^1.7.0",
"fs-extra": "^2.0.0",
"loader-utils": "^1.1.0",
"lodash": ">=3.5 <5",
"mini-css-extract-plugin": ">=0.4.0 <0.4.3",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"pkg-up": "^1.0.0",
"pretty-error": "^2.1.1",
"resolve-url-loader": "^2.3.0",
Expand Down Expand Up @@ -82,7 +83,7 @@
"sinon": "^2.3.4",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"ts-loader": "^4.3.0",
"ts-loader": "^5.3.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in devDependencies, so it really just means that we test against this version - the user can install something lower, but they will get a warning. We could also set this to ^5.0.0, which would mean we would test against 5.0.0 in one our test suites. When the user needs to install this, they would be give the command yarn add ts-loader ^5.0.0, but that should install the latest version anyways.

"typescript": "^2.3.4",
"url-loader": "^1.0.1",
"vue": "^2.3.4",
Expand Down
18 changes: 18 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ describe('WebpackConfig object', () => {
});
});

describe('configureOptimizeCssPlugin', () => {
it('Setting callback', () => {
const config = createConfig();
const callback = () => {};
config.configureOptimizeCssPlugin(callback);

expect(config.optimizeCssPluginOptionsCallback).to.equal(callback);
});

it('Setting invalid callback argument', () => {
const config = createConfig();

expect(() => {
config.configureOptimizeCssPlugin('foo');
}).to.throw('Argument 1 to configureOptimizeCssPlugin() must be a callback function');
});
});

describe('addEntry', () => {
it('Calling with a duplicate name throws an error', () => {
const config = createConfig();
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@ describe('Public API', () => {

});

describe('configureOptimizeCssPlugin', () => {

it('should return the API object', () => {
const returnedValue = api.configureOptimizeCssPlugin(() => {});
expect(returnedValue).to.equal(api);
});

});

describe('Runtime environment proxy', () => {
beforeEach(() => {
api.clearRuntimeEnvironment();
Expand Down
Loading