Skip to content

Commit ba3d91e

Browse files
author
lfyfly
committed
add: cfg.commonCss
1 parent b54a104 commit ba3d91e

File tree

8 files changed

+43
-8
lines changed

8 files changed

+43
-8
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,14 @@ npm run sp
8282
- pages多级目录支持(忽略下划线开头的文件和文件夹)
8383
- 当配置文件`webpack.cfg.js``build.assetsPublicPath === './' `,二级目录以上页面需要在页面中,增加`<base>`标签进行修正相对路径。如`src/pages/test/test-0.html`中的`<head>`中的`<base href="../" />`
8484
- 当配置文件`webpack.cfg.js``build.assetsPublicPath === '/' `, 则路径为绝对路径,无需修正路径
85+
86+
- 页面共有css文件入口支持
87+
```
88+
commonCss:{
89+
entry: path.resolve(__dirname,'src/common_css_entry.js'), // String 必填,入口文件,绝对地址
90+
exclude:['about'] // Arrary 排除页面,不填所有页面都引入common_css
91+
// ['about'] 代表about页面不引用 common_css
92+
},
93+
```
8594
## gulp 多页面配置
8695
[gulp-easy](https://github.com/lfyfly/gulp-easy)

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-multi-page",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "webpack webpack4 multi-page multiPage multiplePage",
55
"main": "index.js",
66
"scripts": {
@@ -11,7 +11,7 @@
1111
"webp": "gulp webp"
1212
},
1313
"author": "",
14-
"license": "ISC",
14+
"license": "MIT",
1515
"devDependencies": {
1616
"autoprefixer": "^9.0.1",
1717
"babel-core": "^6.26.3",

src/common_css_entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// 只因用样式文件
2+
import '@/css/_common/common.scss'

src/css/_common/common.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: $global-color;
3+
}

src/css/index/index.scss

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
$color: green;
21
body {
3-
color: $color;
42
background: url(~@/assets/img/test.jpg);
53
img{
64
width: 100px;

webpack.cfg.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
const path = require('path')
12
module.exports = {
3+
commonCss:{
4+
entry: path.resolve(__dirname,'src/common_css_entry.js'), // String 必填,绝对地址
5+
exclude:['about'] // Arrary 排除页面,不填所有页面都引入common_css
6+
},
27
// px2rem:{
38
// remUni:100,
49
// remPrecision: 6

webpack.common.js

+6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
55
const VueLoaderPlugin = require('vue-loader/lib/plugin')
66
const { resolve, getEntries, getHtmlWebpackPlugins } = require('./webpack.until.js')
77
const cfg = require('./webpack.cfg.js')
8+
let otherEntries = {}
9+
// 公共css文件入口
10+
if (cfg.commonCss && cfg.commonCss.entry) {
11+
otherEntries.common_css = cfg.commonCss.entry
12+
}
813

914
module.exports = (env, argv) => {
1015
console.log(argv.mode, '========================')
1116
return {
1217
entry: {
18+
...otherEntries,
1319
...getEntries(argv)
1420
},
1521
resolve: { //导入的时候不用写拓展名

webpack.until.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ const resolve = function (_path) {
55
return path.resolve(__dirname, _path)
66
}
77
const cfg = require('./webpack.cfg')
8+
9+
// 判断当前页面是否包含CommonCssChunk
10+
function getCommonCssChunk(chunkName) {
11+
if (!cfg.commonCss) return []
12+
// 无commonCss.exclude,所有页面包含
13+
if(!cfg.commonCss.exclude) return 'common_css'
14+
// 有commonCss.exclude,不包含在该数组的页面引用
15+
if(cfg.commonCss.exclude && !cfg.commonCss.exclude.includes(chunkName)) return 'common_css'
16+
// 其他
17+
return []
18+
}
19+
820
let until = {
921
resolve,
1022
getFileList(targetPath) {
@@ -64,13 +76,13 @@ let until = {
6476
var reg = /\.[^.]+$/
6577
if (reg.test(file.filename)) {
6678
chunkName = file.filename.replace(reg, '')
67-
console.log('.' + file.filepath.replace(targetPath, '').replace(reg,'.html'))
79+
console.log('.' + file.filepath.replace(targetPath, '').replace(reg, '.html'))
6880
HtmlWebpackPlugins.push(
6981
new HtmlWebpackPlugin({
70-
baseTagUrl:'../',
82+
baseTagUrl: '../',
7183
template: file.filepath,
72-
filename:'.' + file.filepath.replace(targetPath, '').replace(reg,'.html'),
73-
chunks: [chunkName].concat(argv.mode === 'production' ? ['vendor', 'commons', 'manifest'] : []),
84+
filename: '.' + file.filepath.replace(targetPath, '').replace(reg, '.html'),
85+
chunks: [chunkName].concat(getCommonCssChunk(chunkName)).concat(argv.mode === 'production' ? ['vendor', 'commons', 'manifest'] : []),
7486
inject: true,
7587
minify: argv.mode !== 'production' ? undefined : {
7688
removeComments: true,

0 commit comments

Comments
 (0)