Skip to content

Add options callback to Encore.enableLessLoader() #134

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

Merged
merged 1 commit into from
Aug 9, 2017
Merged
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
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,21 @@ module.exports = {
/**
* Call this if you plan on loading less files.
*
* Encore.enableLessLoader();
*
* Or pass options to the loader
*
* Encore.enableLessLoader(function(options) {
* // https://github.com/webpack-contrib/less-loader#examples
* // http://lesscss.org/usage/#command-line-usage-options
* // options.relativeUrls = false;
* });
*
* @param {function} lessLoaderOptionsCallback
* @return {exports}
*/
enableLessLoader() {
webpackConfig.enableLessLoader();
enableLessLoader(lessLoaderOptionsCallback = () => {}) {
webpackConfig.enableLessLoader(lessLoaderOptionsCallback);

return this;
},
Expand Down
9 changes: 8 additions & 1 deletion lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class WebpackConfig {
resolve_url_loader: true
};
this.useLessLoader = false;
this.lessLoaderOptionsCallback = function() {};
this.cleanupOutput = false;
this.sharedCommonsEntryName = null;
this.providedVariables = {};
Expand Down Expand Up @@ -231,8 +232,14 @@ class WebpackConfig {
}
}

enableLessLoader() {
enableLessLoader(lessLoaderOptionsCallback = () => {}) {
this.useLessLoader = true;

if (typeof lessLoaderOptionsCallback !== 'function') {
throw new Error('Argument 1 to enableLessLoader() must be a callback function.');
}

this.lessLoaderOptionsCallback = lessLoaderOptionsCallback;
}

enableReactPreset() {
Expand Down
15 changes: 12 additions & 3 deletions lib/loaders/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ module.exports = {
getLoaders(webpackConfig, ignorePostCssLoader = false) {
loaderFeatures.ensureLoaderPackagesExist('less');

const config = {
sourceMap: webpackConfig.useSourceMaps
};

// allow options to be configured
webpackConfig.lessLoaderOptionsCallback.apply(
// use config as the this variable
config,
[config]
);

return [
...cssLoader.getLoaders(webpackConfig, ignorePostCssLoader),
{
loader: 'less-loader',
options: {
sourceMap: webpackConfig.useSourceMaps
}
options: config
},
];
}
Expand Down
25 changes: 25 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,31 @@ describe('WebpackConfig object', () => {
});
});

describe('enableLessLoader', () => {
it('Calling method sets it', () => {
const config = createConfig();
config.enableLessLoader();

expect(config.useLessLoader).to.be.true;
});

it('Calling with callback', () => {
const config = createConfig();
const callback = (lessOptions) => {};
config.enableLessLoader(callback);

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

it('Calling with non-callback throws an error', () => {
const config = createConfig();

expect(() => {
config.enableLessLoader('FOO');
}).to.throw('must be a callback function');
});
});

describe('enableTypeScriptLoader', () => {
it('Calling method sets it', () => {
const config = createConfig();
Expand Down
22 changes: 22 additions & 0 deletions test/loaders/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,26 @@ describe('loaders/less', () => {

cssLoader.getLoaders.restore();
});

it('getLoaders() with options callback', () => {
const config = createConfig();
config.enableSourceMaps(true);

// make the cssLoader return nothing
sinon.stub(cssLoader, 'getLoaders')
.callsFake(() => []);

config.enableLessLoader(function(lessOptions) {
lessOptions.custom_option = 'foo';
lessOptions.other_option = true;
});

const actualLoaders = lessLoader.getLoaders(config);
expect(actualLoaders[0].options).to.deep.equals({
sourceMap: true,
custom_option: 'foo',
other_option: true
});
cssLoader.getLoaders.restore();
});
});