diff --git a/packages/fes-compiler/src/config/index.js b/packages/fes-compiler/src/config/index.js index 0f2ebc89..55067a7a 100644 --- a/packages/fes-compiler/src/config/index.js +++ b/packages/fes-compiler/src/config/index.js @@ -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'; diff --git a/packages/fes-compiler/src/service/enums.js b/packages/fes-compiler/src/service/enums.js index ee29aa4e..ff6c2499 100644 --- a/packages/fes-compiler/src/service/enums.js +++ b/packages/fes-compiler/src/service/enums.js @@ -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', }; diff --git a/packages/fes-compiler/src/service/index.js b/packages/fes-compiler/src/service/index.js index 9034e4ad..1faf5cba 100644 --- a/packages/fes-compiler/src/service/index.js +++ b/packages/fes-compiler/src/service/index.js @@ -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, }); } diff --git a/packages/fes-compiler/src/service/pluginAPI.js b/packages/fes-compiler/src/service/pluginAPI.js index 6cdf90d7..5905e144 100644 --- a/packages/fes-compiler/src/service/pluginAPI.js +++ b/packages/fes-compiler/src/service/pluginAPI.js @@ -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; } diff --git a/packages/fes-compiler/src/service/plugins/builder.js b/packages/fes-compiler/src/service/plugins/builder.js new file mode 100644 index 00000000..19671a01 --- /dev/null +++ b/packages/fes-compiler/src/service/plugins/builder.js @@ -0,0 +1,11 @@ +export default (api) => { + api.describe({ + key: 'builder', + config: { + schema(joi) { + return joi.string(); + }, + default: '', + }, + }); +}; diff --git a/packages/fes-compiler/src/service/utils/pluginUtils.js b/packages/fes-compiler/src/service/utils/pluginUtils.js index 87d185a5..8e3f4fb0 100644 --- a/packages/fes-compiler/src/service/utils/pluginUtils.js +++ b/packages/fes-compiler/src/service/utils/pluginUtils.js @@ -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) => diff --git a/packages/fes-template-vite/.fes.js b/packages/fes-template-vite/.fes.js index 79acb567..d8ef8a5f 100644 --- a/packages/fes-template-vite/.fes.js +++ b/packages/fes-template-vite/.fes.js @@ -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'], }, }); diff --git a/packages/fes-template-vite/.fes.prod.js b/packages/fes-template-vite/.fes.prod.js index 7cf1c120..33f8ac56 100644 --- a/packages/fes-template-vite/.fes.prod.js +++ b/packages/fes-template-vite/.fes.prod.js @@ -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/', +}); diff --git a/packages/fes-template-vite/package.json b/packages/fes-template-vite/package.json index 28e88ff1..409393d0 100644 --- a/packages/fes-template-vite/package.json +++ b/packages/fes-template-vite/package.json @@ -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", diff --git a/packages/fes/src/cli.js b/packages/fes/src/cli.js index 7fd11672..cc37109c 100644 --- a/packages/fes/src/cli.js +++ b/packages/fes/src/cli.js @@ -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; diff --git a/packages/fes/types.d.ts b/packages/fes/types.d.ts index de0fe6e3..d850e41b 100644 --- a/packages/fes/types.d.ts +++ b/packages/fes/types.d.ts @@ -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;