Skip to content

Commit aeb4a95

Browse files
Lyrkanweaverryan
authored andcommitted
Allow to disable the default image and font loaders
1 parent 2cae5df commit aeb4a95

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
@@ -430,4 +430,22 @@ describe('WebpackConfig object', () => {
430430
expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
431431
});
432432
});
433+
434+
describe('disableImagesLoader', () => {
435+
it('Disable default images loader', () => {
436+
const config = createConfig();
437+
config.disableImagesLoader();
438+
439+
expect(config.useImagesLoader).to.be.false;
440+
});
441+
});
442+
443+
describe('disableFontsLoader', () => {
444+
it('Disable default fonts loader', () => {
445+
const config = createConfig();
446+
config.disableFontsLoader();
447+
448+
expect(config.useFontsLoader).to.be.false;
449+
});
450+
});
433451
});

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)