Skip to content

Commit 0bbf35b

Browse files
committed
Add Encore.disableAssetsLoaders()
1 parent 4ad8d88 commit 0bbf35b

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ module.exports = {
398398
return this;
399399
},
400400

401+
/**
402+
* Call this if you wish to disable the default
403+
* assets loaders (images and fonts).
404+
*
405+
* @returns {exports}
406+
*/
407+
disableAssetsLoaders() {
408+
webpackConfig.disableAssetsLoaders();
409+
410+
return this;
411+
},
412+
401413
/**
402414
* If enabled, the output directory is emptied between
403415
* each build (to remove old files).

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class WebpackConfig {
5656
this.loaders = [];
5757
this.useTypeScriptLoader = false;
5858
this.tsConfigurationCallback = function() {};
59+
this.useAssetsLoaders = true;
5960
}
6061

6162
getContext() {
@@ -253,6 +254,10 @@ class WebpackConfig {
253254
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
254255
}
255256

257+
disableAssetsLoaders() {
258+
this.useAssetsLoaders = false;
259+
}
260+
256261
cleanupOutputBeforeBuild() {
257262
this.cleanupOutput = true;
258263
}

lib/config-generator.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,28 @@ 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.useAssetsLoaders) {
129+
rules.push({
127130
test: /\.(png|jpg|jpeg|gif|ico|svg)$/,
128131
loader: 'file-loader',
129132
options: {
130133
name: `images/[name]${this.webpackConfig.useVersioning ? '.[hash]' : ''}.[ext]`,
131134
publicPath: this.webpackConfig.getRealPublicPath()
132135
}
133-
},
134-
{
136+
});
137+
138+
rules.push({
135139
test: /\.(woff|woff2|ttf|eot|otf)$/,
136140
loader: 'file-loader',
137141
options: {
138142
name: `fonts/[name]${this.webpackConfig.useVersioning ? '.[hash]' : ''}.[ext]`,
139143
publicPath: this.webpackConfig.getRealPublicPath()
140144
}
141-
},
142-
];
145+
});
146+
}
143147

144148
if (this.webpackConfig.useSassLoader) {
145149
rules.push({

test/WebpackConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,13 @@ describe('WebpackConfig object', () => {
358358
expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
359359
});
360360
});
361+
362+
describe('disableAssetsLoaders', () => {
363+
it('Disable default assets loaders', () => {
364+
const config = createConfig();
365+
config.disableAssetsLoaders();
366+
367+
expect(config.useAssetsLoaders).to.be.false;
368+
});
369+
});
361370
});

test/config-generator.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,36 @@ describe('The config-generator function', () => {
416416
expect(ignorePlugin).to.not.be.undefined;
417417
});
418418
});
419+
420+
describe('disableAssetsLoaders() removes the default assets loaders', () => {
421+
it('without disableAssetsLoaders()', () => {
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 disableAssetsLoaders
427+
428+
const actualConfig = configGenerator(config);
429+
430+
expect(function() {
431+
findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules);
432+
findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules);
433+
}).to.not.throw();
434+
});
435+
436+
it('with disableAssetsLoaders()', () => {
437+
const config = createConfig();
438+
config.outputPath = '/tmp/output/public-path';
439+
config.publicPath = '/public-path';
440+
config.addEntry('main', './main');
441+
config.disableAssetsLoaders();
442+
443+
const actualConfig = configGenerator(config);
444+
445+
expect(function() {
446+
findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules);
447+
findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules);
448+
}).to.throw();
449+
});
450+
});
419451
});

0 commit comments

Comments
 (0)