1 | const chalk = require('chalk')
|
2 | const lodash = require('lodash')
|
3 | const webpack = require('webpack')
|
4 | const CopyWebpackPlugin = require('copy-webpack-plugin')
|
5 | const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
|
6 | const HtmlWebpackPlugin = require('html-webpack-plugin')
|
7 | const ProgressBarWebpackPlugin = require('progress-bar-webpack-plugin')
|
8 | const { VueLoaderPlugin } = require('vue-loader')
|
9 | const config = require('./lib/config.js')
|
10 | const helpers = require('./lib/helpers.js')
|
11 |
|
12 | let webpackConfig = {
|
13 | entry: {
|
14 | main: ['babel-polyfill', helpers.resolveProjectPath(config.entry)]
|
15 | },
|
16 |
|
17 | output: {
|
18 | filename: '[name].js',
|
19 | globalObject: 'this',
|
20 | path: helpers.resolveProjectPath(config.output),
|
21 | publicPath: config.cordova ? '' : config.base
|
22 | },
|
23 |
|
24 | optimization: {
|
25 | runtimeChunk: true,
|
26 | splitChunks: {
|
27 | cacheGroups: {
|
28 | vendor: {
|
29 | chunks: 'initial',
|
30 | name: 'vendor',
|
31 | test: /node_modules/,
|
32 | enforce: true
|
33 | }
|
34 | }
|
35 | }
|
36 | },
|
37 |
|
38 | module: {
|
39 | rules: [
|
40 | {
|
41 | test: /\.css$/,
|
42 | loaders: ['vue-style-loader', 'css-loader']
|
43 | },
|
44 | {
|
45 | test: /\.js$/,
|
46 | loader: 'babel-loader',
|
47 | exclude: /node_modules/,
|
48 | options: {
|
49 | presets: ['env', 'vue-app']
|
50 | }
|
51 | },
|
52 | {
|
53 | test: /\.js$/,
|
54 | loader: 'string-replace-loader',
|
55 | exclude: /node_modules/,
|
56 | query: {
|
57 | multiple: config.replace
|
58 | }
|
59 | },
|
60 | {
|
61 | test: /\.vue$/,
|
62 | loader: 'vue-loader'
|
63 | },
|
64 | {
|
65 | test: /\.js$/,
|
66 | include: /workers/,
|
67 | loader: 'worker-loader'
|
68 | },
|
69 | {
|
70 | test: /\.(mp3|ogg|webm)$/,
|
71 | loader: 'file-loader',
|
72 | exclude: /node_modules/,
|
73 | options: {
|
74 | name: 'audio/[name].[hash:7].[ext]'
|
75 | }
|
76 | },
|
77 | {
|
78 | test: /\.(gif|ico|jpe?g|png|svg)$/,
|
79 | loader: 'file-loader',
|
80 | options: {
|
81 | name: 'images/[name].[hash:7].[ext]'
|
82 | }
|
83 | },
|
84 | {
|
85 | test: /\.(eot|otf|ttf|woff2?)$/,
|
86 | loader: 'file-loader',
|
87 | options: {
|
88 | name: 'fonts/[name].[hash:7].[ext]'
|
89 | }
|
90 | },
|
91 | {
|
92 | test: [/\.(frag|vert?)$/],
|
93 | loader: 'raw-loader'
|
94 | }
|
95 | ]
|
96 | },
|
97 |
|
98 | performance: {
|
99 | hints: false
|
100 | },
|
101 |
|
102 | plugins: [
|
103 | new VueLoaderPlugin(),
|
104 | new CopyWebpackPlugin(lodash.map(config.static, entry => {
|
105 | if (entry.from) entry.from = helpers.resolveProjectPath(entry.from)
|
106 |
|
107 | return entry
|
108 | })),
|
109 | new FriendlyErrorsWebpackPlugin({
|
110 | compilationSuccessInfo: {
|
111 | messages: ['URL: http://localhost:' + config.port]
|
112 | }
|
113 | }),
|
114 | new ProgressBarWebpackPlugin({
|
115 | format: chalk.blue('Building') + ' [:percent :bar]',
|
116 | summary: false
|
117 | })
|
118 | ],
|
119 |
|
120 | resolve: {
|
121 | alias: helpers.mergeAlias({
|
122 | '~': helpers.resolveProjectPath(),
|
123 | 'vue$': 'vue/dist/vue.esm.js'
|
124 | }, config.alias),
|
125 | extensions: ['.css', '.js', '.json', '.vue'],
|
126 | modules: [
|
127 | helpers.resolveLibraryPath('node_modules'),
|
128 | helpers.resolveProjectPath('node_modules')
|
129 | ]
|
130 | },
|
131 |
|
132 | resolveLoader: {
|
133 | modules: [
|
134 | helpers.resolveLibraryPath('node_modules'),
|
135 | helpers.resolveProjectPath('node_modules')
|
136 | ]
|
137 | }
|
138 | }
|
139 |
|
140 | lodash.forEach(config.html, entry => {
|
141 | webpackConfig.plugins.push(
|
142 | new HtmlWebpackPlugin({
|
143 | filename: entry.filename,
|
144 | template: helpers.resolveProjectPath(entry.template)
|
145 | })
|
146 | )
|
147 | })
|
148 |
|
149 | if (config.define) {
|
150 | webpackConfig.plugins.push(new webpack.DefinePlugin(config.define))
|
151 | }
|
152 |
|
153 | if (lodash.intersection(config.plugins, ['coffee']).length > 0) {
|
154 | webpackConfig = require('./lib/plugins/coffee.js')(webpackConfig)
|
155 | }
|
156 |
|
157 | if (lodash.intersection(config.plugins, ['less']).length > 0) {
|
158 | webpackConfig = require('./lib/plugins/less.js')(webpackConfig)
|
159 | }
|
160 |
|
161 | if (lodash.intersection(config.plugins, ['jade', 'pug']).length > 0) {
|
162 | webpackConfig = require('./lib/plugins/pug.js')(webpackConfig)
|
163 | }
|
164 |
|
165 | if (lodash.intersection(config.plugins, ['sass', 'scss']).length > 0) {
|
166 | webpackConfig = require('./lib/plugins/sass.js')(webpackConfig)
|
167 | }
|
168 |
|
169 | if (lodash.intersection(config.plugins, ['styl', 'stylus']).length > 0) {
|
170 | webpackConfig = require('./lib/plugins/stylus.js')(webpackConfig)
|
171 | }
|
172 |
|
173 | module.exports = webpackConfig
|