Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加对miniCssExtractLoaderOption的支持 #16026

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
defineConstants = {},
runtime = {},
env = {},

miniCssExtractLoaderOption = {},
cssLoaderOption = {},
sassLoaderOption = {},
lessLoaderOption = {},
Expand Down Expand Up @@ -239,7 +241,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
deviceRatio,
enableSourceMap,
compile: config.compile || {},

miniCssExtractLoaderOption,
cssLoaderOption,
lessLoaderOption,
sassLoaderOption,
Expand Down
8 changes: 5 additions & 3 deletions packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ export const getMiniXScriptLoader = pipe(mergeOption, partial(getLoader, path.re
export const getMiniTemplateLoader = pipe(mergeOption, partial(getLoader, path.resolve(__dirname, '../loaders/miniTemplateLoader')))
export const getResolveUrlLoader = pipe(mergeOption, partial(getLoader, 'resolve-url-loader'))

const getExtractCssLoader = () => {
const getExtractCssLoader = (miniCssExtractLoaderOption = {}) => {
return {
loader: MiniCssExtractPlugin.loader
loader: MiniCssExtractPlugin.loader,
options: miniCssExtractLoaderOption
}
}
export const getMiniCssExtractPlugin = pipe(mergeOption, listify, partial(getPlugin, MiniCssExtractPlugin))
Expand Down Expand Up @@ -215,6 +216,7 @@ export const getModule = (appPath: string, {
lessLoaderOption,
sassLoaderOption,
stylusLoaderOption,
miniCssExtractLoaderOption,
fontUrlLoaderOption,
imageUrlLoaderOption,
mediaUrlLoaderOption,
Expand Down Expand Up @@ -252,7 +254,7 @@ export const getModule = (appPath: string, {
),
cssLoaderOption
]
const extractCssLoader = getExtractCssLoader()
const extractCssLoader = getExtractCssLoader(miniCssExtractLoaderOption)
const miniTemplateLoader = getMiniTemplateLoader([{
buildAdapter
}])
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-webpack-runner/src/config/dev.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
defineConstants = {},
env = {},
styleLoaderOption = {},
miniCssExtractLoaderOption = {},
cssLoaderOption = {},
sassLoaderOption = {},
lessLoaderOption = {},
Expand Down Expand Up @@ -74,6 +75,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
enableSourceMap,

styleLoaderOption,
miniCssExtractLoaderOption,
cssLoaderOption,
lessLoaderOption,
sassLoaderOption,
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-webpack-runner/src/config/prod.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
defineConstants = {},
env = {},
styleLoaderOption = {},
miniCssExtractLoaderOption = {},
cssLoaderOption = {},
sassLoaderOption = {},
lessLoaderOption = {},
Expand Down Expand Up @@ -77,7 +78,7 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
deviceRatio,
enableExtract,
enableSourceMap,

miniCssExtractLoaderOption,
styleLoaderOption,
cssLoaderOption,
lessLoaderOption,
Expand Down
9 changes: 5 additions & 4 deletions packages/taro-webpack-runner/src/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ const getUrlLoader = pipe(
mergeOption,
partial(getLoader, 'url-loader')
)
const getExtractCssLoader = () => {
const getExtractCssLoader = (miniCssExtractLoaderOption = {}) => {
return {
loader: MiniCssExtractPlugin.loader
loader: MiniCssExtractPlugin.loader,
options: miniCssExtractLoaderOption
}
}
const getImportMetaLoader = pipe(
Expand Down Expand Up @@ -217,7 +218,7 @@ export const parseModule = (appPath: string, {
deviceRatio,
enableExtract,
enableSourceMap,

miniCssExtractLoaderOption,
styleLoaderOption,
cssLoaderOption,
lessLoaderOption,
Expand Down Expand Up @@ -312,7 +313,7 @@ export const parseModule = (appPath: string, {
}
}, styleLoaderOption])

const extractCssLoader = getExtractCssLoader()
const extractCssLoader = getExtractCssLoader(miniCssExtractLoaderOption)

const lastStyleLoader = enableExtract ? extractCssLoader : styleLoader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6269,7 +6269,7 @@ require("./runtime");
"use strict";

(wx["webpackJsonp"] = wx["webpackJsonp"] || []).push([ [ 539 ], {
309: function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) {
169: function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) {
var taro_runtime = __webpack_require__(521);
var render = function render() {
var _vm = this, _c = _vm._self._c;
Expand Down Expand Up @@ -6355,7 +6355,7 @@ require("./runtime");
return __webpack_require__(__webpack_require__.s = moduleId);
};
__webpack_require__.O(0, [ 592 ], (function() {
return __webpack_exec__(309);
return __webpack_exec__(169);
}));
var __webpack_exports__ = __webpack_require__.O();
} ]);
Expand Down Expand Up @@ -8106,7 +8106,7 @@ require("./runtime");
"use strict";

(wx["webpackJsonp"] = wx["webpackJsonp"] || []).push([ [ 539 ], {
75: function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) {
356: function(__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) {
var taro_runtime = __webpack_require__(521);
var render = function render() {
var _vm = this, _c = _vm._self._c;
Expand Down Expand Up @@ -8194,7 +8194,7 @@ require("./runtime");
return __webpack_require__(__webpack_require__.s = moduleId);
};
__webpack_require__.O(0, [ 592 ], (function() {
return __webpack_exec__(75);
return __webpack_exec__(356);
}));
var __webpack_exports__ = __webpack_require__.O();
} ]);
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-webpack5-runner/src/webpack/H5WebpackModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class H5WebpackModule {
getModules () {
const {
styleLoaderOption = {},
miniCssExtractLoaderOption = {},
sassLoaderOption = {},
lessLoaderOption = {},
stylusLoaderOption = {},
Expand All @@ -63,7 +64,7 @@ export class H5WebpackModule {

const rule: Record<string, IRule> = {
taroStyle: this.getTaroStyleRule(styleLoaderOption),
customStyle: this.getCustomStyleRule(styleLoaderOption),
customStyle: this.getCustomStyleRule(styleLoaderOption, miniCssExtractLoaderOption),
css: {
test: REG_STYLE,
oneOf: this.getCSSLoaders(cssModuleOption)
Expand Down Expand Up @@ -146,12 +147,12 @@ export class H5WebpackModule {
}

/** 开发者的样式在尾部注入 */
getCustomStyleRule (styleLoaderOption) {
getCustomStyleRule (styleLoaderOption, miniCssExtractLoaderOption) {
const {
mode,
enableExtract = mode === 'production'
} = this.combination.config
const extractCssLoader = WebpackModule.getExtractCSSLoader()
const extractCssLoader = WebpackModule.getExtractCSSLoader(miniCssExtractLoaderOption)
const styleLoader = WebpackModule.getStyleLoader(styleLoaderOption)
const lastStyleLoader = enableExtract ? extractCssLoader : styleLoader
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class MiniWebpackModule {
sassLoaderOption,
lessLoaderOption,
stylusLoaderOption,
miniCssExtractLoaderOption,
designWidth,
deviceRatio
} = config
Expand All @@ -56,7 +57,7 @@ export class MiniWebpackModule {

const postcssPlugins = getPostcssPlugins(appPath, this.__postcssOption)

const cssLoaders = this.getCSSLoaders(postcssPlugins, cssModuleOption)
const cssLoaders = this.getCSSLoaders(postcssPlugins, cssModuleOption, miniCssExtractLoaderOption)
const resolveUrlLoader = WebpackModule.getResolveUrlLoader()
const sassLoader = WebpackModule.getSassLoader(sassLoaderOption)
const scssLoader = WebpackModule.getScssLoader(sassLoaderOption)
Expand Down Expand Up @@ -131,12 +132,12 @@ export class MiniWebpackModule {
return cssLoadersCopy
}

getCSSLoaders (postcssPlugins: any[], cssModuleOption: PostcssOption.cssModules) {
getCSSLoaders (postcssPlugins: any[], cssModuleOption: PostcssOption.cssModules, miniCssExtractLoaderOption) {
const { config } = this.combination
const {
cssLoaderOption
} = config
const extractCSSLoader = WebpackModule.getExtractCSSLoader()
const extractCSSLoader = WebpackModule.getExtractCSSLoader(miniCssExtractLoaderOption)
const cssLoader = WebpackModule.getCSSLoader(cssLoaderOption)
const postCSSLoader = WebpackModule.getPostCSSLoader({
postcssOptions: {
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-webpack5-runner/src/webpack/WebpackModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ export class WebpackModule {
return WebpackModule.getLoader('css-loader', options)
}

static getExtractCSSLoader () {
static getExtractCSSLoader (miniCssExtractLoaderOption = {}) {
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
return {
loader: MiniCssExtractPlugin.loader
loader: MiniCssExtractPlugin.loader,
options: miniCssExtractLoaderOption
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/taro/types/compile/config/h5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export interface IH5Config {
/** [style-loader](https://github.com/webpack-contrib/style-loader) 的附加配置 */
styleLoaderOption?: IOption

/** [MiniCssExtractPlugin.loader](https://github.com/webpack-contrib/mini-css-extract-plugin) 的附加配置 */
miniCssExtractLoaderOption?: IOption

/** [sass-loader](https://github.com/webpack-contrib/sass-loader) 的附加配置 */
sassLoaderOption?: IOption

Expand Down
4 changes: 4 additions & 0 deletions packages/taro/types/compile/config/mini.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export interface IMiniAppConfig {
/** [stylus-loader](https://github.com/shama/stylus-loader) 的附加配置 */
stylusLoaderOption?: IOption

/** [MiniCssExtractPlugin.loader](https://github.com/webpack-contrib/mini-css-extract-plugin) 的附加配置 */
miniCssExtractLoaderOption?: IOption


/** 针对 mp4 | webm | ogg | mp3 | wav | flac | aac 文件的 [url-loader](https://github.com/webpack-contrib/url-loader) 配置 */
mediaUrlLoaderOption?: IOption

Expand Down
Loading