Skip to content

Commit

Permalink
多页面应用调整:
Browse files Browse the repository at this point in the history
1.项目目录结构调整

2.webpack配置调整
  • Loading branch information
no1harm committed Oct 30, 2018
1 parent 7efc9da commit 9cce969
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 25 deletions.
44 changes: 44 additions & 0 deletions build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,50 @@ const config = require('../config')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const packageConfig = require('../package.json')

var glob = require('glob')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var PAGE_PATH = path.resolve(__dirname, '../src/pages')
var merge = require('webpack-merge')

//多入口配置
exports.entries = function() {
var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
var map = {}
entryFiles.forEach((filePath) => {
var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
map[filename] = filePath
})
return map
}

//多页面输出配置
exports.htmlPlugin = function() {
var entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
var arr = []
entryHtml.forEach((filePath) => {
var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.'))
var conf = {
template: filePath,
filename: filename + '.html',
chunks: [filename],
inject: true
}
if (process.env.NODE_ENV === 'production') {
conf = merge(conf, {
chunks: ['manifest', 'vendor', filename],
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency'
})
}
arr.push(new HtmlWebpackPlugin(conf))
})
return arr
}

exports.assetsPath = function (_path) {
const assetsSubDirectory = process.env.NODE_ENV === 'production'
? config.build.assetsSubDirectory
Expand Down
7 changes: 4 additions & 3 deletions build/webpack.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ function resolve (dir) {

module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
// entry: {
// app: './src/main.js'
// },
entry: utils.entries(),
output: {
path: config.build.assetsRoot,
filename: '[name].js',
Expand Down
14 changes: 7 additions & 7 deletions build/webpack.dev.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const merge = require('webpack-merge')
const path = require('path')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
// const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder')

Expand Down Expand Up @@ -52,11 +52,11 @@ const devWebpackConfig = merge(baseWebpackConfig, {
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
// inject: true
// }),
// copy custom static assets
new CopyWebpackPlugin([
{
Expand All @@ -65,7 +65,7 @@ const devWebpackConfig = merge(baseWebpackConfig, {
ignore: ['.*']
}
])
]
].concat(utils.htmlPlugin())
})

module.exports = new Promise((resolve, reject) => {
Expand Down
30 changes: 15 additions & 15 deletions build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ const webpackConfig = merge(baseWebpackConfig, {
// generate dist index.html with correct asset hash for caching.
// you can customize output by editing /index.html
// see https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
}),
// new HtmlWebpackPlugin({
// filename: config.build.index,
// template: 'index.html',
// inject: true,
// minify: {
// removeComments: true,
// collapseWhitespace: true,
// removeAttributeQuotes: true
// // more options:
// // https://github.com/kangax/html-minifier#options-quick-reference
// },
// // necessary to consistently work with multiple chunks via CommonsChunkPlugin
// chunksSortMode: 'dependency'
// }),
// keep module.id stable when vendor modules does not change
new webpack.HashedModuleIdsPlugin(),
// enable scope hoisting
Expand Down Expand Up @@ -116,7 +116,7 @@ const webpackConfig = merge(baseWebpackConfig, {
ignore: ['.*']
}
])
]
].concat(utils.htmlPlugin())
})

if (config.build.productionGzip) {
Expand Down
File renamed without changes.
File renamed without changes
12 changes: 12 additions & 0 deletions src/pages/index/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>youzan-project</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
File renamed without changes.
File renamed without changes.

0 comments on commit 9cce969

Please sign in to comment.