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 1 commit
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
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 = new Set();
Copy link
Member

Choose a reason for hiding this comment

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

This does not need to be a Set. It could just be an array.

You create a new object inside addLoader, so the uniqueness check of the Set will never exclude anything anyway. You're just using it as an array here

}

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

addLoader(test, use, options = { include: null, exclude: null }) {
this.loaders.add({ 'test': test, 'use': use, 'include': options.include || null, 'exclude': options.exclude || null });
}

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
6 changes: 6 additions & 0 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class ConfigGenerator {
});
}

if (this.webpackConfig.loaders.size > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

no need for this check. .forEach works fine on empty arrays too

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

return rules;
}

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

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

config.addLoader(/\.custom$/, 'custom-loader');

expect(Array.from(config.loaders)).to.deep.equals([{ 'test': /\.custom$/, 'use': 'custom-loader', 'include': null, 'exclude': null }]);
});

it('Adds a custom exclude path', () => {
const config = createConfig();

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

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

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

const actualConfig = configGenerator(config);

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

it('addLoader() with custom exlude path', () => {
const config = createConfig();
config.outputPath = '/tmp/output/public-path';
config.publicPath = '/public-path';
config.addLoader(/\.custom$/, 'custom-loader', { 'exclude': 'node_modules' });

const actualConfig = configGenerator(config);

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

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