Skip to content

Commit

Permalink
Merge pull request #11 from pierredup/custom-loaders
Browse files Browse the repository at this point in the history
Allow to add custom loaders
  • Loading branch information
weaverryan committed Jun 16, 2017
2 parents 964fce4 + d6a1bc0 commit 1a3300e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
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);
}

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

0 comments on commit 1a3300e

Please sign in to comment.