Skip to content
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

Allow to add custom loaders #11

Merged
merged 5 commits into from
Jun 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const WebpackConfig = require('./lib/WebpackConfig');
Expand Down Expand Up @@ -126,6 +135,32 @@ module.exports = {
return this;
},

/**
* Adds a custom loader config
*
* @param {object} loader The loader config object
*
* @returns {exports}
*/
addLoader(loader) {
webpackConfig.addLoader(loader);

return this;
},

/**
* Alias to addLoader
*
* @param {object} rule
*
* @returns {exports}
*/
addRule(rule) {
this.addLoader(rule);

return this;
},

/**
* When enabled, files are rendered with a hash based
* on their contents (e.g. main.a2b61cc.js)
Expand Down
5 changes: 5 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class WebpackConfig {
this.providedVariables = {};
this.babelConfigurationCallback = function() {};
this.useReact = false;
this.loaders = [];
}

getContext() {
Expand Down Expand Up @@ -157,6 +158,10 @@ class WebpackConfig {
this.styleEntries.set(name, src);
}

addLoader(loader) {
this.loaders.push(loader);
}

Copy link
Member

Choose a reason for hiding this comment

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

We need to expose this in index.js as well (that file/module holds the true public API). And I think we should (in that file) create addLoader() and also addRule() (which would just call addLoader())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, don't know how I missed this :) Functions added

enableVersioning(enabled = true) {
this.useVersioning = enabled;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ class ConfigGenerator {
});
}

this.webpackConfig.loaders.forEach((loader) => {
rules.push(loader);
});

return rules;
}

Expand Down
10 changes: 10 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,14 @@ describe('WebpackConfig object', () => {
}).to.throw('Invalid option "fake_option" passed to enableSassLoader()');
});
});

describe('addLoader', () => {
it('Adds a new loader', () => {
const config = createConfig();

config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' });

expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
});
});
});
13 changes: 13 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,19 @@ describe('The config-generator function', () => {
});
});

describe('addLoader() adds a custom loader', () => {
it('addLoader()', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' });

const actualConfig = configGenerator(config);

expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'loader': 'custom-loader' });
});
});

describe('.js rule receives different configuration', () => {
it('Use default config', () => {
const config = createConfig();
Expand Down