mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-09 23:27:28 +02:00
feat: allow user to modify generated webpack config
This commit is contained in:
parent
2b5ee3e869
commit
10b1a38762
9 changed files with 172 additions and 3 deletions
107
test/webpack/utils.test.js
Normal file
107
test/webpack/utils.test.js
Normal file
|
@ -0,0 +1,107 @@
|
|||
import {validate} from 'webpack';
|
||||
import path from 'path';
|
||||
import Config from 'webpack-chain';
|
||||
|
||||
import {applyConfigureWebpack, applyChainWebpack} from '@lib/webpack/utils';
|
||||
|
||||
describe('extending generated webpack config', () => {
|
||||
test('direct mutation on generated webpack config object', async () => {
|
||||
// fake generated webpack config
|
||||
let config = {
|
||||
output: {
|
||||
path: __dirname,
|
||||
filename: 'bundle.js'
|
||||
}
|
||||
};
|
||||
|
||||
/* eslint-disable */
|
||||
const configureWebpack = (generatedConfig, isServer) => {
|
||||
if (!isServer) {
|
||||
generatedConfig.entry = 'entry.js';
|
||||
generatedConfig.output = {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'new.bundle.js'
|
||||
};
|
||||
}
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
config = applyConfigureWebpack(configureWebpack, config, false);
|
||||
expect(config).toEqual({
|
||||
entry: 'entry.js',
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'new.bundle.js'
|
||||
}
|
||||
});
|
||||
const errors = validate(config);
|
||||
|
||||
console.log(errors);
|
||||
expect(errors.length).toBe(0);
|
||||
});
|
||||
|
||||
test('webpack-merge with user webpack config object', async () => {
|
||||
// fake generated webpack config
|
||||
let config = {
|
||||
output: {
|
||||
path: __dirname,
|
||||
filename: 'bundle.js'
|
||||
}
|
||||
};
|
||||
|
||||
/* eslint-disable */
|
||||
const configureWebpack = {
|
||||
entry: 'entry.js',
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'new.bundle.js'
|
||||
}
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
config = applyConfigureWebpack(configureWebpack, config, false);
|
||||
expect(config).toEqual({
|
||||
entry: 'entry.js',
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'new.bundle.js'
|
||||
}
|
||||
});
|
||||
const errors = validate(config);
|
||||
expect(errors.length).toBe(0);
|
||||
});
|
||||
|
||||
test('use webpack-chain API', async () => {
|
||||
// fake generated webpack config in webpack-chain format
|
||||
let config = new Config();
|
||||
config.output.path(__dirname).filename('bundle.js');
|
||||
|
||||
// user chainWebpack
|
||||
/* eslint-disable */
|
||||
const chainWebpack = (oldConfig, isServer) => {
|
||||
if (!isServer) {
|
||||
oldConfig.entry('main').add('./entry.js');
|
||||
oldConfig.output
|
||||
.path(path.join(__dirname, 'dist'))
|
||||
.filename('new.bundle.js');
|
||||
}
|
||||
};
|
||||
/* eslint-enable */
|
||||
|
||||
applyChainWebpack(chainWebpack, config, false);
|
||||
|
||||
// transform to webpack configuration object format
|
||||
config = config.toConfig();
|
||||
expect(config).toEqual({
|
||||
output: {
|
||||
path: path.join(__dirname, 'dist'),
|
||||
filename: 'new.bundle.js'
|
||||
},
|
||||
entry: {
|
||||
main: ['./entry.js']
|
||||
}
|
||||
});
|
||||
const errors = validate(config);
|
||||
expect(errors.length).toBe(0);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue