Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
chore: onBuildStart
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Nov 13, 2020
1 parent 07582e7 commit eb6e913
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/remax-cli/src/API.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as t from '@babel/types';
import { Plugin, Meta, HostComponent, Platform } from '@remax/types';
import { Plugin, Meta, HostComponent, Platform, Options } from '@remax/types';
import { slash } from '@remax/shared';
import { merge } from 'lodash';
import Config from 'webpack-chain';
Expand Down Expand Up @@ -90,6 +90,14 @@ export default class API {
}, true);
}

onBuildStart(config: Options) {
this.plugins.forEach(plugin => {
if (typeof plugin.onBuildStart === 'function') {
plugin.onBuildStart({ config });
}
});
}

onAppConfig(config: any) {
return this.plugins.reduce((acc, plugin) => {
if (typeof plugin.onAppConfig === 'function') {
Expand Down
2 changes: 2 additions & 0 deletions packages/remax-cli/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import API from '../API';
const version = require('remax/package.json').version;

export function run(options: Options, api: API): webpack.Compiler {
api.onBuildStart(options);

if (options.target === Platform.web) {
// 兼容 herbox 所以用 require
const WebBuilder = require('./WebBuilder').default;
Expand Down
4 changes: 2 additions & 2 deletions packages/remax-cli/src/build/webpack/config.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default function webpackConfig(builder: Builder): webpack.Configuration {
builder,
});

cssConfig(config, builder.options, false);
cssConfig(config, builder, false);

config.module
.rule('image')
Expand Down Expand Up @@ -221,7 +221,7 @@ export default function webpackConfig(builder: Builder): webpack.Configuration {
config,
webpack,
addCSSRule: (ruleConfig: RuleConfig) => {
addCSSRule(config, builder.options, false, ruleConfig);
addCSSRule(config, builder, false, ruleConfig);
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/remax-cli/src/build/webpack/config.miniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default function webpackConfig(builder: Builder): webpack.Configuration {
builder,
});

cssConfig(config, builder.options, false);
cssConfig(config, builder, false);

config.module
.rule('image')
Expand Down
13 changes: 8 additions & 5 deletions packages/remax-cli/src/build/webpack/config/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Config from 'webpack-chain';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { Options } from '@remax/types';
import { slash } from '@remax/shared';
import Builder from '../../Builder';

export interface RuleConfig {
name: string;
Expand All @@ -20,7 +21,8 @@ function resolvePostcssConfig(options: Options) {
return slash(path.resolve(__dirname, '../../../..'));
}

export function addCSSRule(webpackConfig: Config, options: Options, web: boolean, ruleConfig: RuleConfig) {
export function addCSSRule(webpackConfig: Config, builder: Builder, web: boolean, ruleConfig: RuleConfig) {
const { options } = builder;
const rule = webpackConfig.module.rule(ruleConfig.name).test(ruleConfig.test);

function applyLoaders(rule: Config.Rule<Config.Rule<Config.Module>>, cssModules: boolean) {
Expand Down Expand Up @@ -76,16 +78,17 @@ export function addCSSRule(webpackConfig: Config, options: Options, web: boolean
applyLoaders(rule.oneOf('normal'), false);
}

export function cssConfig(webpackConfig: Config, options: Options, web: boolean) {
addCSSRule(webpackConfig, options, web, {
export function cssConfig(webpackConfig: Config, builder: Builder, web: boolean) {
addCSSRule(webpackConfig, builder, web, {
name: 'css',
test: /\.css(\?.*)?$/,
});

if (!web) {
const { style } = builder.api.getMeta();
webpackConfig.module
.rule('acss')
.test(/\.acss$/)
.rule(style)
.test(file => file.endsWith(style))
.use('mini-css-extract-loader')
.loader(MiniCssExtractPlugin.loader)
.end()
Expand Down
2 changes: 1 addition & 1 deletion packages/remax-cli/src/build/webpack/webBaseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function webBaseConfig(config: Config, builder: Builder) {
config.module.rule('js').exclude.add(/(node_modules)/);
}

cssConfig(config, builder.options, true);
cssConfig(config, builder, true);

config.module
.rule('image')
Expand Down
2 changes: 2 additions & 0 deletions packages/remax-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export interface Plugin {
*/
shouldHostComponentRegister?: (options: ShouldHostComponentRegister) => boolean;

onBuildStart?: (params: { config: Options }) => void;

/**
* 修改 webpack 配置
*/
Expand Down

0 comments on commit eb6e913

Please sign in to comment.