From cc1833b3f8e98b2d7e39441a02351d01f30d0bc2 Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Thu, 20 Jul 2017 19:31:48 +0200 Subject: [PATCH 1/3] Add Encore.disableAssetsLoaders() --- index.js | 12 ++++++++++++ lib/WebpackConfig.js | 5 +++++ lib/config-generator.js | 16 ++++++++++------ test/WebpackConfig.js | 9 +++++++++ test/config-generator.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 83b71888..13329a27 100644 --- a/index.js +++ b/index.js @@ -429,6 +429,18 @@ module.exports = { return this; }, + /** + * Call this if you wish to disable the default + * assets loaders (images and fonts). + * + * @returns {exports} + */ + disableAssetsLoaders() { + webpackConfig.disableAssetsLoaders(); + + 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..9630bf58 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -59,6 +59,7 @@ class WebpackConfig { this.tsConfigurationCallback = function() {}; this.useForkedTypeScriptTypeChecking = false; this.forkedTypeScriptTypesCheckOptionsCallback = () => {}; + this.useAssetsLoaders = true; } getContext() { @@ -268,6 +269,10 @@ class WebpackConfig { this.vueLoaderOptionsCallback = vueLoaderOptionsCallback; } + disableAssetsLoaders() { + this.useAssetsLoaders = false; + } + cleanupOutputBeforeBuild() { this.cleanupOutput = true; } diff --git a/lib/config-generator.js b/lib/config-generator.js index 11e8ab2f..1eb9985d 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -122,24 +122,28 @@ class ConfigGenerator { { test: /\.css$/, use: extractText.extract(this.webpackConfig, cssLoaderUtil.getLoaders(this.webpackConfig, false)) - }, - { + } + ]; + + if (this.webpackConfig.useAssetsLoaders) { + rules.push({ test: /\.(png|jpg|jpeg|gif|ico|svg)$/, loader: 'file-loader', options: { name: 'images/[name].[hash:8].[ext]', publicPath: this.webpackConfig.getRealPublicPath() } - }, - { + }); + + 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..dca5b080 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -430,4 +430,13 @@ describe('WebpackConfig object', () => { expect(config.loaders).to.deep.equals([{ 'test': /\.custom$/, 'loader': 'custom-loader' }]); }); }); + + describe('disableAssetsLoaders', () => { + it('Disable default assets loaders', () => { + const config = createConfig(); + config.disableAssetsLoaders(); + + expect(config.useAssetsLoaders).to.be.false; + }); + }); }); diff --git a/test/config-generator.js b/test/config-generator.js index 9d5b739e..98223978 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -416,4 +416,36 @@ describe('The config-generator function', () => { expect(ignorePlugin).to.not.be.undefined; }); }); + + describe('disableAssetsLoaders() removes the default assets loaders', () => { + it('without disableAssetsLoaders()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + // do not call disableAssetsLoaders + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules); + findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules); + }).to.not.throw(); + }); + + it('with disableAssetsLoaders()', () => { + const config = createConfig(); + config.outputPath = '/tmp/output/public-path'; + config.publicPath = '/public-path'; + config.addEntry('main', './main'); + config.disableAssetsLoaders(); + + const actualConfig = configGenerator(config); + + expect(function() { + findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules); + findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules); + }).to.throw(); + }); + }); }); From 051759f25845d3483126c75c7b81529a56f5624b Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Fri, 21 Jul 2017 19:03:13 +0200 Subject: [PATCH 2/3] Add public API test --- test/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/index.js b/test/index.js index 5c4c8687..b6118212 100644 --- a/test/index.js +++ b/test/index.js @@ -204,6 +204,15 @@ describe('Public API', () => { }); + describe('disableAssetsLoaders', () => { + + it('must return the API object', () => { + const returnedValue = api.disableAssetsLoaders(); + expect(returnedValue).to.equal(api); + }); + + }); + describe('cleanupOutputBeforeBuild', () => { it('must return the API object', () => { From 3947d56c415da6e2de8128d52b4595ea2e71b477 Mon Sep 17 00:00:00 2001 From: Lyrkan Date: Tue, 25 Jul 2017 19:07:40 +0200 Subject: [PATCH 3/3] Split disableAssetsLoaders into disableImagesLoader and disableFontsLoader --- index.js | 18 +++++++++++++++--- lib/WebpackConfig.js | 11 ++++++++--- lib/config-generator.js | 4 +++- test/WebpackConfig.js | 17 +++++++++++++---- test/config-generator.js | 40 ++++++++++++++++++++++++++++++++++------ test/index.js | 13 +++++++++++-- 6 files changed, 84 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 13329a27..ca02b927 100644 --- a/index.js +++ b/index.js @@ -431,12 +431,24 @@ module.exports = { /** * Call this if you wish to disable the default - * assets loaders (images and fonts). + * images loader. * * @returns {exports} */ - disableAssetsLoaders() { - webpackConfig.disableAssetsLoaders(); + disableImagesLoader() { + webpackConfig.disableImagesLoader(); + + return this; + }, + + /** + * Call this if you wish to disable the default + * fonts loader. + * + * @returns {exports} + */ + disableFontsLoader() { + webpackConfig.disableFontsLoader(); return this; }, diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 9630bf58..81dbf669 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -59,7 +59,8 @@ class WebpackConfig { this.tsConfigurationCallback = function() {}; this.useForkedTypeScriptTypeChecking = false; this.forkedTypeScriptTypesCheckOptionsCallback = () => {}; - this.useAssetsLoaders = true; + this.useImagesLoader = true; + this.useFontsLoader = true; } getContext() { @@ -269,8 +270,12 @@ class WebpackConfig { this.vueLoaderOptionsCallback = vueLoaderOptionsCallback; } - disableAssetsLoaders() { - this.useAssetsLoaders = false; + disableImagesLoader() { + this.useImagesLoader = false; + } + + disableFontsLoader() { + this.useFontsLoader = false; } cleanupOutputBeforeBuild() { diff --git a/lib/config-generator.js b/lib/config-generator.js index 1eb9985d..40e3dc36 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -125,7 +125,7 @@ class ConfigGenerator { } ]; - if (this.webpackConfig.useAssetsLoaders) { + if (this.webpackConfig.useImagesLoader) { rules.push({ test: /\.(png|jpg|jpeg|gif|ico|svg)$/, loader: 'file-loader', @@ -134,7 +134,9 @@ class ConfigGenerator { publicPath: this.webpackConfig.getRealPublicPath() } }); + } + if (this.webpackConfig.useFontsLoader) { rules.push({ test: /\.(woff|woff2|ttf|eot|otf)$/, loader: 'file-loader', diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index dca5b080..7b1abd42 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -431,12 +431,21 @@ describe('WebpackConfig object', () => { }); }); - describe('disableAssetsLoaders', () => { - it('Disable default assets loaders', () => { + describe('disableImagesLoader', () => { + it('Disable default images loader', () => { const config = createConfig(); - config.disableAssetsLoaders(); + config.disableImagesLoader(); - expect(config.useAssetsLoaders).to.be.false; + 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 98223978..9b4dd2bd 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -417,33 +417,61 @@ describe('The config-generator function', () => { }); }); - describe('disableAssetsLoaders() removes the default assets loaders', () => { - it('without disableAssetsLoaders()', () => { + 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 disableAssetsLoaders + // do not call disableImagesLoader const actualConfig = configGenerator(config); expect(function() { findRule(/\.(png|jpg|jpeg|gif|ico|svg)$/, actualConfig.module.rules); - findRule(/\.(woff|woff2|ttf|eot|otf)$/, actualConfig.module.rules); }).to.not.throw(); }); - it('with disableAssetsLoaders()', () => { + it('with disableImagesLoader()', () => { const config = createConfig(); config.outputPath = '/tmp/output/public-path'; config.publicPath = '/public-path'; config.addEntry('main', './main'); - config.disableAssetsLoaders(); + 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 b6118212..6ab715d9 100644 --- a/test/index.js +++ b/test/index.js @@ -204,10 +204,19 @@ describe('Public API', () => { }); - describe('disableAssetsLoaders', () => { + describe('disableImagesLoader', () => { it('must return the API object', () => { - const returnedValue = api.disableAssetsLoaders(); + 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); });