Skip to content

Commit cc1833b

Browse files
committed
Add Encore.disableAssetsLoaders()
1 parent 2cae5df commit cc1833b

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
@@ -429,6 +429,18 @@ module.exports = {
429429
return this;
430430
},
431431

432+
/**
433+
* Call this if you wish to disable the default
434+
* assets loaders (images and fonts).
435+
*
436+
* @returns {exports}
437+
*/
438+
disableAssetsLoaders() {
439+
webpackConfig.disableAssetsLoaders();
440+
441+
return this;
442+
},
443+
432444
/**
433445
* If enabled, the output directory is emptied between
434446
* each build (to remove old files).

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class WebpackConfig {
5959
this.tsConfigurationCallback = function() {};
6060
this.useForkedTypeScriptTypeChecking = false;
6161
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
62+
this.useAssetsLoaders = true;
6263
}
6364

6465
getContext() {
@@ -268,6 +269,10 @@ class WebpackConfig {
268269
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
269270
}
270271

272+
disableAssetsLoaders() {
273+
this.useAssetsLoaders = false;
274+
}
275+
271276
cleanupOutputBeforeBuild() {
272277
this.cleanupOutput = true;
273278
}

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].[hash:8].[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].[hash:8].[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
@@ -430,4 +430,13 @@ describe('WebpackConfig object', () => {
430430
expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]);
431431
});
432432
});
433+
434+
describe('disableAssetsLoaders', () => {
435+
it('Disable default assets loaders', () => {
436+
const config = createConfig();
437+
config.disableAssetsLoaders();
438+
439+
expect(config.useAssetsLoaders).to.be.false;
440+
});
441+
});
433442
});

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)