1 | var path = require('path'),
|
2 | webpack = require('webpack'),
|
3 | HtmlWebpackPlugin = require('html-webpack-plugin'),
|
4 | BomPlugin = require('webpack-utf8-bom'),
|
5 | fs = require('fs'),
|
6 | ExtractTextPlugin = require('extract-text-webpack-plugin'),
|
7 | CleanWebpackPlugin = require('clean-webpack-plugin'),
|
8 | S3Plugin = require('webpack-s3-plugin'),
|
9 | WebpackMd5Hash = require('webpack-md5-hash')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | let port = 8080
|
16 |
|
17 | const buildEntry = function() {
|
18 | let entryPathes = [`./src/pages`]
|
19 | let ret = {}
|
20 | for (let entryPath of entryPathes) {
|
21 | let dir = path.join(__dirname, entryPath)
|
22 | let entries = fs.readdirSync(dir)
|
23 | entries.forEach(entry => {
|
24 | if (entry.charAt(0) === '.') return
|
25 | ret[path.basename(entry, '.js')] = `${entryPath}/${entry}`
|
26 | })
|
27 | }
|
28 | return ret
|
29 | }
|
30 |
|
31 | const buildHTML = function() {
|
32 | let dir = path.join(__dirname, `src/pages`)
|
33 | let entries = fs.readdirSync(dir)
|
34 | return entries
|
35 | .filter(entry => {
|
36 | return entry.charAt(0) !== '.'
|
37 | })
|
38 | .map(entry => {
|
39 | let basename = path.basename(entry, '.js')
|
40 | return new HtmlWebpackPlugin({
|
41 | filename: `${basename}.html`,
|
42 | template: `./src/templates/index.html`,
|
43 | chunks: ['vendor', basename],
|
44 | inject: 'true'
|
45 | })
|
46 | })
|
47 | }
|
48 |
|
49 | module.exports = {
|
50 | entry: Object.assign(
|
51 | {
|
52 | vendor: ['vue', 'vue-router', 'vuex', 'babel-polyfill']
|
53 | },
|
54 | buildEntry()
|
55 | ),
|
56 | output: {
|
57 | path: path.resolve(__dirname, 'dist'),
|
58 | publicPath: process.env.NODE_ENV == 'production' ? `//testquickb2c.s3.amazonaws.com/` : '/dist/',
|
59 | filename: process.env.NODE_ENV == 'production' ? '[name].[chunkhash:12].js' : '[name].js'
|
60 | },
|
61 | resolve: {
|
62 | extensions: ['.vue', '.js'],
|
63 | modules: ['node_modules'],
|
64 | alias: {
|
65 | vue: 'vue/dist/vue.min.js',
|
66 | components: __dirname + '/src/components/',
|
67 | assets: __dirname + '/src/assets/',
|
68 | stores: __dirname + '/src/stores/',
|
69 | utils: __dirname + '/src/utils'
|
70 | }
|
71 | },
|
72 | module: {
|
73 | rules: [
|
74 | {
|
75 | enforce: 'pre',
|
76 | test: /\.(js|vue)$/,
|
77 | exclude: /(node_modules|plugins|assets)/,
|
78 | loader: 'eslint-loader',
|
79 | options: {
|
80 | fix: false
|
81 | }
|
82 | },
|
83 | {
|
84 | test: /\.vue$/,
|
85 | loader: 'vue-loader',
|
86 | options: {
|
87 | loaders:
|
88 | process.env.NODE_ENV == 'production' ? { css: ExtractTextPlugin.extract({ use: 'css-loader', fallback: 'vue-style-loader' }) } : null
|
89 | }
|
90 | },
|
91 | {
|
92 | test: /\.js$/,
|
93 | loader: 'babel-loader',
|
94 | exclude: /node_modules/,
|
95 | options: {
|
96 | presets: ['es2015', 'stage-0'],
|
97 | plugins: ['transform-runtime']
|
98 | }
|
99 | },
|
100 | {
|
101 | test: /\.html$/,
|
102 | loader: 'vue-html-loader'
|
103 | },
|
104 | {
|
105 | test: /\.(png|jpg|gif|svg)$/,
|
106 | loader: 'url-loader',
|
107 | query: {
|
108 | limit: 15000,
|
109 | name: 'images/[name].[hash].[ext]'
|
110 | }
|
111 | }
|
112 | ]
|
113 | },
|
114 | devServer: {
|
115 | historyApiFallback: {
|
116 | index: `/dist/`
|
117 | },
|
118 | host: '0.0.0.0',
|
119 | port: port,
|
120 | disableHostCheck: true
|
121 | }
|
122 | }
|
123 |
|
124 | module.exports.plugins = [new CleanWebpackPlugin(['dist'])].concat(buildHTML()).concat([
|
125 | new webpack.optimize.CommonsChunkPlugin({
|
126 | name: 'vendor',
|
127 | filename: process.env.NODE_ENV === 'production' ? 'vendors.[chunkhash:12].js' : 'vendors.js',
|
128 | minChunks: function(module, count) {
|
129 | return false
|
130 | },
|
131 | minify: {
|
132 | removeComments: true,
|
133 | collapseWhitespace: false
|
134 | }
|
135 | }),
|
136 | new WebpackMd5Hash(),
|
137 | new webpack.LoaderOptionsPlugin({
|
138 | options: {
|
139 | vue: {
|
140 | autoprefixer: {
|
141 | browsers: ['> 5%']
|
142 | }
|
143 | }
|
144 | }
|
145 | }),
|
146 | new webpack.DefinePlugin({
|
147 | 'process.env': {
|
148 | NODE_ENV: `"${process.env.NODE_ENV}"`,
|
149 | APP_ID: `"${process.env.APP_ID}"`,
|
150 | APP_KEY: `"${process.env.APP_KEY}"`,
|
151 | API_BASE: `"${process.env.API_BASE}"`,
|
152 | MASTER_KEY: `"${process.env.MASTER_KEY}"`
|
153 | }
|
154 | }),
|
155 | new webpack.HashedModuleIdsPlugin()
|
156 | ])
|
157 | if (process.env.NODE_ENV === 'production') {
|
158 | module.exports.plugins = (module.exports.plugins || []).concat([
|
159 | new webpack.optimize.UglifyJsPlugin({
|
160 | compress: {
|
161 | warnings: false
|
162 | }
|
163 | }),
|
164 | new webpack.optimize.OccurrenceOrderPlugin(),
|
165 | new BomPlugin(true),
|
166 | new ExtractTextPlugin('[name].[contenthash:12].css'),
|
167 | new S3Plugin({
|
168 | include: /.*\.(css|js)$/,
|
169 | s3Options: {
|
170 | accessKeyId: 'AKIAIJIR4W2DBXGIJ2EA',
|
171 | secretAccessKey: '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'
|
172 | },
|
173 | s3UploadOptions: {
|
174 | Bucket: 'testquickb2c'
|
175 | }
|
176 | })
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | ])
|
188 | } else {
|
189 | module.exports.devtool = 'cheap-source-map'
|
190 | }
|