ant-design-vue/antd-tools/getWebpackConfig.js

211 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-03-18 11:59:38 +00:00
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const deepAssign = require('deep-assign')
const chalk = require('chalk')
const postcssConfig = require('./postcssConfig')
const distFileBaseName = 'antd'
module.exports = function (modules) {
const pkg = require(path.join(process.cwd(), 'package.json'))
const babelConfig = require('./getBabelCommonConfig')(modules || false)
const pluginImportOptions = [
{
style: true,
libraryName: 'antd',
libraryDirectory: 'components',
},
]
// if (distFileBaseName !== 'antd') { pluginImportOptions.push({ style:
// 'css', libraryDirectory: 'components', libraryName: 'antd', }) }
babelConfig
.plugins
.push([
require.resolve('babel-plugin-import'),
pluginImportOptions,
])
2018-03-18 11:59:38 +00:00
const config = {
devtool: 'source-map',
output: {
path: path.join(process.cwd(), './dist/'),
filename: '[name].js',
},
resolve: {
modules: [
'node_modules', path.join(__dirname, '../node_modules'),
],
extensions: [
'.js', '.jsx', '.vue', '.md', '.json',
],
2018-03-18 11:59:38 +00:00
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': process.cwd(),
},
},
node: [
'child_process',
'cluster',
'dgram',
'dns',
'fs',
'module',
'net',
'readline',
'repl',
'tls',
].reduce((acc, name) => Object.assign({}, acc, { [name]: 'empty' }), {}),
module: {
noParse: [/moment.js/],
rules: [
{
test: /\.vue$/,
exclude: /node_modules/,
use: [
{
loader: 'vue-loader',
2018-03-19 13:23:38 +00:00
options: {
loaders: {
js: [
{
loader: 'babel-loader',
2018-03-19 13:23:38 +00:00
options: {
presets: ['env'],
plugins: ['transform-vue-jsx', 'transform-object-rest-spread'],
},
},
2018-03-19 13:23:38 +00:00
],
},
},
2018-03-18 11:59:38 +00:00
},
],
}, {
2018-03-19 13:23:38 +00:00
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
2018-03-19 13:23:38 +00:00
options: babelConfig,
}, {
2018-03-18 11:59:38 +00:00
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
},
}, {
2018-03-18 11:59:38 +00:00
loader: 'postcss-loader',
options: Object.assign({}, postcssConfig, { sourceMap: true }),
2018-03-18 11:59:38 +00:00
},
],
}),
}, {
2018-03-18 11:59:38 +00:00
test: /\.less$/,
use: ExtractTextPlugin.extract({
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
},
}, {
2018-03-18 11:59:38 +00:00
loader: 'postcss-loader',
options: Object.assign({}, postcssConfig, { sourceMap: true }),
}, {
2018-03-18 11:59:38 +00:00
loader: 'less-loader',
options: {
sourceMap: true,
},
},
],
}),
},
],
},
plugins: [
new ExtractTextPlugin({ filename: '[name].css', disable: false, allChunks: true }),
2018-03-18 11:59:38 +00:00
new CaseSensitivePathsPlugin(),
new webpack.BannerPlugin(`
${distFileBaseName} v${pkg.version}
Copyright 2017-present, ant-design-vue.
2018-03-18 11:59:38 +00:00
All rights reserved.
`),
new webpack.ProgressPlugin((percentage, msg, addInfo) => {
const stream = process.stderr
if (stream.isTTY && percentage < 0.71) {
stream.cursorTo(0)
stream.write(`📦 ${chalk.magenta(msg)} (${chalk.magenta(addInfo)})`)
stream.clearLine(1)
} else if (percentage === 1) {
console.log(chalk.green('\nwebpack: bundle build is now finished.'))
}
}),
],
}
if (process.env.RUN_ENV === 'PRODUCTION') {
2018-03-21 13:16:11 +00:00
const entry = ['./index']
2018-03-18 11:59:38 +00:00
config.entry = {
[`${distFileBaseName}.min`]: entry,
}
config.externals = {
vue: {
root: 'Vue',
commonjs2: 'vue',
commonjs: 'vue',
amd: 'vue',
},
}
config.output.library = distFileBaseName
config.output.libraryTarget = 'umd'
const uncompressedConfig = deepAssign({}, config)
config.plugins = config
.plugins
.concat([
new webpack
.optimize
.UglifyJsPlugin({
sourceMap: true,
output: {
ascii_only: true,
},
compress: {
warnings: false,
},
}),
new webpack
.optimize
.ModuleConcatenationPlugin(),
new webpack.LoaderOptionsPlugin({ minimize: true }),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
}),
])
2018-03-18 11:59:38 +00:00
uncompressedConfig.entry = {
[distFileBaseName]: entry,
}
uncompressedConfig
.plugins
.push(new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
}))
2018-03-18 11:59:38 +00:00
return [config, uncompressedConfig]
}
return config
}