Skip to content

Commit b21f7aa

Browse files
committed
Allow to add custom loaders
1 parent ba34a7f commit b21f7aa

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

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 = new Set();
5253
}
5354

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

161+
addLoader(test, use, options = { include: null, exclude: null }) {
162+
this.loaders.add({ 'test': test, 'use': use, 'include': options.include || null, 'exclude': options.exclude || null });
163+
}
164+
160165
enableVersioning(enabled = true) {
161166
this.useVersioning = enabled;
162167
}

lib/config-generator.js

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

238+
if (this.webpackConfig.loaders.size > 0) {
239+
this.webpackConfig.loaders.forEach((loader) => {
240+
rules.push(loader);
241+
});
242+
}
243+
238244
return rules;
239245
}
240246

test/WebpackConfig.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,22 @@ 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 with default options', () => {
282+
const config = createConfig();
283+
284+
config.addLoader(/\.custom$/, 'custom-loader');
285+
286+
expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }]);
287+
});
288+
289+
it('Adds a custom exclude path', () => {
290+
const config = createConfig();
291+
292+
config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' });
293+
294+
expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' }]);
295+
});
296+
});
279297
});

test/config-generator.js

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

351+
describe('addLoader() adds custom rules', () => {
352+
it('addLoader()', () => {
353+
const config = createConfig();
354+
config.outputPath = '/tmp/output/public-path';
355+
config.publicPath = '/public-path';
356+
config.addLoader(/\.custom$/, 'custom-loader');
357+
358+
const actualConfig = configGenerator(config);
359+
360+
expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null });
361+
});
362+
363+
it('addLoader() with custom exlude path', () => {
364+
const config = createConfig();
365+
config.outputPath = '/tmp/output/public-path';
366+
config.publicPath = '/public-path';
367+
config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' });
368+
369+
const actualConfig = configGenerator(config);
370+
371+
expect(actualConfig.module.rules).to.deep.include({ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': 'node_modules' });
372+
});
373+
});
374+
351375
describe('.js rule receives different configuration', () => {
352376
it('Use default config', () => {
353377
const config = createConfig();

0 commit comments

Comments
 (0)