Skip to content

Allow to disable the default image and font loaders #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
10 changes: 10 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class WebpackConfig {
this.tsConfigurationCallback = function() {};
this.useForkedTypeScriptTypeChecking = false;
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
this.useImagesLoader = true;
this.useFontsLoader = true;
}

getContext() {
Expand Down Expand Up @@ -268,6 +270,14 @@ class WebpackConfig {
this.vueLoaderOptionsCallback = vueLoaderOptionsCallback;
}

disableImagesLoader() {
this.useImagesLoader = false;
}

disableFontsLoader() {
this.useFontsLoader = false;
}

cleanupOutputBeforeBuild() {
this.cleanupOutput = true;
}
Expand Down
18 changes: 12 additions & 6 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
18 changes: 18 additions & 0 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
60 changes: 60 additions & 0 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down