feat: 支持配置选择vite (#159)

This commit is contained in:
听海 2022-12-07 10:20:39 +08:00 committed by GitHub
parent a9b7b1ef88
commit c214ea0828
11 changed files with 86 additions and 47 deletions

View File

@ -5,8 +5,8 @@
import { existsSync } from 'fs';
import { extname, join } from 'path';
import { chalk, chokidar, compatESModuleRequire, deepmerge, cleanRequireCache, lodash, parseRequireDeps, winPath } from '@fesjs/utils';
import assert from 'assert';
import { chalk, chokidar, compatESModuleRequire, deepmerge, cleanRequireCache, lodash, parseRequireDeps, winPath } from '@fesjs/utils';
import joi from 'joi';
import { ServiceStage } from '../service/enums';
import { getUserConfigWithKey, updateUserConfigWithKey } from './utils/configUtils';

View File

@ -1,6 +1,7 @@
export const PluginType = {
preset: 'preset',
plugin: 'plugin'
plugin: 'plugin',
builder: 'builder',
};
export const ServiceStage = {
@ -12,21 +13,21 @@ export const ServiceStage = {
pluginReady: 5,
getConfig: 6,
getPaths: 7,
run: 8
run: 8,
};
export const ConfigChangeType = {
reload: 'reload',
regenerateTmpFiles: 'regenerateTmpFiles'
regenerateTmpFiles: 'regenerateTmpFiles',
};
export const ApplyPluginsType = {
add: 'add',
modify: 'modify',
event: 'event'
event: 'event',
};
export const EnableBy = {
register: 'register',
config: 'config'
config: 'config',
};

View File

@ -5,18 +5,18 @@
import { join } from 'path';
import { EventEmitter } from 'events';
import assert from 'assert';
import { AsyncSeriesWaterfallHook } from 'tapable';
import { existsSync } from 'fs';
import { AsyncSeriesWaterfallHook } from 'tapable';
import { lodash, chalk } from '@fesjs/utils';
import { Command, Option } from 'commander';
import Config from '../config';
import { getUserConfigWithKey } from '../config/utils/configUtils';
import { resolvePresets, pathToObj, resolvePlugins } from './utils/pluginUtils';
import loadDotEnv from './utils/loadDotEnv';
import isPromise from './utils/isPromise';
import BabelRegister from './babelRegister';
import PluginAPI from './pluginAPI';
import { ApplyPluginsType, ConfigChangeType, EnableBy, PluginType, ServiceStage } from './enums';
import Config from '../config';
import { getUserConfigWithKey } from '../config/utils/configUtils';
import getPaths from './getPaths';
// TODO
@ -125,11 +125,13 @@ export default class Service extends EventEmitter {
...baseOpts,
presets: opts.presets || [],
userConfigPresets: this.userConfig.presets || [],
builder: this.userConfig.builder,
});
this.initialPlugins = resolvePlugins({
...baseOpts,
plugins: opts.plugins || [],
userConfigPlugins: this.userConfig.plugins || [],
builder: this.userConfig.builder,
});
}

View File

@ -123,9 +123,9 @@ export default class PluginAPI {
registerBuilder(builder) {
assert(typeof builder === 'object', 'api.registerBuilder() failed, the builder must be object.');
const { name } = builder;
assert(typeof name === 'string', 'api.registerBuilder() failed, the builder.name must be string.');
assert(typeof this.service.builder.name !== 'string', `检测到您使用了 builder: ${name},已经加载 builder: ${this.service.builder.name} 请保留一个`);
// const { name } = builder;
// assert(typeof name === 'string', 'api.registerBuilder() failed, the builder.name must be string.');
// assert(typeof this.service.builder.name !== 'string', `检测到您使用了 builder: ${name},已经加载 builder: ${this.service.builder.name} 请保留一个`);
this.service.builder = builder;
}

View File

@ -0,0 +1,11 @@
export default (api) => {
api.describe({
key: 'builder',
config: {
schema(joi) {
return joi.string();
},
default: '',
},
});
};

View File

@ -1,11 +1,12 @@
import { dirname, join, basename, relative, extname } from 'path';
import { compatESModuleRequire, resolve, winPath, pkgUp, lodash } from '@fesjs/utils';
import { compatESModuleRequire, resolve, winPath, pkgUp, lodash, chalk } from '@fesjs/utils';
import { PluginType } from '../enums';
const RE = {
[PluginType.plugin]: /^(@fesjs\/|@webank\/fes-|fes-)plugin-/,
[PluginType.preset]: /^(@fesjs\/|@webank\/fes-|fes-)(preset|builder)-/,
[PluginType.preset]: /^(@fesjs\/|@webank\/fes-|fes-)preset-/,
[PluginType.builder]: /^(@fesjs\/|@webank\/fes-|fes-)builder-/,
};
export function isPluginOrPreset(type, name) {
@ -17,10 +18,29 @@ export function isPluginOrPreset(type, name) {
return re.test(name);
}
function filterPluginAndPreset(type, pkg) {
return Object.keys(pkg.devDependencies || {})
.concat(Object.keys(pkg.dependencies || {}))
function filterBuilder(opts) {
const builders = Object.keys(opts.pkg.devDependencies || {})
.concat(Object.keys(opts.pkg.dependencies || {}))
.filter(isPluginOrPreset.bind(null, PluginType.builder))
.filter((builder) => builder.indexOf(opts.builder || '') !== -1);
if (builders.length > 1) {
console.log(chalk.yellow(`提示您使用了多个builder默认使用第一个${builders[0]}`));
return builders[0];
}
return builders;
}
function filterPluginAndPreset(type, opts) {
const base = Object.keys(opts.pkg.devDependencies || {})
.concat(Object.keys(opts.pkg.dependencies || {}))
.filter(isPluginOrPreset.bind(null, type));
if (type === PluginType.preset) {
return base.concat(filterBuilder(opts));
}
if (type === PluginType.plugin) {
return base.concat(join(__dirname, '../plugins/builder.js'));
}
return base;
}
export function getPluginsOrPresets(type, opts) {
@ -30,7 +50,7 @@ export function getPluginsOrPresets(type, opts) {
...(opts[type === PluginType.preset ? 'presets' : 'plugins'] || []),
// env
...(process.env[`FES_${upperCaseType}S`] || '').split(',').filter(Boolean),
...filterPluginAndPreset(type, opts.pkg),
...filterPluginAndPreset(type, opts),
// user config
...(opts[type === PluginType.preset ? 'userConfigPresets' : 'userConfigPlugins'] || []),
].map((path) =>

View File

@ -1,27 +1,28 @@
import { defineBuildConfig } from "@fesjs/fes";
import { defineBuildConfig } from '@fesjs/fes';
export default defineBuildConfig({
builder: 'vite',
define: {
__DEV__: false
__DEV__: false,
},
title: '海贼王',
router: {
mode: 'hash'
mode: 'hash',
},
access: {
roles: {
admin: ['*'],
menuTest: ['/', '/menuTest']
}
menuTest: ['/', '/menuTest'],
},
},
mock: {
prefix: '/v2'
prefix: '/v2',
},
proxy: {
'/v2': {
target: 'https://api.douban.com/',
changeOrigin: true
}
changeOrigin: true,
},
},
layout: {
title: 'Fes.js',
@ -33,51 +34,51 @@ export default defineBuildConfig({
{
name: 'index',
icon: '/wine-outline.svg',
match: ['/route/*']
match: ['/route/*'],
},
{
name: 'store'
name: 'store',
},
{
name: 'editor',
icon: '/wine-outline.svg'
icon: '/wine-outline.svg',
},
{
title: '$externalLink',
icon: 'UserOutlined',
path: 'https://www.baidu.com'
path: 'https://www.baidu.com',
},
{
name: 'mock'
name: 'mock',
},
{
title: '菜单权限测试',
children: [
{
title: '子菜单',
path: '/menuTest'
path: '/menuTest',
},
]
],
},
{
name: 'cssModule'
name: 'cssModule',
},
{
name: 'pinia'
}
]
name: 'pinia',
},
],
},
enums: {
status: [
['0', '无效的'],
['1', '有效的']
]
['1', '有效的'],
],
},
vuex: {
strict: true
strict: true,
},
dynamicImport: true,
monacoEditor: {
languages: ['javascript', 'typescript', 'html', 'json']
languages: ['javascript', 'typescript', 'html', 'json'],
},
});

View File

@ -1,5 +1,6 @@
import { defineBuildConfig } from "@fesjs/fes";
import { defineBuildConfig } from '@fesjs/fes';
export default {
publicPath: 'https://gw.alipayobjects.com/'
};
export default defineBuildConfig({
builder: 'webpack',
publicPath: 'https://gw.alipayobjects.com/',
});

View File

@ -44,6 +44,7 @@
},
"dependencies": {
"@fesjs/builder-vite": "^3.0.0-rc.0",
"@fesjs/builder-webpack": "^3.0.0-rc.0",
"@fesjs/fes": "^3.0.0-rc.0",
"@fesjs/fes-design": "^0.7.0",
"@fesjs/plugin-access": "^3.0.0-rc.0",

View File

@ -1,9 +1,9 @@
import { chalk, yParser, semver } from '@fesjs/utils';
import fesPkg from '../package.json';
import { Service } from './serviceWithBuiltIn';
import fork from './utils/fork';
import getCwd from './utils/getCwd';
import getPkg from './utils/getPkg';
import fesPkg from '../package.json';
import { hackFesInBuild } from './hackFesInBuild';
const requiredVersion = fesPkg.engines.node;

View File

@ -9,7 +9,9 @@ export interface RouteMeta {
export interface PluginRuntimeConfig {}
export interface PluginBuildConfig {}
export interface PluginBuildConfig {
builder?: string,
}
export declare function defineRouteMeta(routeMeta: RouteMeta): RouteMeta;