Skip to content

Commit

Permalink
feat: export createCSSRule in chainWebpack for implementing sass, sty…
Browse files Browse the repository at this point in the history
…lus, ...
  • Loading branch information
sorrycc committed Mar 9, 2020
1 parent 68506de commit b0b3694
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/config/README.md
Expand Up @@ -91,7 +91,7 @@ Umi 内置了以下别名:

```js
export default {
chainWebpack(memo, { env, webpack }) {
chainWebpack(memo, { env, webpack, createCSSRule }) {
// 设置 alias
memo.resolve.alias.set('foo', '/tmp/a/b/foo');

Expand All @@ -108,6 +108,7 @@ export default {
* memo,当前 webpack-chain对象
* env,当前环境,`development``production``test`
* webpack,webpack 实例,用于获取其内部插件
* createCSSRule,用于扩展其他 CSS 实现,比如 sass, stylus

## chunks

Expand Down
3 changes: 2 additions & 1 deletion docs/config/README.zh-CN.md
Expand Up @@ -89,7 +89,7 @@ Umi 内置了以下别名:

```js
export default {
chainWebpack(memo, { env, webpack }) {
chainWebpack(memo, { env, webpack, createCSSRule }) {
// 设置 alias
memo.resolve.alias.set('foo', '/tmp/a/b/foo');

Expand All @@ -106,6 +106,7 @@ export default {
* memo,当前 webpack-chain对象
* env,当前环境,`development``production``test`
* webpack,webpack 实例,用于获取其内部插件
* createCSSRule,用于扩展其他 CSS 实现,比如 sass, stylus

## chunks

Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/api.md
Expand Up @@ -262,7 +262,7 @@ api.addHTMLScript(() => {
比如:

```js
api.chainWebpack((config, { webpack }) => {
api.chainWebpack((config, { webpack, env, createCSSRule }) => {
// Set alias
config.resolve.alias.set('a', 'path/to/a');

Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/api.zh-CN.md
Expand Up @@ -262,7 +262,7 @@ api.addHTMLScript(() => {
比如:

```js
api.chainWebpack((config, { webpack }) => {
api.chainWebpack((config, { webpack, env, createCSSRule }) => {
// Set alias
config.resolve.alias.set('a', 'path/to/a');

Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/src/getConfig/css.ts
Expand Up @@ -21,7 +21,7 @@ interface ICreateCSSRuleOpts extends IOpts {
options?: object;
}

function createCSSRule({
export function createCSSRule({
webpackConfig,
config,
lang,
Expand Down
15 changes: 14 additions & 1 deletion packages/bundler-webpack/src/getConfig/getConfig.ts
Expand Up @@ -11,7 +11,7 @@ import {
getBabelPresetOpts,
getTargetsAndBrowsersList,
} from '@umijs/bundler-utils';
import css from './css';
import css, { createCSSRule } from './css';
import terserOptions from './terserOptions';
import { objToStringified } from './utils';

Expand Down Expand Up @@ -391,16 +391,29 @@ export default async function getConfig(
},
);

function createCSSRuleFn(opts: any) {
createCSSRule({
webpackConfig,
config,
isDev,
browserslist,
miniCSSExtractPluginLoaderPath,
...opts,
});
}

if (opts.chainWebpack) {
webpackConfig = await opts.chainWebpack(webpackConfig, {
webpack: bundleImplementor,
createCSSRule: createCSSRuleFn,
});
}
// 用户配置的 chainWebpack 优先级最高
if (config.chainWebpack) {
config.chainWebpack(webpackConfig, {
env,
webpack: bundleImplementor,
createCSSRule: createCSSRuleFn,
});
}
let ret = webpackConfig.toConfig() as defaultWebpack.Configuration;
Expand Down
23 changes: 21 additions & 2 deletions packages/types/index.d.ts
Expand Up @@ -65,6 +65,15 @@ export interface ITargets {
[key: string]: number | boolean;
}

interface ICreateCSSRule {
(opts: {
lang: string;
test: RegExp;
loader?: string;
options?: object;
}): void;
}

type IPresetOrPlugin = string | [string, any];
type env = 'development' | 'production';

Expand Down Expand Up @@ -155,7 +164,10 @@ export interface IApi extends PluginAPI {
(string | { name: string; headScript?: boolean })[],
{ route: IRoute }
>;
chainWebpack: IModify<WebpackChain, { webpack: typeof webpack }>;
chainWebpack: IModify<
WebpackChain,
{ webpack: typeof webpack; createCSSRule: ICreateCSSRule }
>;

// ApplyPluginType.add
addHTMLHeadScripts: IAdd<{ route?: IRoute }, IScriptConfig>;
Expand Down Expand Up @@ -206,7 +218,14 @@ export interface IConfig extends IConfigCore {
autoprefixer?: object;
base?: string;
chainWebpack?: {
(memo: WebpackChain, args: { webpack: typeof webpack; env: env }): void;
(
memo: WebpackChain,
args: {
webpack: typeof webpack;
env: env;
createCSSRule: ICreateCSSRule;
},
): void;
};
chunks?: string[];
cssLoader?: object;
Expand Down

0 comments on commit b0b3694

Please sign in to comment.