Skip to content

Commit 4f73d73

Browse files
committed
feature #103 Allow to disable the default image and font loaders (Lyrkan)
This PR was squashed before being merged into the master branch (closes #103). Discussion ---------- Allow to disable the default image and font loaders This PR adds a `disableAssetsLoaders` method to the API, allowing users to disable the default image and font loaders. Currently these two loaders are always added (with some parts of them being hardcoded) and can't really be overriden which can causes some issues (see #73). By allowing to disable this behavior, users would be able to replace them by their own loaders. About the new method: * **Why a `disableAssetsLoaders` and not an `enableAssetsLoaders` instead**: I still think that these two loaders should be added by default since almost every project will need them. Moreover, keeping them enabled will avoid breaking BC. * **Why a single method instead of one for the image loader and one for the fonts loader**: I may be wrong there, but I think that if an user disable one of them, it'll probably disable the other one aswell anyway. Commits ------- aeb4a95 Allow to disable the default image and font loaders
2 parents 86e197b + aeb4a95 commit 4f73d73

File tree

6 files changed

+142
-6
lines changed

6 files changed

+142
-6
lines changed

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,30 @@ module.exports = {
429429
return this;
430430
},
431431

432+
/**
433+
* Call this if you wish to disable the default
434+
* images loader.
435+
*
436+
* @returns {exports}
437+
*/
438+
disableImagesLoader() {
439+
webpackConfig.disableImagesLoader();
440+
441+
return this;
442+
},
443+
444+
/**
445+
* Call this if you wish to disable the default
446+
* fonts loader.
447+
*
448+
* @returns {exports}
449+
*/
450+
disableFontsLoader() {
451+
webpackConfig.disableFontsLoader();
452+
453+
return this;
454+
},
455+
432456
/**
433457
* If enabled, the output directory is emptied between
434458
* each build (to remove old files).

lib/WebpackConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class WebpackConfig {
5959
this.tsConfigurationCallback = function() {};
6060
this.useForkedTypeScriptTypeChecking = false;
6161
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
62+
this.useImagesLoader = true;
63+
this.useFontsLoader = true;
6264
}
6365

6466
getContext() {
@@ -268,6 +270,14 @@ class WebpackConfig {
268270
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
269271
}
270272

273+
disableImagesLoader() {
274+
this.useImagesLoader = false;
275+
}
276+
277+
disableFontsLoader() {
278+
this.useFontsLoader = false;
279+
}
280+
271281
cleanupOutputBeforeBuild() {
272282
this.cleanupOutput = true;
273283
}

lib/config-generator.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,30 @@ class ConfigGenerator {
122122
{
123123
test: /\.css$/,
124124
use: extractText.extract(this.webpackConfig, cssLoaderUtil.getLoaders(this.webpackConfig, false))
125-
},
126-
{
125+
}
126+
];
127+
128+
if (this.webpackConfig.useImagesLoader) {
129+
rules.push({
127130
test: /\.(png|jpg|jpeg|gif|ico|svg)$/,
128131
loader: 'file-loader',
129132
options: {
130133
name: 'images/[name].[hash:8].[ext]',
131134
publicPath: this.webpackConfig.getRealPublicPath()
132135
}
133-
},
134-
{
136+
});
137+
}
138+
139+
if (this.webpackConfig.useFontsLoader) {
140+
rules.push({
135141
test: /\.(woff|woff2|ttf|eot|otf)$/,
136142
loader: 'file-loader',
137143
options: {
138144
name: 'fonts/[name].[hash:8].[ext]',
139145
publicPath: this.webpackConfig.getRealPublicPath()
140146
}
141-
},
142-
];
147+
});
148+
}
143149

144150
if (this.webpackConfig.useSassLoader) {
145151
rules.push({

test/WebpackConfig.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,22 @@ describe('WebpackConfig object', () => {
417417
expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
418418
});
419419
});
420+
421+
describe('disableImagesLoader', () => {
422+
it('Disable default images loader', () => {
423+
const config = createConfig();
424+
config.disableImagesLoader();
425+
426+
expect(config.useImagesLoader).to.be.false;
427+
});
428+
});
429+
430+
describe('disableFontsLoader', () => {
431+
it('Disable default fonts loader', () => {
432+
const config = createConfig();
433+
config.disableFontsLoader();
434+
435+
expect(config.useFontsLoader).to.be.false;
436+
});
437+
});
420438
});

test/config-generator.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,64 @@ describe('The config-generator function', () => {
416416
expect(ignorePlugin).to.not.be.undefined;
417417
});
418418
});
419+
420+
describe('disableImagesLoader() removes the default images loader', () => {
421+
it('without disableImagesLoader()', () => {
422+
const config = createConfig();
423+
config.outputPath = '/tmp/output/public-path';
424+
config.publicPath = '/public-path';
425+
config.addEntry('main', './main');
426+
// do not call disableImagesLoader
427+
428+
const actualConfig = configGenerator(config);
429+
430+
expect(function() {
431+
findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules);
432+
}).to.not.throw();
433+
});
434+
435+
it('with disableImagesLoader()', () => {
436+
const config = createConfig();
437+
config.outputPath = '/tmp/output/public-path';
438+
config.publicPath = '/public-path';
439+
config.addEntry('main', './main');
440+
config.disableImagesLoader();
441+
442+
const actualConfig = configGenerator(config);
443+
444+
expect(function() {
445+
findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules);
446+
}).to.throw();
447+
});
448+
});
449+
450+
describe('disableFontsLoader() removes the default fonts loader', () => {
451+
it('without disableFontsLoader()', () => {
452+
const config = createConfig();
453+
config.outputPath = '/tmp/output/public-path';
454+
config.publicPath = '/public-path';
455+
config.addEntry('main', './main');
456+
// do not call disableFontsLoader
457+
458+
const actualConfig = configGenerator(config);
459+
460+
expect(function() {
461+
findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules);
462+
}).to.not.throw();
463+
});
464+
465+
it('with disableFontsLoader()', () => {
466+
const config = createConfig();
467+
config.outputPath = '/tmp/output/public-path';
468+
config.publicPath = '/public-path';
469+
config.addEntry('main', './main');
470+
config.disableFontsLoader();
471+
472+
const actualConfig = configGenerator(config);
473+
474+
expect(function() {
475+
findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules);
476+
}).to.throw();
477+
});
478+
});
419479
});

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ describe('Public API', () => {
204204

205205
});
206206

207+
describe('disableImagesLoader', () => {
208+
209+
it('must return the API object', () => {
210+
const returnedValue = api.disableImagesLoader();
211+
expect(returnedValue).to.equal(api);
212+
});
213+
214+
});
215+
216+
describe('disableFontsLoader', () => {
217+
218+
it('must return the API object', () => {
219+
const returnedValue = api.disableFontsLoader();
220+
expect(returnedValue).to.equal(api);
221+
});
222+
223+
});
224+
207225
describe('cleanupOutputBeforeBuild', () => {
208226

209227
it('must return the API object', () => {

0 commit comments

Comments
 (0)