diff --git a/index.js b/index.js index 83b71888..ca02b927 100644 --- a/index.js +++ b/index.js @@ -429,6 +429,30 @@ module.exports = { return this; }, + /** + * Call this if you wish to disable the default + * images loader. + * + * @returns {exports} + */ + disableImagesLoader() { + webpackConfig.disableImagesLoader(); + + return this; + }, + + /** + * Call this if you wish to disable the default + * fonts loader. + * + * @returns {exports} + */ + disableFontsLoader() { + webpackConfig.disableFontsLoader(); + + return this; + }, + /** * If enabled, the output directory is emptied between * each build (to remove old files). diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 8d1a6edf..81dbf669 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -59,6 +59,8 @@ class WebpackConfig { this.tsConfigurationCallback = function() {}; this.useForkedTypeScriptTypeChecking = false; this.forkedTypeScriptTypesCheckOptionsCallback = () => {}; + this.useImagesLoader = true; + this.useFontsLoader = true; } getContext() { @@ -268,6 +270,14 @@ class WebpackConfig { this.vueLoaderOptionsCallback = vueLoaderOptionsCallback; } + disableImagesLoader() { + this.useImagesLoader = false; + } + + disableFontsLoader() { + this.useFontsLoader = false; + } + cleanupOutputBeforeBuild() { this.cleanupOutput = true; } diff --git a/lib/config-generator.js b/lib/config-generator.js index 11e8ab2f..40e3dc36 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -122,24 +122,30 @@ class ConfigGenerator { { test: /\.css$/, use: extractText.extract(this.webpackConfig, cssLoaderUtil.getLoaders(this.webpackConfig, false)) - }, - { + } + ]; + + if (this.webpackConfig.useImagesLoader) { + rules.push({ test: /\.(png|jpg|jpeg|gif|ico|svg)$/, loader: 'file-loader', options: { name: 'images/[name].[hash:8].[ext]', publicPath: this.webpackConfig.getRealPublicPath() } - }, - { + }); + } + + if (this.webpackConfig.useFontsLoader) { + rules.push({ test: /\.(woff|woff2|ttf|eot|otf)$/, loader: 'file-loader', options: { name: 'fonts/[name].[hash:8].[ext]', publicPath: this.webpackConfig.getRealPublicPath() } - }, - ]; + }); + } if (this.webpackConfig.useSassLoader) { rules.push({ diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index aac8d59c..7b1abd42 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -430,4 +430,22 @@ describe('WebpackConfig object', () => { expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); }); }); + + describe('disableImagesLoader', () => { + it('Disable default images loader', () => { + const config = createConfig(); + config.disableImagesLoader(); + + expect(config.useImagesLoader).to.be.false; + }); + }); + + describe('disableFontsLoader', () => { + it('Disable default fonts loader', () => { + const config = createConfig(); + config.disableFontsLoader(); + + expect(config.useFontsLoader).to.be.false; + }); + }); }); diff --git a/test/config-generator.js b/test/config-generator.js index 9d5b739e..9b4dd2bd 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -416,4 +416,64 @@ describe('The config-generator function', () => { expect(ignorePlugin).to.not.be.undefined; }); }); + + describe('disableImagesLoader() removes the default images loader', () => { + it('without disableImagesLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + // do not call disableImagesLoader + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules); + }).to.not.throw(); + }); + + it('with disableImagesLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + config.disableImagesLoader(); + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules); + }).to.throw(); + }); + }); + + describe('disableFontsLoader() removes the default fonts loader', () => { + it('without disableFontsLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + // do not call disableFontsLoader + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules); + }).to.not.throw(); + }); + + it('with disableFontsLoader()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + config.disableFontsLoader(); + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules); + }).to.throw(); + }); + }); }); diff --git a/test/index.js b/test/index.js index 5c4c8687..6ab715d9 100644 --- a/test/index.js +++ b/test/index.js @@ -204,6 +204,24 @@ describe('Public API', () => { }); + describe('disableImagesLoader', () => { + + it('must return the API object', () => { + const returnedValue = api.disableImagesLoader(); + expect(returnedValue).to.equal(api); + }); + + }); + + describe('disableFontsLoader', () => { + + it('must return the API object', () => { + const returnedValue = api.disableFontsLoader(); + expect(returnedValue).to.equal(api); + }); + + }); + describe('cleanupOutputBeforeBuild', () => { it('must return the API object', () => {