Skip to content

Commit facd9ff

Browse files
committed
feature #130 Add options callback to Encore.enablePostCssLoader() (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Add options callback to Encore.enablePostCssLoader() This PR allows to add a callback as the first argument of `Encore.enablePostCssLoader()` in order to be able to modify the options of the `postcss-loader`. As an example (fixes #129): ```js Encore.enablePostCssLoader((options) => { options.config = { path: 'app/config/postcss.config.js' }; }); ``` Commits ------- 0999277 Add options callback to Encore.enablePostCssLoader()
2 parents ad306b9 + 0999277 commit facd9ff

File tree

5 files changed

+82
-14
lines changed

5 files changed

+82
-14
lines changed

index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,20 @@ module.exports = {
275275
*
276276
* https://github.com/postcss/postcss-loader
277277
*
278+
* Encore.enablePostCssLoader();
279+
*
280+
* Or pass options to the loader
281+
*
282+
* Encore.enablePostCssLoader(function(options) {
283+
* // https://github.com/postcss/postcss-loader#options
284+
* // options.config = {...}
285+
* })
286+
*
287+
* @param {function} postCssLoaderOptionsCallback
278288
* @return {exports}
279289
*/
280-
enablePostCssLoader() {
281-
webpackConfig.enablePostCssLoader();
290+
enablePostCssLoader(postCssLoaderOptionsCallback = () => {}) {
291+
webpackConfig.enablePostCssLoader(postCssLoaderOptionsCallback);
282292

283293
return this;
284294
},

lib/WebpackConfig.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WebpackConfig {
4040
this.useVersioning = false;
4141
this.useSourceMaps = false;
4242
this.usePostCssLoader = false;
43+
this.postCssLoaderOptionsCallback = function() {};
4344
this.useSassLoader = false;
4445
this.sassLoaderOptionsCallback = function() {};
4546
this.sassOptions = {
@@ -198,8 +199,14 @@ class WebpackConfig {
198199
this.addEntry(name, files);
199200
}
200201

201-
enablePostCssLoader() {
202+
enablePostCssLoader(postCssLoaderOptionsCallback = () => {}) {
202203
this.usePostCssLoader = true;
204+
205+
if (typeof postCssLoaderOptionsCallback !== 'function') {
206+
throw new Error('Argument 1 to enablePostCssLoader() must be a callback function.');
207+
}
208+
209+
this.postCssLoaderOptionsCallback = postCssLoaderOptionsCallback;
203210
}
204211

205212
enableSassLoader(sassLoaderOptionsCallback = () => {}, options = {}) {

lib/loaders/css.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,20 @@ module.exports = {
3838
if (usePostCssLoader) {
3939
loaderFeatures.ensureLoaderPackagesExist('postcss');
4040

41+
const postCssLoaderOptions = {
42+
sourceMap: webpackConfig.useSourceMaps
43+
};
44+
45+
// allow options to be configured
46+
webpackConfig.postCssLoaderOptionsCallback.apply(
47+
// use config as the this variable
48+
postCssLoaderOptions,
49+
[postCssLoaderOptions]
50+
);
51+
4152
cssLoaders.push({
4253
loader: 'postcss-loader',
43-
options: {
44-
sourceMap: webpackConfig.useSourceMaps
45-
}
54+
options: postCssLoaderOptions
4655
});
4756
}
4857

test/WebpackConfig.js

+24
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,30 @@ describe('WebpackConfig object', () => {
282282
});
283283
});
284284

285+
describe('enablePostCssLoader', () => {
286+
it('Call with no config', () => {
287+
const config = createConfig();
288+
config.enablePostCssLoader();
289+
290+
expect(config.usePostCssLoader).to.be.true;
291+
});
292+
293+
it('Pass options callback', () => {
294+
const config = createConfig();
295+
const callback = () => {};
296+
config.enablePostCssLoader(callback);
297+
298+
expect(config.usePostCssLoader).to.be.true;
299+
expect(config.postCssLoaderOptionsCallback).to.equal(callback);
300+
});
301+
302+
it('Pass invalid options callback', () => {
303+
const config = createConfig();
304+
305+
expect(() => config.enablePostCssLoader('FOO')).to.throw('must be a callback function');
306+
});
307+
});
308+
285309
describe('enableSassLoader', () => {
286310
it('Call with no config', () => {
287311
const config = createConfig();

test/loaders/css.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@ describe('loaders/css', () => {
4444
expect(actualLoaders[0].options.minimize).to.be.true;
4545
});
4646

47-
it('getLoaders() with PostCSS', () => {
48-
const config = createConfig();
49-
config.enableSourceMaps();
50-
config.enablePostCssLoader();
47+
describe('getLoaders() with PostCSS', () => {
48+
it('without options callback', () => {
49+
const config = createConfig();
50+
config.enableSourceMaps();
51+
config.enablePostCssLoader();
5152

52-
const actualLoaders = cssLoader.getLoaders(config);
53-
// css-loader & postcss-loader
54-
expect(actualLoaders).to.have.lengthOf(2);
55-
expect(actualLoaders[1].options.sourceMap).to.be.true;
53+
const actualLoaders = cssLoader.getLoaders(config);
54+
// css-loader & postcss-loader
55+
expect(actualLoaders).to.have.lengthOf(2);
56+
expect(actualLoaders[1].options.sourceMap).to.be.true;
57+
});
58+
59+
it('with options callback', () => {
60+
const config = createConfig();
61+
config.enableSourceMaps();
62+
config.enablePostCssLoader((options) => {
63+
options.config = {
64+
path: 'config/postcss.config.js'
65+
};
66+
});
67+
68+
const actualLoaders = cssLoader.getLoaders(config);
69+
// css-loader & postcss-loader
70+
expect(actualLoaders).to.have.lengthOf(2);
71+
expect(actualLoaders[1].options.sourceMap).to.be.true;
72+
expect(actualLoaders[1].options.config.path).to.equal('config/postcss.config.js');
73+
});
5674
});
5775
});

0 commit comments

Comments
 (0)