1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const webpack = require('webpack')
|
5 | const merge = require('webpack-merge')
|
6 | const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
|
7 | const CopyWebpackPlugin = require('copy-webpack-plugin-hash')
|
8 | const { SinaHybridPlugin } = require('../libs/hybrid')
|
9 | const HtmlWebpackPlugin = require('sina-html-webpack-plugin')
|
10 | const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
|
11 |
|
12 | const config = require('../config')
|
13 | const { banner, rootPath } = require('../libs/utils')
|
14 |
|
15 | const maraConf = require(config.paths.marauder)
|
16 | const shouldUseSourceMap = !!maraConf.sourceMap
|
17 |
|
18 | function getLibraryConf() {
|
19 | const pkgName = require(config.paths.packageJson).name
|
20 |
|
21 | return pkgName
|
22 | }
|
23 |
|
24 | module.exports = function({ entry, cmd }) {
|
25 | const distPageDir = `${config.paths.dist}/${entry}`
|
26 | const baseWebpackConfig = require('./webpack.base.conf')(entry, 'mc')
|
27 | const hasHtml = fs.existsSync(`${config.paths.page}/${entry}/index.html`)
|
28 |
|
29 | const webpackConfig = merge(baseWebpackConfig, {
|
30 |
|
31 | bail: true,
|
32 |
|
33 | devtool: shouldUseSourceMap ? 'source-map' : false,
|
34 | output: {
|
35 | path: distPageDir,
|
36 | publicPath: config.build.assetsPublicPath,
|
37 | filename: maraConf.hash
|
38 | ? 'static/js/[name].[chunkhash:8].min.js'
|
39 | : 'static/js/[name].min.js',
|
40 | library: entry,
|
41 |
|
42 | libraryTarget: 'umd'
|
43 | },
|
44 | plugins: [
|
45 | new webpack.DefinePlugin(config.build.env.stringified),
|
46 | new UglifyJsPlugin({
|
47 | uglifyOptions: {
|
48 | ecma: 5,
|
49 | compress: {
|
50 | warnings: false,
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | comparisons: false
|
56 | },
|
57 | mangle: {
|
58 | safari10: true
|
59 | },
|
60 | output: {
|
61 | comments: false,
|
62 |
|
63 |
|
64 | ascii_only: true
|
65 | }
|
66 | },
|
67 |
|
68 |
|
69 | parallel: true,
|
70 |
|
71 | cache: true,
|
72 | sourceMap: shouldUseSourceMap
|
73 | }),
|
74 | new webpack.BannerPlugin({
|
75 | banner: banner(),
|
76 | entryOnly: true
|
77 | }),
|
78 | new OptimizeCssAssetsPlugin({
|
79 |
|
80 |
|
81 |
|
82 | cssProcessorOptions: Object.assign(
|
83 | { autoprefixer: false, safe: true },
|
84 | shouldUseSourceMap
|
85 | ? {
|
86 | map: { inline: false }
|
87 | }
|
88 | : {}
|
89 | ),
|
90 | canPrint: false
|
91 | }),
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
98 | hasHtml &&
|
99 | new HtmlWebpackPlugin({
|
100 |
|
101 | filename: rootPath(`dist/${entry}/index.html`),
|
102 |
|
103 | template: `${config.paths.page}/${entry}/index.html`,
|
104 | minify: false,
|
105 |
|
106 | inject: true,
|
107 | collapseWhitespace: true,
|
108 | removeRedundantAttributes: true,
|
109 | useShortDoctype: true,
|
110 | removeEmptyAttributes: true,
|
111 | removeStyleLinkTypeAttributes: true,
|
112 | keepClosingSlash: true
|
113 | }),
|
114 |
|
115 | maraConf.hybrid && new SinaHybridPlugin({ entry }),
|
116 | ...copyPublicFiles(entry, distPageDir)
|
117 | ].filter(Boolean)
|
118 | })
|
119 |
|
120 | function copyPublicFiles(entry, distPageDir) {
|
121 | const pagePublicDir = rootPath(`${config.paths.page}/${entry}/public`)
|
122 | const plugins = []
|
123 |
|
124 | function getCopyOption(src) {
|
125 | return {
|
126 | from: src,
|
127 |
|
128 | to: distPageDir,
|
129 | ignore: ['.*']
|
130 | }
|
131 | }
|
132 |
|
133 |
|
134 | if (fs.existsSync(config.paths.public)) {
|
135 | plugins.push(new CopyWebpackPlugin([getCopyOption(config.paths.public)]))
|
136 | }
|
137 |
|
138 |
|
139 | if (fs.existsSync(pagePublicDir)) {
|
140 | plugins.push(new CopyWebpackPlugin([getCopyOption(pagePublicDir)]))
|
141 | }
|
142 |
|
143 | return plugins
|
144 | }
|
145 |
|
146 |
|
147 | if (maraConf.zip === true || maraConf.hybrid) {
|
148 | const ZipPlugin = require('zip-webpack-plugin')
|
149 | webpackConfig.plugins.push(
|
150 | new ZipPlugin({
|
151 |
|
152 |
|
153 | filename: entry,
|
154 |
|
155 |
|
156 |
|
157 | extension: maraConf.hybrid ? 'php' : 'zip',
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | exclude: maraConf.debug
|
165 | ? [
|
166 | /__MACOSX$/,
|
167 | /.DS_Store$/,
|
168 | /dependencyGraph.json$/,
|
169 | /debug.css$/,
|
170 | /build.json$/,
|
171 | /js.map$/,
|
172 | /css.map$/
|
173 | ]
|
174 | : [
|
175 | /__MACOSX$/,
|
176 | /.DS_Store$/,
|
177 | /dependencyGraph.json$/,
|
178 | /debug.js$/,
|
179 | /debug.css$/,
|
180 | /build.json$/,
|
181 | /js.map$/,
|
182 | /css.map$/
|
183 | ],
|
184 |
|
185 |
|
186 |
|
187 | fileOptions: {
|
188 | mtime: new Date(),
|
189 | mode: 0o100664,
|
190 | compress: true,
|
191 | forceZip64Format: false
|
192 | },
|
193 |
|
194 | zipOptions: {
|
195 | forceZip64Format: false
|
196 | }
|
197 | })
|
198 | )
|
199 | }
|
200 |
|
201 | return webpackConfig
|
202 | }
|