Skip to content

Commit

Permalink
feat(global-style): 设置全局样式
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxue committed Dec 14, 2019
1 parent 14920e8 commit ddfca57
Show file tree
Hide file tree
Showing 54 changed files with 444 additions and 148 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
alias | 配置别名 第13课时 | [804f932](https://github.com/luoxue-victor/learn_webpack/commit/804f932)
all | 初始化一个项目 | [336cc13](https://github.com/luoxue-victor/learn_webpack/commit/336cc13)
asdasdasdas | 你爱哈的 | [ea12f9c](https://github.com/luoxue-victor/learn_webpack/commit/ea12f9c)
assets | 加载资源 images、svg、media、fonts | [14920e8](https://github.com/luoxue-victor/learn_webpack/commit/14920e8)
auto readme | 自动生成 readme 文件 | [1e0a526](https://github.com/luoxue-victor/learn_webpack/commit/1e0a526)
autoprefixer | css 自动添加前缀 | [4e88451](https://github.com/luoxue-victor/learn_webpack/commit/4e88451)
case-sensitive=paths | 严格区分大小写 | [164b04d](https://github.com/luoxue-victor/learn_webpack/commit/164b04d)
Expand Down
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [课时 15:定义通用变量](./docs/课时-15.md)
- [课时 16:严格区分路径大小写](./docs/课时-16.md)
- [课时 17:加载资源 images、svg、media、fonts](./docs/课时-17.md)
- [课时 17:设置全局样式](./docs/课时-18.md)

</details>

Expand Down Expand Up @@ -109,6 +110,7 @@ webpack-box upgrade 5 # 可以切换到 webpack5/4
- [optimization 优化配置](./config/optimization.js)
- [样式表配置](./config/style.js)
- [stylelint 配置](./config/styleLintPlugin.js)
- [设置 style 全局变量](./config/styleResourcesLoader.js)
- [多线程配置](./config/threadLoader.js)
- [tslint 配置](./config/tslintPlugin.js)

Expand Down Expand Up @@ -138,14 +140,15 @@ module.exports = function (config) {
* @param {function} chainWebpack
* @param {string} entry 入口
* @param {string} output 出口
*
* @param {string} publicPath
* @param {string} port 端口
* @param {object} eslint eslint 配置
* @param {object} stylelint stylelint 配置
* @param {object} eslint eslint 配置
* @param {object} alias 配置别名
* @param {Boolean} filenameHashing 文件名是否使用 hash
* @param {object} env 配置通用变量,可以在 node 跟 web 之间共同使用
* @param {Boolean} filenameHashing 文件名是否使用 hash,当文件发生变动的时候 filename 才会改变
* @param {Boolean} css 配置 css
*/
return {
entry: 'src/main.js',
Expand All @@ -159,6 +162,34 @@ module.exports = function (config) {
'@': resolve('src'),
'@src': resolve('src')
},
resources: {
less: {
patterns: [
path.resolve(__dirname, './src/global/*.less')
]
},
scss: {
patterns: [
path.resolve(__dirname, './src/global/*.scss')
]
}
},
css: {
sourceMap: true,
loaderOptions: {
css: {},
less: {
globalVars: {
gray: '#ccc'
}
},
sass: {},
postcss: {},
stylus: {}
},
isCssModule: false, // 是否对css进行模块化处理
needInlineMinification: false // 是否需要压缩css
},
filenameHashing: true,
eslint: {
lintOnSave: true, // 开启运行时检测
Expand Down
33 changes: 31 additions & 2 deletions box.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ module.exports = function (config) {
* @param {function} chainWebpack
* @param {string} entry 入口
* @param {string} output 出口
*
* @param {string} publicPath
* @param {string} port 端口
* @param {object} eslint eslint 配置
* @param {object} stylelint stylelint 配置
* @param {object} eslint eslint 配置
* @param {object} alias 配置别名
* @param {Boolean} filenameHashing 文件名是否使用 hash
* @param {object} env 配置通用变量,可以在 node 跟 web 之间共同使用
* @param {Boolean} filenameHashing 文件名是否使用 hash,当文件发生变动的时候 filename 才会改变
* @param {Boolean} css 配置 css
*/
return {
entry: 'src/main.js',
Expand All @@ -31,6 +32,34 @@ module.exports = function (config) {
'@': resolve('src'),
'@src': resolve('src')
},
resources: {
less: {
patterns: [
path.resolve(__dirname, './src/global/*.less')
]
},
scss: {
patterns: [
path.resolve(__dirname, './src/global/*.scss')
]
}
},
css: {
sourceMap: true,
loaderOptions: {
css: {},
less: {
globalVars: {
gray: '#ccc'
}
},
sass: {},
postcss: {},
stylus: {}
},
isCssModule: false, // 是否对css进行模块化处理
needInlineMinification: false // 是否需要压缩css
},
filenameHashing: true,
eslint: {
lintOnSave: true, // 开启运行时检测
Expand Down
18 changes: 16 additions & 2 deletions config/MiniCssExtractPlugin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
// [mini-css-extract-plugin 配置]
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = ({ config }) => {
module.exports = ({ config, options = {} }) => {
const rootOptions = options
const getAssetPath = require('../util/getAssetPath')
const isProd = process.env.NODE_ENV === 'production'
return () => {
const {
extract = isProd
} = rootOptions.css || {}
const filename = getAssetPath(
rootOptions,
`css/[name]${rootOptions.filenameHashing ? '.[contenthash:8]' : ''}.css`
)
const extractOptions = Object.assign({
filename,
chunkFilename: filename
}, extract && typeof extract === 'object' ? extract : {})
config
.plugin('mini-css-extract')
.use(MiniCssExtractPlugin)
.use(MiniCssExtractPlugin, [extractOptions])
}
}
2 changes: 1 addition & 1 deletion config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = ({ config, webpackVersion, resolve, options }) => {
// 出口
.output
.path(resolve(output))
.filename('[name].bundle.js')
.filename(`js/${options.filenameHashing ? '[name].[contenthash:8]' : ''}.bundle.js`)
.publicPath(publicPath)

if (parseInt(webpackVersion) === 5) {
Expand Down
153 changes: 128 additions & 25 deletions config/style.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,139 @@
// [样式表配置]
module.exports = ({ config }) => {
module.exports = ({ config, options = {} }) => {
const rootOptions = options
const isProd = process.env.NODE_ENV === 'production'
const getAssetPath = require('../util/getAssetPath')
const {
sourceMap = false,
loaderOptions = {},
extract = true,
needInlineMinification = true,
isCssModule = false
} = rootOptions.css || {}

const createCSSRule = (lang, test, loader, options = {}) => {
const baseRule = config.module.rule(lang).test(test)
const normalRule = baseRule.oneOf('normal')
const cssnanoOptions = {
preset: ['default', {
mergeLonghand: false,
cssDeclarationSorter: false
}]
}
if (rootOptions.productionSourceMap && sourceMap) {
cssnanoOptions.map = { inline: false }
}

const filename = getAssetPath(
rootOptions,
`css/[name]${rootOptions.filenameHashing ? '.[contenthash:8]' : ''}.css`
)
const extractOptions = Object.assign({
filename,
chunkFilename: filename
}, extract && typeof extract === 'object' ? extract : {})

const cssPublicPath = '../'.repeat(
extractOptions.filename
.replace(/^\.[\/\\]/, '')
.split(/[\/\\]/g)
.length - 1
)

applyLoaders(normalRule, isCssModule)

function applyLoaders (rule, isCssModule) {
const cssLoaderOptions = Object.assign({
sourceMap
}, loaderOptions.css)
if (extract) {
rule
.use('extract-css-loader')
.loader(require('mini-css-extract-plugin').loader)
.options({
hmr: !isProd,
publicPath: cssPublicPath
})
} else {
// rule
// .use('vue-style-loader')
// .loader(require.resolve('vue-style-loader'))
// .options({
// sourceMap
// })
}
if (isCssModule) {
cssLoaderOptions.modules = {
localIdentName: '[name]_[local]_[hash:base64:5]',
...cssLoaderOptions.modules
}
} else {
delete cssLoaderOptions.modules
}

normalRule
.use('extract-css-loader')
.loader(require('mini-css-extract-plugin').loader)
.options({
hmr: process.env.NODE_ENV === 'development',
publicPath: '/'
})
normalRule
.use('css-loader')
.loader(require.resolve('css-loader'))
.options({})
normalRule
.use('postcss-loader')
.loader(require.resolve('postcss-loader'))
if (loader) {
const rs = require.resolve(loader)
normalRule
.use(loader)
.loader(rs)
.options(options)
rule
.use('css-loader')
.loader(require.resolve('css-loader'))
.options(cssLoaderOptions)

if (needInlineMinification) {
rule
.use('cssnano')
.loader(require.resolve('postcss-loader'))
.options({
sourceMap,
plugins: [require('cssnano')(cssnanoOptions)]
})
}

rule
.use('postcss-loader')
.loader(require.resolve('postcss-loader'))
.options(Object.assign({ sourceMap }, loaderOptions.postcss))

if (loader) {
let resolvedLoader
try {
resolvedLoader = require.resolve(loader)
} catch (error) {
resolvedLoader = loader
}
rule
.use(loader)
.loader(resolvedLoader)
.options(Object.assign({ sourceMap }, options))
}
}
}

const defaultSassLoaderOptions = {}
return () => {
createCSSRule('css', /\.css$/, 'css-loader', {})
createCSSRule('less', /\.less$/, 'less-loader', {})
createCSSRule('scss', /\.scss$/, 'sass-loader', {})
createCSSRule('css', /\.css$/, 'css-loader')
createCSSRule('postcss', /\.p(ost)?css$/)
createCSSRule('scss', /\.scss$/, 'sass-loader', Object.assign(
{},
defaultSassLoaderOptions,
loaderOptions.scss || loaderOptions.sass
))

createCSSRule('sass', /\.sass$/, 'sass-loader', Object.assign(
{},
defaultSassLoaderOptions,
loaderOptions.sass,
{
sassOptions: Object.assign(
{},
loaderOptions.sass && loaderOptions.sass.sassOptions,
{
indentedSyntax: true
}
)
}
))

createCSSRule('less', /\.less$/, 'less-loader', loaderOptions.less)

createCSSRule('stylus', /\.styl(us)?$/, 'stylus-loader', Object.assign({
preferPathResolver: 'webpack'
}, loaderOptions.stylus))
}
}
18 changes: 18 additions & 0 deletions config/styleResourcesLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// [设置 style 全局变量]
module.exports = ({ config, options }) => {
const resourcesOpt = options.resources
return () => {
[
'normal'
].forEach((oneOf) => {
Object.keys(resourcesOpt).forEach(loader => {
config.module.rule(loader).oneOf(oneOf)
.use('style-resources-loader')
.loader('style-resources-loader')
.options({
patterns: resourcesOpt[loader].patterns
})
})
})
}
}
Loading

0 comments on commit ddfca57

Please sign in to comment.