Skip to content

Commit 4697ae4

Browse files
committed
update build and config
1 parent f46dca8 commit 4697ae4

File tree

7 files changed

+87
-77
lines changed

7 files changed

+87
-77
lines changed

build/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var webpackConfig = require('./webpack.prod.conf')
1313
var spinner = ora('building for production...')
1414
spinner.start()
1515

16-
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
16+
rm(path.join(config.build.assetsRoot, '*'), err => {
1717
if (err) throw err
1818
webpack(webpackConfig, function (err, stats) {
1919
spinner.stop()

build/dev-server.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ var compiler = webpack(webpackConfig)
4545
Object.keys(proxyTable).forEach(function (context) {
4646
var options = proxyTable[context]
4747
if (typeof options === 'string') {
48-
options = {
49-
target: options
50-
}
48+
options = { target: options }
5149
}
5250
app.use(proxyMiddleware(options.filter || context, options))
5351
})
@@ -86,7 +84,7 @@ var readyPromise = new Promise(resolve => {
8684
module.exports = new Promise((resolve, reject) => {
8785
portfinder.basePort = port
8886
portfinder.getPortPromise()
89-
.then(newPort => {
87+
.then(newPort => {
9088
if (port !== newPort) {
9189
console.log(`${port}端口被占用,开启新端口${newPort}`)
9290
}
@@ -102,7 +100,7 @@ module.exports = new Promise((resolve, reject) => {
102100
server.close()
103101
}
104102
})
105-
}).catch(error => {
106-
console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error)
107-
})
103+
}).catch(error => {
104+
console.log('没有找到空闲端口,请打开任务管理器杀死进程端口再试', error)
105+
})
108106
})

build/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.cssLoaders = function (options) {
3737

3838
// generate loader string to be used with extract text plugin
3939
function generateLoaders (loader, loaderOptions) {
40-
var loaders = [cssLoader, postcssLoader, px2rpxLoader]
40+
var loaders = [cssLoader, px2rpxLoader, postcssLoader]
4141
if (loader) {
4242
loaders.push({
4343
loader: loader + '-loader',
@@ -62,6 +62,7 @@ exports.cssLoaders = function (options) {
6262
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
6363
return {
6464
css: generateLoaders(),
65+
wxss: generateLoaders(),
6566
postcss: generateLoaders(),
6667
less: generateLoaders('less'),
6768
sass: generateLoaders('sass', { indentedSyntax: true }),

build/webpack.base.conf.js

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,46 @@ var fs = require('fs')
33
var utils = require('./utils')
44
var config = require('../config')
55
var vueLoaderConfig = require('./vue-loader.conf')
6+
var MpvuePlugin = require('webpack-mpvue-asset-plugin')
7+
var glob = require('glob')
8+
var CopyWebpackPlugin = require('copy-webpack-plugin')
9+
var configFilesArray = []
10+
var relative = require('relative')
611

712
function resolve (dir) {
813
return path.join(__dirname, '..', dir)
914
}
1015

11-
function getEntry (dir, entryFile) {
12-
const files = fs.readdirSync(dir)
13-
return files.reduce((res, k) => {
14-
const page = path.resolve(dir, k, entryFile)
15-
if (fs.existsSync(page)) {
16-
res[k] = page
17-
}
18-
return res
19-
}, {})
16+
function getEntry (rootSrc) {
17+
var map = {};
18+
glob.sync(rootSrc + '/pages/**/main.js')
19+
.forEach(file => {
20+
var key = relative(rootSrc, file).replace('.js', '');
21+
map[key] = file;
22+
})
23+
glob.sync(rootSrc + '/pages/**/main.json')
24+
.forEach(file => {
25+
configFilesArray.push({
26+
from: file,
27+
to: relative(rootSrc, file)
28+
})
29+
})
30+
return map;
2031
}
2132

2233
const appEntry = { app: resolve('./src/main.js') }
23-
const pagesEntry = getEntry(resolve('./src/pages'), 'main.js')
34+
configFilesArray.push({
35+
from: resolve('./src/main.json'),
36+
to: 'app.json'
37+
})
38+
const pagesEntry = getEntry(resolve('./src'), 'pages/**/main.js')
2439
const entry = Object.assign({}, appEntry, pagesEntry)
2540

26-
process.noDeprecation = true
27-
2841
module.exports = {
29-
entry: entry,
42+
// 如果要自定义生成的 dist 目录里面的文件路径,
43+
// 可以将 entry 写成 {'toPath': 'fromPath'} 的形式,
44+
// toPath 为相对于 dist 的路径, 例:index/demo,则生成的文件地址为 dist/index/demo.js
45+
entry,
3046
target: require('mpvue-webpack-target'),
3147
output: {
3248
path: config.build.assetsRoot,
@@ -41,19 +57,21 @@ module.exports = {
4157
'vue': 'mpvue',
4258
'@': resolve('src')
4359
},
44-
symlinks: false
60+
symlinks: false,
61+
aliasFields: ['mpvue', 'weapp', 'browser'],
62+
mainFields: ['browser', 'module', 'main']
4563
},
4664
module: {
4765
rules: [
48-
// {
49-
// test: /\.(js|vue)$/,
50-
// loader: 'eslint-loader',
51-
// enforce: 'pre',
52-
// include: [resolve('src'), resolve('test')],
53-
// options: {
54-
// formatter: require('eslint-friendly-formatter')
55-
// }
56-
// },
66+
{
67+
test: /\.(js|vue)$/,
68+
loader: 'eslint-loader',
69+
enforce: 'pre',
70+
include: [resolve('src'), resolve('test')],
71+
options: {
72+
formatter: require('eslint-friendly-formatter')
73+
}
74+
},
5775
{
5876
test: /\.vue$/,
5977
loader: 'mpvue-loader',
@@ -85,7 +103,7 @@ module.exports = {
85103
loader: 'url-loader',
86104
options: {
87105
limit: 10000,
88-
name: utils.assetsPath('media/[name]].[ext]')
106+
name: utils.assetsPath('media/[name].[ext]')
89107
}
90108
},
91109
{
@@ -97,5 +115,16 @@ module.exports = {
97115
}
98116
}
99117
]
100-
}
118+
},
119+
plugins: [
120+
new MpvuePlugin(),
121+
new CopyWebpackPlugin(configFilesArray),
122+
new CopyWebpackPlugin([
123+
{
124+
from: path.resolve(__dirname, '../static'),
125+
to: path.resolve(__dirname, '../dist/static'),
126+
ignore: ['.*']
127+
}
128+
])
129+
]
101130
}

build/webpack.dev.conf.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ module.exports = merge(baseWebpackConfig, {
2929
devtool: '#source-map',
3030
output: {
3131
path: config.build.assetsRoot,
32-
// filename: utils.assetsPath('js/[name].[chunkhash].js'),
33-
// chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
34-
filename: utils.assetsPath('js/[name].js'),
35-
chunkFilename: utils.assetsPath('js/[id].js')
32+
// filename: utils.assetsPath('[name].[chunkhash].js'),
33+
// chunkFilename: utils.assetsPath('[id].[chunkhash].js')
34+
filename: utils.assetsPath('[name].js'),
35+
chunkFilename: utils.assetsPath('[id].js')
3636
},
3737
plugins: [
3838
new webpack.DefinePlugin({
@@ -42,8 +42,8 @@ module.exports = merge(baseWebpackConfig, {
4242
// copy from ./webpack.prod.conf.js
4343
// extract css into its own file
4444
new ExtractTextPlugin({
45-
// filename: utils.assetsPath('css/[name].[contenthash].css')
46-
filename: utils.assetsPath('css/[name].wxss')
45+
// filename: utils.assetsPath('[name].[contenthash].css')
46+
filename: utils.assetsPath('[name].wxss')
4747
}),
4848
// Compress extracted CSS. We are using this plugin so that possible
4949
// duplicated CSS from different components can be deduped.
@@ -53,28 +53,20 @@ module.exports = merge(baseWebpackConfig, {
5353
}
5454
}),
5555
new webpack.optimize.CommonsChunkPlugin({
56-
name: 'vendor',
56+
name: 'common/vendor',
5757
minChunks: function (module, count) {
5858
// any required modules inside node_modules are extracted to vendor
5959
return (
6060
module.resource &&
6161
/\.js$/.test(module.resource) &&
6262
module.resource.indexOf('node_modules') >= 0
63-
) || count >= 2
63+
) || count > 1
6464
}
6565
}),
6666
new webpack.optimize.CommonsChunkPlugin({
67-
name: 'manifest',
68-
chunks: ['vendor']
67+
name: 'common/manifest',
68+
chunks: ['common/vendor']
6969
}),
70-
// copy custom static assets
71-
new CopyWebpackPlugin([
72-
{
73-
from: path.resolve(__dirname, '../static'),
74-
to: config.build.assetsSubDirectory,
75-
ignore: ['.*']
76-
}
77-
]),
7870

7971
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
8072
// new webpack.HotModuleReplacementPlugin(),

build/webpack.prod.conf.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var webpack = require('webpack')
44
var config = require('../config')
55
var merge = require('webpack-merge')
66
var baseWebpackConfig = require('./webpack.base.conf')
7+
var UglifyJsPlugin = require('uglifyjs-webpack-plugin')
78
var CopyWebpackPlugin = require('copy-webpack-plugin')
89
// var HtmlWebpackPlugin = require('html-webpack-plugin')
910
var ExtractTextPlugin = require('extract-text-webpack-plugin')
@@ -21,26 +22,23 @@ var webpackConfig = merge(baseWebpackConfig, {
2122
devtool: config.build.productionSourceMap ? '#source-map' : false,
2223
output: {
2324
path: config.build.assetsRoot,
24-
// filename: utils.assetsPath('js/[name].[chunkhash].js'),
25-
// chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
26-
filename: utils.assetsPath('js/[name].js'),
27-
chunkFilename: utils.assetsPath('js/[id].js')
25+
// filename: utils.assetsPath('[name].[chunkhash].js'),
26+
// chunkFilename: utils.assetsPath('[id].[chunkhash].js')
27+
filename: utils.assetsPath('[name].js'),
28+
chunkFilename: utils.assetsPath('[id].js')
2829
},
2930
plugins: [
3031
// http://vuejs.github.io/vue-loader/en/workflow/production.html
3132
new webpack.DefinePlugin({
3233
'process.env': env
3334
}),
34-
new webpack.optimize.UglifyJsPlugin({
35-
compress: {
36-
warnings: false
37-
},
35+
new UglifyJsPlugin({
3836
sourceMap: true
3937
}),
4038
// extract css into its own file
4139
new ExtractTextPlugin({
42-
// filename: utils.assetsPath('css/[name].[contenthash].css')
43-
filename: utils.assetsPath('css/[name].wxss')
40+
// filename: utils.assetsPath('[name].[contenthash].css')
41+
filename: utils.assetsPath('[name].wxss')
4442
}),
4543
// Compress extracted CSS. We are using this plugin so that possible
4644
// duplicated CSS from different components can be deduped.
@@ -70,30 +68,22 @@ var webpackConfig = merge(baseWebpackConfig, {
7068
new webpack.HashedModuleIdsPlugin(),
7169
// split vendor js into its own file
7270
new webpack.optimize.CommonsChunkPlugin({
73-
name: 'vendor',
71+
name: 'common/vendor',
7472
minChunks: function (module, count) {
7573
// any required modules inside node_modules are extracted to vendor
7674
return (
7775
module.resource &&
7876
/\.js$/.test(module.resource) &&
7977
module.resource.indexOf('node_modules') >= 0
80-
) || count >= 2
78+
) || count > 1
8179
}
8280
}),
8381
// extract webpack runtime and module manifest to its own file in order to
8482
// prevent vendor hash from being updated whenever app bundle is updated
8583
new webpack.optimize.CommonsChunkPlugin({
86-
name: 'manifest',
87-
chunks: ['vendor']
88-
}),
89-
// copy custom static assets
90-
new CopyWebpackPlugin([
91-
{
92-
from: path.resolve(__dirname, '../static'),
93-
to: config.build.assetsSubDirectory,
94-
ignore: ['.*']
95-
}
96-
])
84+
name: 'common/manifest',
85+
chunks: ['common/vendor']
86+
})
9787
]
9888
})
9989

config/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
env: require('./prod.env'),
77
index: path.resolve(__dirname, '../dist/index.html'),
88
assetsRoot: path.resolve(__dirname, '../dist'),
9-
assetsSubDirectory: 'static',
9+
assetsSubDirectory: '',
1010
assetsPublicPath: '/',
1111
productionSourceMap: false,
1212
// Gzip off by default as many popular static hosts such as
@@ -26,7 +26,7 @@ module.exports = {
2626
port: 8080,
2727
// 在小程序开发者工具中不需要自动打开浏览器
2828
autoOpenBrowser: false,
29-
assetsSubDirectory: 'static',
29+
assetsSubDirectory: '',
3030
assetsPublicPath: '/',
3131
proxyTable: {},
3232
// CSS Sourcemaps off by default because relative paths are "buggy"

0 commit comments

Comments
 (0)