1 |
|
2 |
|
3 |
|
4 |
|
5 | const webpack = require('webpack')
|
6 | const path = require('path')
|
7 | const fs = require('fs')
|
8 | const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
|
9 | const HtmlWebpackPlugin = require('html-webpack-plugin')
|
10 | const appConfig = require(path.join(process.cwd(), 'app.config'))
|
11 | const getVueLoaderConfig = require('./vue-loader.conf')
|
12 | const vendorsManifest = require(path.join(process.cwd(), 'vendors-manifest.json'))
|
13 | const vueLoaderPlugin = require('vue-loader/lib/plugin')
|
14 | const merge = require('webpack-merge')
|
15 |
|
16 |
|
17 | let manifest = ''
|
18 |
|
19 |
|
20 | const appVersion = new Date().getTime()
|
21 | const targetDir = path.join(process.cwd(), 'dist')
|
22 |
|
23 | function resolve(dir) {
|
24 | return path.resolve(process.cwd(), dir)
|
25 | }
|
26 |
|
27 |
|
28 | const favicon = path.join(process.cwd(), 'favicon.ico')
|
29 |
|
30 | module.exports = function (env) {
|
31 | const cordova = appConfig.enableCordova ? 'cordova/index.js' : ''
|
32 | if (env === 'prod' && appConfig.offlineCaching) {
|
33 | manifest = `manifest=sweet.mobile.manifest`
|
34 | fs.writeFileSync(path.resolve(targetDir, `sweet.mobile.manifest`), '')
|
35 | }
|
36 |
|
37 |
|
38 | const config = merge(appConfig.webpack, {
|
39 |
|
40 | entry: appConfig.webpack.entry,
|
41 |
|
42 | output: {
|
43 |
|
44 | filename: '[name].js',
|
45 |
|
46 | chunkFilename: '[id].js',
|
47 | },
|
48 |
|
49 | resolve: {
|
50 |
|
51 | extensions: ['.js', '.vue', '.json'],
|
52 |
|
53 | alias: {
|
54 | 'vue': 'vue/dist/vue.esm.js',
|
55 | '@': resolve('src'),
|
56 | '@cwd': resolve(''),
|
57 | }
|
58 | },
|
59 | module: {
|
60 | rules: [
|
61 | {
|
62 | test: /\.vue$/,
|
63 | use: [getVueLoaderConfig(env), 'eslint-loader'],
|
64 | },
|
65 |
|
66 | {
|
67 | test: /\.js$/,
|
68 | include: [
|
69 | resolve('src'),
|
70 | resolve('./node_modules/framework7-vue'),
|
71 | resolve('./node_modules/framework7'),
|
72 | resolve('./node_modules/template7'),
|
73 | resolve('./node_modules/dom7')
|
74 | ],
|
75 | loader: 'babel-loader',
|
76 | options: {
|
77 | presets: ['@babel/preset-env'],
|
78 | }
|
79 | },
|
80 |
|
81 | {
|
82 | test: /\.(js|jsx)$/,
|
83 | include: resolve('./node_modules/@sweetui'),
|
84 | loader: 'babel-loader',
|
85 | options: {
|
86 | presets: ['@babel/preset-env'],
|
87 | plugins: ["@babel/plugin-syntax-dynamic-import"]
|
88 | }
|
89 | },
|
90 | {
|
91 | test: /\.jsx$/,
|
92 | use: ['babel-loader', 'eslint-loader'],
|
93 | exclude: /node_modules/,
|
94 | },
|
95 |
|
96 | {
|
97 | test: /\.json$/,
|
98 | include: [resolve('src'), resolve('lib'), resolve('client')],
|
99 | exclude: /node_modules/,
|
100 | use: 'json-loader',
|
101 | },
|
102 | {
|
103 | test: /\.ico$/,
|
104 | use: [{
|
105 | loader: 'url-loader',
|
106 | options: {
|
107 | limit: 1,
|
108 | name: 'resources/[path][name].[hash:8].[ext]',
|
109 | },
|
110 | }],
|
111 | },
|
112 |
|
113 | {
|
114 | test: /\.(gif|jpg|jpeg|png|bmp|svg|woff|woff2|eot|ttf)(\?.*)?$/,
|
115 | use: [{
|
116 | loader: 'url-loader',
|
117 | options: {
|
118 |
|
119 | limit: 8912,
|
120 |
|
121 | name: 'resources/[path][name].[hash:8].[ext]',
|
122 | },
|
123 | }],
|
124 | },
|
125 | ],
|
126 | },
|
127 | plugins: [
|
128 |
|
129 | new CaseSensitivePathsPlugin(),
|
130 |
|
131 | new HtmlWebpackPlugin({
|
132 | appVersion,
|
133 | favicon,
|
134 | manifest,
|
135 | filename: 'index.html',
|
136 | cordova: cordova,
|
137 | dllSrc: `${(env === 'prod' ? '' : '/dist/')}${vendorsManifest.name}.dll.js`,
|
138 | template: path.join(process.cwd(), 'index.template.ejs'),
|
139 | inject: true,
|
140 | minify: {
|
141 | removeComments: true,
|
142 | collapseWhitespace: true,
|
143 | removeAttributeQuotes: true
|
144 | },
|
145 | }),
|
146 | new webpack.DllReferencePlugin({
|
147 | context: path.join(process.cwd()),
|
148 | manifest: path.join(process.cwd(), 'vendors-manifest.json'),
|
149 | }),
|
150 | new webpack.DefinePlugin({
|
151 | 'process.env': appConfig.definePlugin || {},
|
152 | }),
|
153 |
|
154 | new vueLoaderPlugin(),
|
155 | ],
|
156 | })
|
157 | return config
|
158 | }
|