UNPKG

3.7 kBJavaScriptView Raw
1'use strict'
2
3const webpack = require('webpack')
4const merge = require('webpack-merge')
5const ExtractTextPlugin = require('extract-text-webpack-plugin')
6const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
7const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
8
9const config = require('../config')
10const { banner, getEntries } = require('../libs/utils')
11
12const maraConf = require(config.paths.marauder)
13const shouldUseSourceMap = !!maraConf.sourceMap
14
15function getLibraryConf() {
16 const pkgName = require(config.paths.packageJson).name
17
18 return pkgName
19}
20
21module.exports = function(options) {
22 const baseWebpackConfig = require('./webpack.base.conf')('__LIB__')
23
24 const webpackConfig = merge(baseWebpackConfig, {
25 // 在第一个错误出错时抛出,而不是无视错误
26 bail: true,
27 entry: getEntries(config.paths.libEntry),
28 devtool: shouldUseSourceMap ? 'source-map' : false,
29 output: {
30 path: config.paths.lib,
31 filename: options.filename,
32 library: getLibraryConf(),
33 // https://doc.webpack-china.org/configuration/output/#output-librarytarget
34 libraryTarget: options.format
35 },
36 plugins: [
37 new webpack.DefinePlugin(config.build.env.stringified),
38 options.minify &&
39 new UglifyJsPlugin({
40 uglifyOptions: {
41 ecma: 5,
42 compress: {
43 warnings: false,
44 // Disabled because of an issue with Uglify breaking seemingly valid code:
45 // https://github.com/facebook/create-react-app/issues/2376
46 // Pending further investigation:
47 // https://github.com/mishoo/UglifyJS2/issues/2011
48 comparisons: false
49 },
50 mangle: {
51 safari10: true
52 },
53 output: {
54 comments: false,
55 // Turned on because emoji and regex is not minified properly using default
56 // https://github.com/facebook/create-react-app/issues/2488
57 ascii_only: true
58 }
59 },
60 // Use multi-process parallel running to improve the build speed
61 // Default number of concurrent runs: os.cpus().length - 1
62 parallel: true,
63 // Enable file caching
64 cache: true,
65 sourceMap: shouldUseSourceMap
66 }),
67 new webpack.BannerPlugin({
68 banner: banner(), // 其值为字符串,将作为注释存在
69 entryOnly: true // 如果值为 true,将只在入口 chunks 文件中添加
70 }),
71 new ExtractTextPlugin({
72 filename: options.minify ? 'style.min.css' : 'style.css'
73 }),
74 options.minify &&
75 new OptimizeCssAssetsPlugin({
76 // cssnano 中自带 autoprefixer,在压缩时会根据配置去除无用前缀
77 // 为保持统一,将其禁用,在 4.0 版本后将会默认禁用
78 // safe: true 禁止计算 z-index
79 cssProcessorOptions: Object.assign(
80 { autoprefixer: false, safe: true },
81 shouldUseSourceMap
82 ? {
83 map: { inline: false }
84 }
85 : {}
86 ),
87 canPrint: false // 不显示通知
88 }),
89 // Moment.js is an extremely popular library that bundles large locale files
90 // by default due to how Webpack interprets its code. This is a practical
91 // solution that requires the user to opt into importing specific locales.
92 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
93 // You can remove this if you don't use Moment.js:
94 new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
95 ].filter(Boolean)
96 })
97
98 return webpackConfig
99}