-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luoxue
committed
Dec 14, 2019
1 parent
14920e8
commit ddfca57
Showing
54 changed files
with
444 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.