UNPKG

5.15 kBJavaScriptView Raw
1/**
2 * webpack base配置
3 * Created by liuzhengdong on 2018/4/3.
4 */
5const webpack = require('webpack')
6const path = require('path')
7const fs = require('fs')
8const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
9const HtmlWebpackPlugin = require('html-webpack-plugin')
10const appConfig = require(path.join(process.cwd(), 'app.config'))
11const getVueLoaderConfig = require('./vue-loader.conf')
12const vendorsManifest = require(path.join(process.cwd(), 'vendors-manifest.json'))
13const vueLoaderPlugin = require('vue-loader/lib/plugin')
14const merge = require('webpack-merge')
15
16// H5离线缓存manifest
17let manifest = ''
18
19// 版本号
20const appVersion = new Date().getTime()
21const targetDir = path.join(process.cwd(), 'dist')
22
23function resolve(dir) {
24 return path.resolve(process.cwd(), dir)
25}
26
27// 网站图标
28const favicon = path.join(process.cwd(), 'favicon.ico')
29
30module.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 // 合并app.config.js的 webpack
38 const config = merge(appConfig.webpack, {
39 // 入口模块配置
40 entry: appConfig.webpack.entry,
41 // 输出模块配置
42 output: {
43 // 生成的文件名, [name] 即为entry配置中的key
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 // 针对src和framework7-vue相关的npm包中的纯js进行编译
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 // 针对@sweetui下的所有模块进行编译
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 // 加载json文件
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 // gif|jpg|jpeg|png|bmp|svg|woff|woff2|eot|ttf这些模块使用url-loader加载
113 {
114 test: /\.(gif|jpg|jpeg|png|bmp|svg|woff|woff2|eot|ttf)(\?.*)?$/,
115 use: [{
116 loader: 'url-loader',
117 options: {
118 // 小于8912字节的文件,返回dataurl
119 limit: 8912,
120 // 生成的文件名,[name]为原始文件名,[hash:8]为根据文件内容生成8位md5值,[ext]为原始文件扩展名
121 name: 'resources/[path][name].[hash:8].[ext]',
122 },
123 }],
124 },
125 ],
126 },
127 plugins: [
128 // 由于mac不区分大小写,linux区分大小写,可能导致mac上正常,在部署时出错,所以强制区分大小写
129 new CaseSensitivePathsPlugin(),
130 // 读取HTML模板文件,并输出HTML文件,开发环境实际输出到内存中
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 // make sure to include the plugin for the magic
154 new vueLoaderPlugin(),
155 ],
156 })
157 return config
158}