Skip to content
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

feat: use umirc for webpack config #458

Merged
merged 1 commit into from
May 14, 2018
Merged
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
1 change: 1 addition & 0 deletions packages/af-webpack/getUserConfigPlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/getUserConfig/getPlugins');
9 changes: 9 additions & 0 deletions packages/af-webpack/src/getUserConfig/getPlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { join } from 'path';
import requireindex from 'requireindex';

export default function() {
const pluginsMap = requireindex(join(__dirname, './configs'));
return Object.keys(pluginsMap).map(key => {
return pluginsMap[key].default();
});
}
7 changes: 2 additions & 5 deletions packages/af-webpack/src/getUserConfig/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ import { existsSync, readFileSync } from 'fs';
import { join, resolve } from 'path';
import assert from 'assert';
import stripJsonComments from 'strip-json-comments';
import requireindex from 'requireindex';
import didyoumean from 'didyoumean';
import chalk from 'chalk';
import isEqual from 'lodash.isequal';
import isPlainObject from 'is-plain-object';
import { clearConsole } from '../reactDevUtils';
import { watch, unwatch } from './watch';
import getPlugins from './getPlugins';

const debug = require('debug')('af-webpack:getUserConfig');

const pluginsMap = requireindex(join(__dirname, './configs'));
const plugins = Object.keys(pluginsMap).map(key => {
return pluginsMap[key].default();
});
const plugins = getPlugins();
const pluginNames = plugins.map(p => p.name);
const pluginsMapByName = plugins.reduce((memo, p) => {
memo[p.name] = p;
Expand Down
4 changes: 0 additions & 4 deletions packages/umi-build-dev/src/getConfig/configPlugins/routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import assert from 'assert';
import { join } from 'path';
import { existsSync } from 'fs';

export default function(api) {
const { cwd } = api.service.paths;

return {
name: 'routes',
validate(val) {
Expand Down
1 change: 1 addition & 0 deletions packages/umi-build-dev/src/getPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Try:
'./plugins/hd',
'./plugins/mock',
'./plugins/hash-history',
'./plugins/afwebpack-config',
'./plugins/404', // 404 must after mock
];
const plugins = [
Expand Down
10 changes: 7 additions & 3 deletions packages/umi-build-dev/src/getWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default function(service = {}) {
const {
cwd,
config,
webpackRCConfig,
babel,
hash,
routes,
Expand All @@ -23,6 +22,13 @@ export default function(service = {}) {
} = service;
const isDev = process.env.NODE_ENV === 'development';

// merge config to webpackRCConfig
let { webpackRCConfig } = service;
webpackRCConfig = {
...(config || {}),
...(webpackRCConfig || {}),
};

// entry
const entryScript = join(cwd, `./${paths.tmpDirPath}/${libraryName}.js`);
const setPublicPathFile = join(__dirname, '../template/setPublicPath.js');
Expand All @@ -39,7 +45,6 @@ export default function(service = {}) {

const pageCount = isDev ? null : Object.keys(routes).length;
debug(`pageCount: ${pageCount}`);
debug(`config: ${JSON.stringify(config)}`);

// default react, support config with preact
// 优先级:用户配置 > preact argument > default (React)
Expand Down Expand Up @@ -177,7 +182,6 @@ export default function(service = {}) {
afWebpackOpts = service.applyPlugins('modifyAFWebpackOpts', {
initialValue: afWebpackOpts,
});
debug(`afWebpackOpts: ${JSON.stringify(afWebpackOpts)}`);

let webpackConfig = getConfig(afWebpackOpts);
webpackConfig = service.applyPlugins('modifyWebpackConfig', {
Expand Down
26 changes: 26 additions & 0 deletions packages/umi-build-dev/src/plugins/afwebpack-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import getUserConfigPlugins from 'af-webpack/getUserConfigPlugins';

const plugins = getUserConfigPlugins();

function noop() {
return true;
}

const excludes = ['entry', 'outputPath', 'hash'];

export default api => {
api.register('modifyConfigPlugins', ({ memo }) => {
plugins.forEach(({ name, validate = noop }) => {
if (!excludes.includes(name)) {
memo.push(() => ({
name,
validate,
onChange() {
api.service.restart(`${name} changed`);
},
}));
}
});
return memo;
});
};
3 changes: 3 additions & 0 deletions packages/umi/src/scripts/realDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ process.on('SIGINT', () => {

dev({
plugins: argv.plugins ? argv.plugins.split(',') : [],
}).catch(e => {
console.log(e);
process.exit(1);
});