-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3765 from alibaba/refactor/react-app-webpack-config
refactor: react app webpack config
- Loading branch information
Showing
17 changed files
with
303 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import getMpaEntries from './getMpaEntries'; | ||
import getRoutesByAppJson from './getRoutesByAppJson'; | ||
import formatPath from './formatPath'; | ||
export { default as getMpaEntries } from './getMpaEntries'; | ||
export { default as getRoutesByAppJson } from './getRoutesByAppJson'; | ||
export { default as formatPath } from './formatPath'; | ||
export { default as validation } from './validation'; | ||
|
||
export { | ||
getMpaEntries, | ||
getRoutesByAppJson, | ||
formatPath, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const assert = require('assert'); | ||
const { isPlainObject } = require('lodash'); | ||
|
||
const validation = (key, value, types) => { | ||
const validateResult = types.split('|').some((type) => { | ||
if (type === 'array') { | ||
return Array.isArray(value); | ||
} else if (type === 'object') { | ||
return isPlainObject(value); | ||
} else { | ||
// eslint-disable-next-line valid-typeof | ||
return typeof value === type; | ||
} | ||
}); | ||
assert(validateResult, `Config ${key} should be ${types.replace('|', ' | ')}, but got ${value}`); | ||
return validateResult; | ||
}; | ||
|
||
export default validation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
const applyCliOption = require('./applyCliOption'); | ||
const applyUserConfig = require('./applyUserConfig'); | ||
const getEnhanceWebpackConfig = require('./getEnhanceWebpackConfig'); | ||
const getDefaultConfig = require('./config/default.config'); | ||
const defaultConfig = require('./config/default.config'); | ||
|
||
export { | ||
applyCliOption, | ||
applyUserConfig, | ||
getEnhanceWebpackConfig, | ||
getDefaultConfig | ||
defaultConfig | ||
}; |
16 changes: 0 additions & 16 deletions
16
packages/build-webpack-config/src/utils/getWebOutputPath.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { validation } = require('build-app-helpers'); | ||
|
||
/* eslint global-require: 0 */ | ||
module.exports = [ | ||
{ | ||
name: 'entry', | ||
defaultValue: 'src/index.jsx', | ||
configWebpack: require('./userConfig/entry'), | ||
validation: (val) => { | ||
// entry: string | array | ||
// entry : { [name]: string | array } | ||
return validation('entry', val, 'string|array|object'); | ||
}, | ||
}, | ||
{ | ||
name: 'ignoreHtmlTemplate', | ||
defauleValue: false, | ||
configWebpack: require('./userConfig/ignoreHtmlTemplate'), | ||
validation: 'boolean', | ||
}, | ||
{ | ||
name: 'outputDir', | ||
defaultValue: 'build', | ||
configWebpack: require('./userConfig/outputDir'), | ||
validation: 'string' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const path = require('path'); | ||
const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
const CopyWebpackPlugin = require('copy-webpack-plugin'); | ||
const WebpackPluginImport = require('webpack-plugin-import'); | ||
const { getFilePath, getWebOutputPath } = require('./utils'); | ||
const { WEB } = require('./constants'); | ||
|
||
module.exports = (api, { target, webpackConfig }) => { | ||
const { context } = api; | ||
const { rootDir } = context; | ||
const outputPath = getWebOutputPath(context, { target }); | ||
|
||
webpackConfig | ||
// SimpleProgressPlugin | ||
.plugin('SimpleProgressPlugin') | ||
.tap(([args]) => { | ||
return [{ | ||
...args, | ||
progressOptions: { | ||
clear: true, | ||
callback: () => { | ||
console.log(); | ||
} | ||
} | ||
}]; | ||
}) | ||
.end() | ||
// HtmlWebpackPlugin | ||
.plugin('HtmlWebpackPlugin') | ||
.use(HtmlWebpackPlugin, [{ | ||
inject: true, | ||
templateParameters: { | ||
NODE_ENV: process.env.NODE_ENV, | ||
}, | ||
favicon: getFilePath([ | ||
path.join(rootDir, 'public/favicon.ico'), | ||
path.join(rootDir, 'public/favicon.png'), | ||
]), | ||
template: path.resolve(rootDir, 'public/index.html'), | ||
minify: false, | ||
}]) | ||
.end() | ||
// CopyWebpackPlugin | ||
.plugin('CopyWebpackPlugin') | ||
.use(CopyWebpackPlugin, [[ | ||
{ | ||
from: path.resolve(rootDir, 'public'), | ||
to: path.resolve(rootDir, outputPath), | ||
ignore: ['index.html'], | ||
}, | ||
]]) | ||
.end() | ||
// WebpackPluginImport | ||
.plugin('WebpackPluginImport') | ||
.use(WebpackPluginImport, [[ | ||
{ | ||
libraryName: /@ali\/ice-.*/, | ||
stylePath: 'style.js', | ||
}, | ||
]]) | ||
.end(); | ||
|
||
// Process rpx to vw | ||
if (target === WEB) { | ||
const cssPreprocessor = [ 'scss', 'scss-module', 'css', 'css-module', 'less', 'less-module']; | ||
cssPreprocessor.forEach(rule => { | ||
if (webpackConfig.module.rules.get(rule)) { | ||
webpackConfig.module | ||
.rule(rule) | ||
.use('postcss-loader') | ||
.tap((options) => { | ||
const { plugins = [] } = options; | ||
return { | ||
...options, | ||
plugins: [ | ||
...plugins, | ||
// eslint-disable-next-line | ||
require('postcss-plugin-rpx2vw') | ||
], | ||
}; | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return webpackConfig; | ||
}; |
Oops, something went wrong.