Skip to content

Commit

Permalink
feat(重写构建): 重写构建
Browse files Browse the repository at this point in the history
  • Loading branch information
winixt committed Nov 14, 2020
1 parent a49f328 commit 28498a3
Show file tree
Hide file tree
Showing 95 changed files with 3,623 additions and 2,620 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# http://editorconfig.org

root = true
lib

[*]
charset = utf-8
Expand Down
2 changes: 1 addition & 1 deletion .fatherrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'path';

// utils must build before core
// runtime must build before renderer-react
const headPkgs = ['fes-runtime', 'fes-core'];
const headPkgs = ['fes-runtime', 'fes-core', 'fes', 'fes-plugin-built-in'];
const tailPkgs = [];
// const otherPkgs = readdirSync(join(__dirname, 'packages')).filter(
// (pkg) =>
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions packages/fes-core/src/Config/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ export default class Config {
return {};
}

// @ts-ignore
// @ts-ignore
addAffix(file, affix) {
const ext = extname(file);
return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`);
Expand Down
3 changes: 2 additions & 1 deletion packages/fes-core/src/Service/PluginAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import * as utils from '@umijs/utils';
import { isValidPlugin, pathToObj } from './utils/pluginUtils';
import { EnableBy, PluginType, ServiceStage } from './enums';

import Logger from '../logger/logger';
// TODO
// 标准化 logger
export default class PluginAPI {
Expand All @@ -11,6 +11,7 @@ export default class PluginAPI {
this.key = opts.key;
this.service = opts.service;
this.utils = utils;
this.logger = new Logger(`fes:plugin:${this.id || this.key}`);
}

// TODO: reversed keys
Expand Down
48 changes: 23 additions & 25 deletions packages/fes-core/src/Service/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,7 @@ export default class Service extends EventEmitter {
// 1. merge default config
// 2. validate
this.setStage(ServiceStage.getConfig);
const defaultConfig = await this.applyPlugins({
key: 'modifyDefaultConfig',
type: this.ApplyPluginsType.modify,
initialValue: await this.configInstance.getDefaultConfig()
});
this.config = await this.applyPlugins({
key: 'modifyConfig',
type: this.ApplyPluginsType.modify,
initialValue: this.configInstance.getConfig({
defaultConfig
})
});
await this.setConfig();

// merge paths to keep the this.paths ref
this.setStage(ServiceStage.getPaths);
Expand All @@ -209,6 +198,21 @@ export default class Service extends EventEmitter {
});
}

async setConfig() {
const defaultConfig = await this.applyPlugins({
key: 'modifyDefaultConfig',
type: this.ApplyPluginsType.modify,
initialValue: await this.configInstance.getDefaultConfig()
});
this.config = await this.applyPlugins({
key: 'modifyConfig',
type: this.ApplyPluginsType.modify,
initialValue: this.configInstance.getConfig({
defaultConfig
})
});
}

async initPlugins() {
this._extraPlugins = [];
this.setStage(ServiceStage.initPlugins);
Expand Down Expand Up @@ -257,7 +261,8 @@ export default class Service extends EventEmitter {
'config',
'env',
'args',
'hasPlugins'
'hasPlugins',
'setConfig'
].includes(prop)
) {
return typeof this[prop] === 'function'
Expand Down Expand Up @@ -422,13 +427,6 @@ export default class Service extends EventEmitter {
this.args = args;
await this.init();

// TODO 临时实现
await this.applyPlugins({
key: 'onGenerateFiles',
type: ApplyPluginsType.event
});


this.setStage(ServiceStage.run);
await this.applyPlugins({
key: 'onStart',
Expand All @@ -437,11 +435,11 @@ export default class Service extends EventEmitter {
args
}
});
// TODO 执行命令
// return this.runCommand({
// name,
// args
// });

return this.runCommand({
name,
args
});
}

async runCommand({
Expand Down
2 changes: 1 addition & 1 deletion packages/fes-core/src/Service/utils/pluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function pathToObj({ path, cwd }) {
} else {
id = winPath(path);
}
id = id.replace('@webank/fes-core/lib/plugins', '@@');
id = id.replace('@webank/fes-plugin-built-in/lib/plugins', '@@');
id = id.replace(/\.js$/, '');

const key = isPkgPlugin
Expand Down
4 changes: 2 additions & 2 deletions packages/fes-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import Config from './Config/Config';
import Service from './Service/Service';
import PluginAPI from './Service/PluginAPI';
import Logger from './logger/logger';
import { PluginType } from './Service/enums';
import { isPlugin } from './Service/utils/pluginUtils';
import ServiceWithBuiltIn from './ServiceWithBuiltIn';

export * from './route';

Expand All @@ -15,5 +15,5 @@ export {
PluginAPI,
isPlugin,
PluginType,
ServiceWithBuiltIn
Logger
};
75 changes: 75 additions & 0 deletions packages/fes-core/src/logger/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
createDebug,
chalk
} from '@umijs/utils';

export default class Logger {
LOG = chalk.black.bgBlue('LOG');

INFO = chalk.black.bgBlue('INFO');

WARN = chalk.black.bgHex('#faad14')('WARN');

ERROR = chalk.black.bgRed('ERROR');

PROFILE = chalk.black.bgCyan('PROFILE');

constructor(namespace) {
// TODO: get namespace filename accounding caller function
if (!namespace) {
throw new Error('logger needs namespace');
}
this.namespace = namespace;
this.profilers = {};
this.debug = createDebug(this.namespace);
}

log(...args) {
// TODO: node env production
console.log(this.LOG, ...args);
}

/**
* The {@link logger.info} function is an alias for {@link logger.log()}.
* @param args
*/
info(...args) {
console.log(this.INFO, ...args);
}

error(...args) {
console.error(this.ERROR, ...args);
}

warn(...args) {
console.warn(this.WARN, ...args);
}

formatTiming(timing) {
return timing < 60 * 1000
? `${Math.round(timing / 10) / 100}s`
: `${Math.round(timing / 600) / 100}m`;
}

profile(id, message) {
const time = Date.now();
const namespace = `${this.namespace}:${id}`;
// for test
let msg;
if (this.profilers[id]) {
const timeEnd = this.profilers[id];
delete this.profilers[id];
process.stderr.write(`${this.PROFILE} `);
msg = `${this.PROFILE} ${chalk.cyan(
`└ ${namespace}`,
)} Completed in ${this.formatTiming(time - timeEnd)}`;
console.log(msg);
} else {
msg = `${this.PROFILE} ${chalk.cyan(`┌ ${namespace}`)} ${message || ''}`;
console.log(msg);
}

this.profilers[id] = time;
return msg;
}
}
15 changes: 0 additions & 15 deletions packages/fes-core/src/plugins/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/fes-plugin-built-in/.fatherrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
disableTypeCheck: false,
};
36 changes: 36 additions & 0 deletions packages/fes-plugin-built-in/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@webank/fes-plugin-built-in",
"version": "2.0.0",
"description": "@webank/fes-plugin-built-in",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib"
],
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"fes"
],
"authors": [
""
],
"license": "MIT",
"bugs": "",
"homepage": "",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@umijs/utils": "^3.2.24",
"@umijs/bundler-webpack": "^3.2.23",
"@umijs/server": "^3.2.23",
"@vue/babel-plugin-jsx": "^1.0.0-rc.3",
"@webank/fes-core": "^2.0.0",
"cliui": "6.0.0",
"vue-loader": "^16.0.0-rc.1",
"html-webpack-plugin": "^3.2.0"
}
}
53 changes: 53 additions & 0 deletions packages/fes-plugin-built-in/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// TODO 拆成独立的包作为内置插件包

export default [
// register methods
require.resolve('./plugins/registerMethods'),

// misc
require.resolve('./plugins/routes'),

// generate files
require.resolve('./plugins/generateFiles/core/plugin'),
require.resolve('./plugins/generateFiles/core/routes'),
require.resolve('./plugins/generateFiles/core/fesExports'),
require.resolve('./plugins/generateFiles/fes'),

// bundle configs
require.resolve('./plugins/features/alias'),
require.resolve('./plugins/features/analyze'),
require.resolve('./plugins/features/autoprefixer'),
require.resolve('./plugins/features/base'),
require.resolve('./plugins/features/chainWebpack'),
require.resolve('./plugins/features/chunks'),
require.resolve('./plugins/features/cssLoader'),
require.resolve('./plugins/features/cssnano'),
require.resolve('./plugins/features/copy'),
require.resolve('./plugins/features/define'),
require.resolve('./plugins/features/devServer'),
require.resolve('./plugins/features/devtool'),
require.resolve('./plugins/features/externals'),
require.resolve('./plugins/features/extraBabelPlugins'),
require.resolve('./plugins/features/extraBabelPresets'),
require.resolve('./plugins/features/extraPostCSSPlugins'),
require.resolve('./plugins/features/hash'),
require.resolve('./plugins/features/html'),
require.resolve('./plugins/features/inlineLimit'),
require.resolve('./plugins/features/lessLoader'),
require.resolve('./plugins/features/mountElementId'),
require.resolve('./plugins/features/nodeModulesTransform'),
require.resolve('./plugins/features/outputPath'),
require.resolve('./plugins/features/plugins'),
require.resolve('./plugins/features/postcssLoader'),
require.resolve('./plugins/features/proxy'),
require.resolve('./plugins/features/publicPath'),
require.resolve('./plugins/features/styleLoader'),
require.resolve('./plugins/features/targets'),
require.resolve('./plugins/features/terserOptions'),
require.resolve('./plugins/features/theme'),
require.resolve('./plugins/features/vueLoader'),

// commands
require.resolve('./plugins/commands/build/build'),
require.resolve('./plugins/commands/dev/dev')
];
Loading

0 comments on commit 28498a3

Please sign in to comment.