diff --git a/packages/core/build/src/build.ts b/packages/core/build/src/build.ts index 46be1144ba..d861bfbc3b 100755 --- a/packages/core/build/src/build.ts +++ b/packages/core/build/src/build.ts @@ -9,6 +9,7 @@ import { getCjsPackages, getPresetsPackages, ROOT_PATH, + ESM_PACKAGES, } from './constant'; import { buildClient } from './buildClient'; import { buildCjs } from './buildCjs'; @@ -18,6 +19,7 @@ import { PkgLog, getPkgLog, toUnixPath, getPackageJson, getUserConfig, UserConfi import { getPackages } from './utils/getPackages'; import { Package } from '@lerna/package'; import { tarPlugin } from './tarPlugin' +import { buildEsm } from './buildEsm'; const BUILD_ERROR = 'build-error'; @@ -51,6 +53,8 @@ export async function build(pkgs: string[]) { if (clientCore) { await buildPackage(clientCore, 'es', buildClient); } + const esmPackages = cjsPackages.filter(pkg => ESM_PACKAGES.includes(pkg.name)); + await buildPackages(esmPackages, 'es', buildEsm); // plugins/*、samples/* await buildPackages(pluginPackages, 'dist', buildPlugin); diff --git a/packages/core/build/src/buildClient.ts b/packages/core/build/src/buildClient.ts index b8259bcdfc..1ca0d73093 100644 --- a/packages/core/build/src/buildClient.ts +++ b/packages/core/build/src/buildClient.ts @@ -19,14 +19,14 @@ export async function buildClient(cwd: string, userConfig: UserConfig, sourcemap } return true; }; - await buildEsm(cwd, userConfig, sourcemap, external, log); - await buildLib(cwd, userConfig, sourcemap, external, log); + await buildClientEsm(cwd, userConfig, sourcemap, external, log); + await buildClientLib(cwd, userConfig, sourcemap, external, log); await buildLocale(cwd, userConfig, log); } type External = (id: string) => boolean; -export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean, external: External, log: PkgLog) { +function buildClientEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean, external: External, log: PkgLog) { log('build client esm'); const entry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/'); const outDir = path.resolve(cwd, 'es'); @@ -61,7 +61,7 @@ export function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean ); } -export async function buildLib( +async function buildClientLib( cwd: string, userConfig: UserConfig, sourcemap: boolean, diff --git a/packages/core/build/src/buildEsm.ts b/packages/core/build/src/buildEsm.ts new file mode 100644 index 0000000000..8515555498 --- /dev/null +++ b/packages/core/build/src/buildEsm.ts @@ -0,0 +1,66 @@ + +import path from 'path'; +import { PkgLog, UserConfig } from './utils'; +import { build as viteBuild } from 'vite'; +import fg from 'fast-glob'; + +export async function buildEsm(cwd: string, userConfig: UserConfig, sourcemap: boolean = false, log: PkgLog) { + log('build esm'); + + const indexEntry = path.join(cwd, 'src/index.ts').replaceAll(/\\/g, '/'); + const outDir = path.resolve(cwd, 'es'); + + await build(cwd, indexEntry, outDir, userConfig, sourcemap, log); + + const clientEntry = fg.sync(['src/client/index.ts', 'src/client.ts'], { cwd, absolute: true, onlyFiles: true })?.[0]?.replaceAll(/\\/g, '/'); + const clientOutDir = path.resolve(cwd, 'es/client'); + if (clientEntry) { + await build(cwd, clientEntry, clientOutDir, userConfig, sourcemap, log); + } + + const pkg = require(path.join(cwd, 'package.json')); + if (pkg.name === '@nocobase/test') { + const e2eEntry = path.join(cwd, 'src/e2e/index.ts').replaceAll(/\\/g, '/'); + const e2eOutDir = path.resolve(cwd, 'es/e2e'); + await build(cwd, e2eEntry, e2eOutDir, userConfig, sourcemap, log); + } +} + +function build(cwd: string, entry: string, outDir: string, userConfig: UserConfig, sourcemap: boolean = false, log: PkgLog) { + const cwdWin = cwd.replaceAll(/\\/g, '/'); + const cwdUnix = cwd.replaceAll(/\//g, '\\'); + const external = function (id: string) { + if (id.startsWith('.') || id.startsWith(cwdUnix) || id.startsWith(cwdWin)) { + return false; + } + return true; + }; + return viteBuild( + userConfig.modifyViteConfig({ + mode: process.env.NODE_ENV || 'production', + define: { + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'), + 'process.env.__TEST__': false, + 'process.env.__E2E__': process.env.__E2E__ ? true : false, + }, + build: { + minify: false, + outDir, + cssCodeSplit: true, + emptyOutDir: true, + sourcemap, + lib: { + entry, + formats: ['es'], + fileName: 'index', + }, + target: ['node16'], + rollupOptions: { + cache: true, + treeshake: true, + external, + }, + }, + }), + ); +} diff --git a/packages/core/build/src/constant.ts b/packages/core/build/src/constant.ts index f92072aff8..0fb14c46cd 100644 --- a/packages/core/build/src/constant.ts +++ b/packages/core/build/src/constant.ts @@ -39,6 +39,7 @@ export const getPresetsPackages = (packages: Package[]) => packages.filter((item) => item.location.startsWith(PRESETS_DIR)); export const CORE_APP = path.join(PACKAGES_PATH, 'core/app'); export const CORE_CLIENT = path.join(PACKAGES_PATH, 'core/client'); +export const ESM_PACKAGES = ['@nocobase/test']; export const CJS_EXCLUDE_PACKAGES = [ path.join(PACKAGES_PATH, 'core/build'), path.join(PACKAGES_PATH, 'core/cli'), diff --git a/packages/core/create-nocobase-app/templates/app/vitest.config.mts b/packages/core/create-nocobase-app/templates/app/vitest.config.mts index 8a0f569009..4e6455b4d1 100644 --- a/packages/core/create-nocobase-app/templates/app/vitest.config.mts +++ b/packages/core/create-nocobase-app/templates/app/vitest.config.mts @@ -1,3 +1,3 @@ -import { defineConfig } from '@nocobase/test'; +import { defineConfig } from '@nocobase/test/vitest.mjs'; export default defineConfig(); diff --git a/packages/core/test/package.json b/packages/core/test/package.json index 7871a6401b..25f4b3fa72 100644 --- a/packages/core/test/package.json +++ b/packages/core/test/package.json @@ -12,8 +12,8 @@ "default": "./lib/index.js" }, "import": { - "types": "./vitest.d.ts", - "default": "./vitest.mjs" + "types": "./es/index.d.ts", + "default": "./es/index.mjs" } }, "./client": { @@ -22,8 +22,8 @@ "default": "./lib/client/index.js" }, "import": { - "types": "./lib/client/index.d.ts", - "default": "./lib/client/index.js" + "types": "./es/client/index.d.ts", + "default": "./es/client/index.mjs" } }, "./e2e": { @@ -32,11 +32,12 @@ "default": "./lib/e2e/index.js" }, "import": { - "types": "./lib/e2e/index.d.ts", - "default": "./lib/e2e/index.js" + "types": "./es/e2e/index.d.ts", + "default": "./es/e2e/index.mjs" } }, - "./package.json": "./package.json" + "./package.json": "./package.json", + "./vitest.mjs": "./vitest.mjs" }, "dependencies": { "@faker-js/faker": "8.1.0", diff --git a/packages/core/test/src/defineConfig.d.ts b/packages/core/test/src/defineConfig.d.ts deleted file mode 100644 index 4491e7022d..0000000000 --- a/packages/core/test/src/defineConfig.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -declare global { - const suite: (typeof import('vitest'))['suite']; - const test: (typeof import('vitest'))['test']; - const describe: (typeof import('vitest'))['describe']; - const it: (typeof import('vitest'))['it']; - const expectTypeOf: (typeof import('vitest'))['expectTypeOf']; - const assertType: (typeof import('vitest'))['assertType']; - const expect: (typeof import('vitest'))['expect']; - const assert: (typeof import('vitest'))['assert']; - const vitest: (typeof import('vitest'))['vitest']; - const vi: (typeof import('vitest'))['vitest']; - const beforeAll: (typeof import('vitest'))['beforeAll']; - const afterAll: (typeof import('vitest'))['afterAll']; - const beforeEach: (typeof import('vitest'))['beforeEach']; - const afterEach: (typeof import('vitest'))['afterEach']; -} -import { type UserConfig } from 'vitest/config'; -export declare const defineConfig: ( - config?: UserConfig & { - server: boolean; - }, -) => UserConfig; diff --git a/packages/core/test/src/defineConfig.ts b/packages/core/test/src/defineConfig.ts deleted file mode 100644 index 384fd68b36..0000000000 --- a/packages/core/test/src/defineConfig.ts +++ /dev/null @@ -1,2 +0,0 @@ -// @ts-ignore -export * from '../vitest'; diff --git a/packages/core/test/src/index.ts b/packages/core/test/src/index.ts index 956680b9ed..0ce5251aa3 100644 --- a/packages/core/test/src/index.ts +++ b/packages/core/test/src/index.ts @@ -1,2 +1 @@ -export * from './defineConfig'; export * from './server'; diff --git a/packages/core/test/src/server/mockServer.ts b/packages/core/test/src/server/mockServer.ts index 005e87bfa5..b126d44ce5 100644 --- a/packages/core/test/src/server/mockServer.ts +++ b/packages/core/test/src/server/mockServer.ts @@ -1,5 +1,5 @@ import { mockDatabase } from '@nocobase/database'; -import Application, { ApplicationOptions, AppSupervisor, Gateway, PluginManager } from '@nocobase/server'; +import { Application, ApplicationOptions, AppSupervisor, Gateway, PluginManager } from '@nocobase/server'; import jwt from 'jsonwebtoken'; import qs from 'qs'; import supertest, { SuperAgentTest } from 'supertest'; @@ -240,7 +240,7 @@ export async function createMockServer( } = {}, ) { const { version, beforeInstall, skipInstall, skipStart, ...others } = options; - const app = mockServer(others); + const app: any = mockServer(others); if (!skipInstall) { if (beforeInstall) { await beforeInstall(app); diff --git a/vitest.config.mts b/vitest.config.mts index 8a0f569009..4e6455b4d1 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -1,3 +1,3 @@ -import { defineConfig } from '@nocobase/test'; +import { defineConfig } from '@nocobase/test/vitest.mjs'; export default defineConfig();