Skip to content

Commit 1a3300e

Browse files
authored
Merge pull request #11 from pierredup/custom-loaders
Allow to add custom loaders
2 parents 964fce4 + d6a1bc0 commit 1a3300e

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* This file is part of the Symfony package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
110
'use strict';
211

312
const WebpackConfig = require('./lib/WebpackConfig');
@@ -126,6 +135,32 @@ module.exports = {
126135
return this;
127136
},
128137

138+
/**
139+
* Adds a custom loader config
140+
*
141+
* @param {object} loader The loader config object
142+
*
143+
* @returns {exports}
144+
*/
145+
addLoader(loader) {
146+
webpackConfig.addLoader(loader);
147+
148+
return this;
149+
},
150+
151+
/**
152+
* Alias to addLoader
153+
*
154+
* @param {object} rule
155+
*
156+
* @returns {exports}
157+
*/
158+
addRule(rule) {
159+
this.addLoader(rule);
160+
161+
return this;
162+
},
163+
129164
/**
130165
* When enabled, files are rendered with a hash based
131166
* on their contents (e.g. main.a2b61cc.js)

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class WebpackConfig {
4949
this.providedVariables = {};
5050
this.babelConfigurationCallback = function() {};
5151
this.useReact = false;
52+
this.loaders = [];
5253
}
5354

5455
getContext() {
@@ -157,6 +158,10 @@ class WebpackConfig {
157158
this.styleEntries.set(name, src);
158159
}
159160

161+
addLoader(loader) {
162+
this.loaders.push(loader);
163+
}
164+
160165
enableVersioning(enabled = true) {
161166
this.useVersioning = enabled;
162167
}

lib/config-generator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ class ConfigGenerator {
235235
});
236236
}
237237

238+
this.webpackConfig.loaders.forEach((loader) => {
239+
rules.push(loader);
240+
});
241+
238242
return rules;
239243
}
240244

test/WebpackConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,14 @@ describe('WebpackConfig object', () => {
276276
}).to.throw('Invalid option "fake_option" passed to enableSassLoader()');
277277
});
278278
});
279+
280+
describe('addLoader', () => {
281+
it('Adds a new loader', () => {
282+
const config = createConfig();
283+
284+
config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' });
285+
286+
expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
287+
});
288+
});
279289
});

test/config-generator.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,19 @@ describe('The config-generator function', () => {
348348
});
349349
});
350350

351+
describe('addLoader() adds a custom loader', () => {
352+
it('addLoader()', () => {
353+
const config = createConfig();
354+
config.outputPath = '/tmp/output/public-path';
355+
config.publicPath = '/public-path';
356+
config.addLoader({ 'test': /\.custom$/, 'loader': 'custom-loader' });
357+
358+
const actualConfig = configGenerator(config);
359+
360+
expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'loader': 'custom-loader' });
361+
});
362+
});
363+
351364
describe('.js rule receives different configuration', () => {
352365
it('Use default config', () => {
353366
const config = createConfig();

0 commit comments

Comments
 (0)