Skip to content

Commit d73a211

Browse files
committed
处理编译问题
1 parent be2490d commit d73a211

File tree

7 files changed

+148
-88
lines changed

7 files changed

+148
-88
lines changed

build/build.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://github.com/shelljs/shelljs
22
require('./check-versions')()
33
require('shelljs/global')
4-
process.env.NODE_ENV = 'production'
4+
env.NODE_ENV = 'production'
55

66
var path = require('path')
77
var config = require('../config')
@@ -21,7 +21,8 @@ spinner.start()
2121
var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
2222
rm('-rf', assetsPath)
2323
mkdir('-p', assetsPath)
24-
cp('-R', 'static/*', assetsPath)
24+
cp('-R', 'public/*', assetsPath)
25+
2526

2627
webpack(webpackConfig, function (err, stats) {
2728
spinner.stop()

build/dev-server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require('./check-versions')()
22
var config = require('../config')
3-
if (!process.env.NODE_ENV) process.env.NODE_ENV = config.dev.env
3+
if (!process.env.NODE_ENV) process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
44
var path = require('path')
55
var express = require('express')
66
var webpack = require('webpack')
@@ -38,11 +38,10 @@ compiler.plugin('compilation', function (compilation) {
3838
Object.keys(proxyTable).forEach(function (context) {
3939
var options = proxyTable[context]
4040
if (typeof options === 'string') {
41-
options = { target: options }
41+
options = { target: options, changeOrigin: true }
4242
}
4343
app.use(proxyMiddleware(context, options))
4444
})
45-
4645
// handle fallback for HTML5 history API
4746
app.use(require('connect-history-api-fallback')())
4847

@@ -54,8 +53,8 @@ app.use(devMiddleware)
5453
app.use(hotMiddleware)
5554

5655
// serve pure static assets
57-
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
58-
app.use(staticPath, express.static('./static'))
56+
var staticPath = path.posix.join(config.build.assetsPublicPath, config.build.assetsSubDirectory)
57+
app.use(staticPath, express.static('./public'))
5958

6059
module.exports = app.listen(port, function (err) {
6160
if (err) {

build/utils.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ var config = require('../config')
33
var ExtractTextPlugin = require('extract-text-webpack-plugin')
44

55
exports.assetsPath = function (_path) {
6-
var assetsSubDirectory = process.env.NODE_ENV === 'production'
7-
? config.build.assetsSubDirectory
8-
: config.dev.assetsSubDirectory
6+
var assetsSubDirectory = config.build.assetsSubDirectory
97
return path.posix.join(assetsSubDirectory, _path)
108
}
119

@@ -36,13 +34,13 @@ exports.cssLoaders = function (options) {
3634

3735
// http://vuejs.github.io/vue-loader/en/configurations/extract-css.html
3836
return {
39-
css: generateLoaders(['css?-autoprefixer']),
40-
postcss: generateLoaders(['css?-autoprefixer']),
41-
less: generateLoaders(['css?-autoprefixer', 'less']),
42-
sass: generateLoaders(['css?-autoprefixer', 'sass?indentedSyntax']),
43-
scss: generateLoaders(['css?-autoprefixer', 'sass']),
44-
stylus: generateLoaders(['css?-autoprefixer', 'stylus']),
45-
styl: generateLoaders(['css?-autoprefixer', 'stylus'])
37+
css: generateLoaders(['css']),
38+
postcss: generateLoaders(['css']),
39+
less: generateLoaders(['css', 'less']),
40+
sass: generateLoaders(['css', 'sass?indentedSyntax']),
41+
scss: generateLoaders(['css', 'sass']),
42+
stylus: generateLoaders(['css', 'stylus']),
43+
styl: generateLoaders(['css', 'stylus'])
4644
}
4745
}
4846

build/webpack.base.conf.js

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,39 @@ var path = require('path')
22
var config = require('../config')
33
var utils = require('./utils')
44
var projectRoot = path.resolve(__dirname, '../')
5-
var webpack = require('webpack')
6-
var ExtractTextPlugin = require('extract-text-webpack-plugin')
75

86
var env = process.env.NODE_ENV
7+
// check env & config/index.js to decide weither to enable CSS Sourcemaps for the
8+
// various preprocessor loaders added to vue-loader at the end of this file
9+
var cssSourceMapDev = (env === 'development' && config.dev.cssSourceMap)
10+
var cssSourceMapProd = (env === 'production' && config.build.productionSourceMap)
11+
var useCssSourceMap = cssSourceMapDev || cssSourceMapProd
912

1013
module.exports = {
1114
entry: {
1215
app: './src/main.js'
1316
},
1417
output: {
1518
path: config.build.assetsRoot,
16-
publicPath: env === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
19+
publicPath: config.build.assetsPublicPath,
1720
filename: '[name].js'
1821
},
1922
resolve: {
20-
// require 时省略的扩展名,如:require('module') 不需要 module.js
21-
extensions: ['', '.js', '.vue', '.scss', '.css'],
23+
extensions: ['', '.js', '.vue'],
2224
fallback: [path.join(__dirname, '../node_modules')],
2325
alias: {
24-
vue: path.join(__dirname, '../node_modules/vue/dist/vue'),
26+
'vue$': 'vue/dist/vue',
2527
'src': path.resolve(__dirname, '../src'),
2628
'assets': path.resolve(__dirname, '../src/assets'),
27-
'components': path.resolve(__dirname, '../src/components'),
28-
'filter': path.resolve(__dirname, '../src/filters.js')
29+
'components': path.resolve(__dirname, '../src/components')
2930
}
3031
},
3132
resolveLoader: {
3233
fallback: [path.join(__dirname, '../node_modules')]
3334
},
3435
module: {
35-
preLoaders: [{
36+
preLoaders: [
37+
{
3638
test: /\.vue$/,
3739
loader: 'eslint',
3840
include: projectRoot,
@@ -45,7 +47,8 @@ module.exports = {
4547
exclude: /node_modules/
4648
}
4749
],
48-
loaders: [{
50+
loaders: [
51+
{
4952
test: /\.vue$/,
5053
loader: 'vue'
5154
},
@@ -60,39 +63,32 @@ module.exports = {
6063
loader: 'json'
6164
},
6265
{
63-
// 文件加载器,处理文件静态资源
64-
test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
65-
loader: 'file-loader?name=./fonts/[hash].[ext]'
66-
}, {
67-
// edit this for additional asset file types
68-
test: /\.(png|jpg|gif)$/,
69-
loader: 'file-loader?name=images/[hash].[ext]'
70-
}, {
71-
test: /\.css$/,
72-
loader: ExtractTextPlugin.extract('style', 'css!autoprefixer!sass')
73-
}, {
74-
test: /\.scss$/,
75-
loader: ExtractTextPlugin.extract('style', 'css!autoprefixer!sass')
66+
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
67+
loader: 'url',
68+
query: {
69+
limit: 10000,
70+
name: utils.assetsPath('img/[name].[hash:7].[ext]')
71+
}
72+
},
73+
{
74+
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
75+
loader: 'url',
76+
query: {
77+
limit: 10000,
78+
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
79+
}
7680
}
7781
]
7882
},
7983
eslint: {
8084
formatter: require('eslint-friendly-formatter')
8185
},
8286
vue: {
83-
loaders: {
84-
css: ExtractTextPlugin.extract('css!autoprefixer'),
85-
scss: ExtractTextPlugin.extract('css!autoprefixer!sass')
86-
}
87-
},
88-
plugins: [
89-
new webpack.optimize.CommonsChunkPlugin({
90-
name: 'vendors', // 将公共模块提取,生成名为`vendors`的chunk
91-
minChunks: 3 // 提取至少3个模块共有的部分
92-
}),
93-
new ExtractTextPlugin('css/[name].css', {
94-
allChunks: true,
95-
disable: false
96-
})
97-
]
87+
loaders: utils.cssLoaders({ sourceMap: useCssSourceMap }),
88+
postcss: [
89+
require('autoprefixer')({
90+
browsers: ['last 2 versions']
91+
})
92+
]
93+
}
9894
}

build/webpack.dev.conf.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var config = require('../config')
2+
var path = require('path')
23
var webpack = require('webpack')
34
var merge = require('webpack-merge')
45
var utils = require('./utils')
@@ -11,20 +12,55 @@ Object.keys(baseWebpackConfig.entry).forEach(function (name) {
1112
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
1213
})
1314

14-
baseWebpackConfig.plugins = (baseWebpackConfig.plugins || []).concat([
15-
new webpack.DefinePlugin({
16-
'process.env': config.dev.env
17-
}),
18-
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
19-
new webpack.optimize.OccurenceOrderPlugin(),
20-
new webpack.HotModuleReplacementPlugin(),
21-
new webpack.NoErrorsPlugin(),
22-
// https://github.com/ampedandwired/html-webpack-plugin
23-
new HtmlWebpackPlugin({
24-
filename: 'index.html',
25-
template: 'index.html',
26-
inject: true
27-
})
28-
])
29-
30-
module.exports = baseWebpackConfig
15+
module.exports = merge(baseWebpackConfig, {
16+
module: {
17+
loaders: utils.styleLoaders({
18+
extract: true
19+
})
20+
},
21+
vue: {
22+
loaders: utils.cssLoaders({
23+
extract: true
24+
})
25+
},
26+
// eval-source-map is faster for development
27+
devtool: '#eval-source-map',
28+
plugins: [
29+
new webpack.DefinePlugin({
30+
'process.env': config.dev.env
31+
}),
32+
new ExtractTextPlugin("[name].[chunkhash].css", {
33+
allChunks: true,
34+
disable: false
35+
}),
36+
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
37+
new webpack.optimize.OccurenceOrderPlugin(),
38+
new webpack.HotModuleReplacementPlugin(),
39+
new webpack.NoErrorsPlugin(),
40+
// https://github.com/ampedandwired/html-webpack-plugin
41+
new HtmlWebpackPlugin({
42+
filename: 'index.html',
43+
template: 'index.html',
44+
inject: true,
45+
chunksSortMode: 'dependency'
46+
}),
47+
// split vendor js into its own file
48+
new webpack.optimize.CommonsChunkPlugin({
49+
name: 'vendor',
50+
minChunks: function (module, count) {
51+
// any required modules inside node_modules are extracted to vendor
52+
return (
53+
module.resource &&
54+
/\.js$/.test(module.resource) &&
55+
module.resource.indexOf(
56+
path.join(__dirname, '../node_modules')
57+
) === 0
58+
)
59+
}
60+
}),
61+
new webpack.optimize.CommonsChunkPlugin({
62+
name: 'manifest',
63+
chunks: ['vendor']
64+
})
65+
]
66+
})

build/webpack.prod.conf.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,40 @@ var merge = require('webpack-merge')
66
var baseWebpackConfig = require('./webpack.base.conf')
77
var ExtractTextPlugin = require('extract-text-webpack-plugin')
88
var HtmlWebpackPlugin = require('html-webpack-plugin')
9+
var env = config.build.env
910

1011
var webpackConfig = merge(baseWebpackConfig, {
12+
module: {
13+
loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true })
14+
},
15+
devtool: config.build.productionSourceMap ? '#source-map' : false,
16+
output: {
17+
path: config.build.assetsRoot,
18+
filename: utils.assetsPath('js/[name].[chunkhash].js'),
19+
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
20+
},
21+
vue: {
22+
loaders: utils.cssLoaders({
23+
sourceMap: config.build.productionSourceMap,
24+
extract: true
25+
})
26+
},
1127
plugins: [
1228
// http://vuejs.github.io/vue-loader/en/workflow/production.html
1329
new webpack.DefinePlugin({
14-
'process.env': 'production'
30+
'process.env': env
1531
}),
1632
new webpack.optimize.UglifyJsPlugin({
1733
compress: {
1834
warnings: false
1935
}
2036
}),
2137
new webpack.optimize.OccurenceOrderPlugin(),
38+
// extract css into its own file
39+
new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css'), {
40+
allChunks: true,
41+
disable: false
42+
}),
2243
// generate dist index.html with correct asset hash for caching.
2344
// you can customize output by editing /index.html
2445
// see https://github.com/ampedandwired/html-webpack-plugin
@@ -35,6 +56,26 @@ var webpackConfig = merge(baseWebpackConfig, {
3556
},
3657
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
3758
chunksSortMode: 'dependency'
59+
}),
60+
// split vendor js into its own file
61+
new webpack.optimize.CommonsChunkPlugin({
62+
name: 'vendor',
63+
minChunks: function (module, count) {
64+
// any required modules inside node_modules are extracted to vendor
65+
return (
66+
module.resource &&
67+
/\.js$/.test(module.resource) &&
68+
module.resource.indexOf(
69+
path.join(__dirname, '../node_modules')
70+
) === 0
71+
)
72+
}
73+
}),
74+
// extract webpack runtime and module manifest to its own file in order to
75+
// prevent vendor hash from being updated whenever app bundle is updated
76+
new webpack.optimize.CommonsChunkPlugin({
77+
name: 'manifest',
78+
chunks: ['vendor']
3879
})
3980
]
4081
})

config/index.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@ module.exports = {
66
env: require('./prod.env'),
77
index: path.resolve(__dirname, '../dist/index.html'),
88
assetsRoot: path.resolve(__dirname, '../dist'),
9-
assetsSubDirectory: 'static',
10-
assetsPublicPath: '',
9+
assetsSubDirectory: 'public',
10+
assetsPublicPath: '/',
1111
productionSourceMap: true,
12-
// Gzip off by default as many popular static hosts such as
13-
// Surge or Netlify already gzip all static assets for you.
14-
// Before setting to `true`, make sure to:
15-
// npm install --save-dev compression-webpack-plugin
1612
productionGzip: false,
1713
productionGzipExtensions: ['js', 'css']
1814
},
1915
dev: {
2016
env: require('./dev.env'),
2117
port: 8020,
22-
assetsSubDirectory: 'static',
23-
assetsPublicPath: '',
24-
proxyTable: {},
25-
// CSS Sourcemaps off by default because relative paths are "buggy"
26-
// with this option, according to the CSS-Loader README
27-
// (https://github.com/webpack/css-loader#sourcemaps)
28-
// In our experience, they generally work as expected,
29-
// just be aware of this issue when enabling this option.
30-
cssSourceMap: false
18+
proxyTable: {
19+
}
3120
}
3221
}

0 commit comments

Comments
 (0)