From 100c7f5bb702c71dde879f4f5083cd0913169308 Mon Sep 17 00:00:00 2001 From: Bright Wu <1521488775@qq.com> Date: Sun, 24 Dec 2023 13:18:40 +0800 Subject: [PATCH] fix: test issues --- examples/hmr/package.json | 3 + examples/vite-adapter-svelet/package.json | 2 +- packages/core/src/config/index.ts | 6 +- packages/core/src/index.ts | 4 -- .../core/src/plugin/js/farm-to-vite-config.ts | 56 ------------------- .../core/src/plugin/js/vite-plugin-adapter.ts | 3 - packages/core/tests/binding.spec.ts | 8 ++- packages/core/tests/common.ts | 6 +- packages/core/tests/config.spec.ts | 43 ++++++++++---- pnpm-lock.yaml | 9 +++ 10 files changed, 54 insertions(+), 86 deletions(-) diff --git a/examples/hmr/package.json b/examples/hmr/package.json index 9b4436ffa..ca13821e9 100644 --- a/examples/hmr/package.json +++ b/examples/hmr/package.json @@ -10,5 +10,8 @@ "devDependencies": { "@farmfe/cli": "workspace:^", "@farmfe/core": "workspace:^" + }, + "dependencies": { + "core-js": "^3.34.0" } } diff --git a/examples/vite-adapter-svelet/package.json b/examples/vite-adapter-svelet/package.json index 9095d18db..89c6fdb8a 100644 --- a/examples/vite-adapter-svelet/package.json +++ b/examples/vite-adapter-svelet/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "farm dev", - "build": "farm build", + "build": "echo \"Error: build script not supported\"", "preview": "farm preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch" diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index 81ba68819..867662d11 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -65,7 +65,6 @@ export function defineFarmConfig( export async function resolveConfig( inlineOptions: FarmCLIOptions, logger: Logger, - command: 'serve' | 'build', mode?: CompilationMode ): Promise { // Clear the console according to the cli command @@ -146,7 +145,8 @@ export async function resolveConfig( ); // check port availability: auto increment the port if a conflict occurs const targetWeb = !( - userConfig.compilation?.output?.targetEnv === 'node' || command === 'build' + userConfig.compilation?.output?.targetEnv === 'node' || + mode === 'production' ); targetWeb && (await DevServer.resolvePortConflict(resolvedUserConfig.server, logger)); @@ -255,7 +255,7 @@ export async function normalizeUserCompilationConfig( // for node target, we should not define process.env.NODE_ENV config.output?.targetEnv === 'node' ? {} - : Object.keys(userConfig.env).reduce((env: any, key) => { + : Object.keys(userConfig.env || {}).reduce((env: any, key) => { env[`process.env.${key}`] = userConfig.env[key]; return env; }, {}) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 79d289338..5d191e346 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -42,7 +42,6 @@ export async function start( const resolvedUserConfig = await resolveConfig( inlineConfig, logger, - 'serve', 'development' ); const { @@ -113,7 +112,6 @@ export async function build( const resolvedUserConfig = await resolveConfig( inlineConfig, logger, - 'build', 'production' ); @@ -138,7 +136,6 @@ export async function preview(inlineConfig: FarmCLIOptions): Promise { const resolvedUserConfig = await resolveConfig( inlineConfig, logger, - 'serve', 'production' ); @@ -218,7 +215,6 @@ export async function watch( const resolvedUserConfig = await resolveConfig( inlineConfig, logger, - 'build', 'development' ); diff --git a/packages/core/src/plugin/js/farm-to-vite-config.ts b/packages/core/src/plugin/js/farm-to-vite-config.ts index c173d3618..b3794cfc1 100644 --- a/packages/core/src/plugin/js/farm-to-vite-config.ts +++ b/packages/core/src/plugin/js/farm-to-vite-config.ts @@ -5,7 +5,6 @@ import { throwIncompatibleError } from './utils.js'; import merge from 'lodash.merge'; -import { Config } from '../../../binding/index.js'; import { Logger } from '../../index.js'; export function farmUserConfigToViteConfig(config: UserConfig): ViteUserConfig { @@ -62,61 +61,6 @@ export function farmUserConfigToViteConfig(config: UserConfig): ViteUserConfig { return viteConfig; } -export function farmNormalConfigToViteConfig( - config: Config['config'], - farmConfig: UserConfig -): ViteUserConfig { - const vitePlugins = farmConfig.vitePlugins.map((plugin) => { - if (typeof plugin === 'function') { - return plugin().vitePlugin; - } else { - return plugin; - } - }); - - return { - root: config.root, - base: config?.output?.publicPath ?? '/', - publicDir: farmConfig.publicDir ?? 'public', - mode: config?.mode, - define: config?.define, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore ignore this error - command: config?.mode === 'production' ? 'build' : 'serve', - resolve: { - alias: config?.resolve?.alias, - extensions: config?.resolve?.extensions, - mainFields: config?.resolve?.mainFields, - conditions: config?.resolve?.conditions, - preserveSymlinks: config?.resolve?.symlinks === false - }, - plugins: vitePlugins, - server: { - hmr: Boolean(farmConfig.server?.hmr), - port: farmConfig.server?.port, - host: farmConfig.server?.host, - strictPort: farmConfig.server?.strictPort, - https: farmConfig.server?.https, - proxy: farmConfig.server?.proxy as any, - open: farmConfig.server?.open - // other options are not supported in farm - }, - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore ignore this error - isProduction: config.compilation?.mode === 'production', - css: { - devSourcemap: false - }, - build: { - outDir: config?.output?.path, - sourcemap: Boolean(config?.sourcemap), - minify: config?.minify, - cssMinify: config?.minify, - ssr: config?.output?.targetEnv === 'node' - // other options are not supported in farm - } - }; -} export function proxyViteConfig( viteConfig: ViteUserConfig, diff --git a/packages/core/src/plugin/js/vite-plugin-adapter.ts b/packages/core/src/plugin/js/vite-plugin-adapter.ts index 5df1d8ffd..1122cb8f9 100644 --- a/packages/core/src/plugin/js/vite-plugin-adapter.ts +++ b/packages/core/src/plugin/js/vite-plugin-adapter.ts @@ -57,7 +57,6 @@ import { } from './vite-server-adapter.js'; import { farmContextToViteContext } from './farm-to-vite-context.js'; import { - farmNormalConfigToViteConfig, farmUserConfigToViteConfig, proxyViteConfig, viteConfigToFarmConfig @@ -220,8 +219,6 @@ export class VitePluginAdapter implements JsPlugin { this._logger ); - this._viteConfig = farmNormalConfigToViteConfig(config, this._farmConfig); - const configResolvedHook = this.wrapRawPluginHook( 'configResolved', this._rawPlugin.configResolved diff --git a/packages/core/tests/binding.spec.ts b/packages/core/tests/binding.spec.ts index a59e1fcb4..196075cd4 100644 --- a/packages/core/tests/binding.spec.ts +++ b/packages/core/tests/binding.spec.ts @@ -4,21 +4,23 @@ import { test } from 'vitest'; import { Compiler, DefaultLogger, + normalizeDevServerOptions, normalizeUserCompilationConfig } from '../src/index.js'; // just make sure the binding works test('Binding - should parse config to rust correctly', async () => { const currentDir = path.dirname(fileURLToPath(import.meta.url)); + const serverConfig = normalizeDevServerOptions({}, 'production'); const compilationConfig = await normalizeUserCompilationConfig( - null, { - root: path.resolve(currentDir, 'fixtures', 'binding') + root: path.resolve(currentDir, 'fixtures', 'binding'), + server: serverConfig }, new DefaultLogger() ); const compiler = new Compiler({ - ...compilationConfig, + config: compilationConfig, jsPlugins: [], rustPlugins: [] }); diff --git a/packages/core/tests/common.ts b/packages/core/tests/common.ts index 97e2540f5..4b6b7d3fb 100644 --- a/packages/core/tests/common.ts +++ b/packages/core/tests/common.ts @@ -12,7 +12,6 @@ export async function getCompiler( input?: Record ): Promise { const compilationConfig = await normalizeUserCompilationConfig( - null, { root, compilation: { @@ -28,9 +27,6 @@ export async function getCompiler( sourcemap: false, persistentCache: false }, - server: { - hmr: false - }, plugins }, new DefaultLogger(), @@ -38,7 +34,7 @@ export async function getCompiler( ); return new Compiler({ - config: compilationConfig.config, + config: compilationConfig, jsPlugins: plugins, rustPlugins: [] }); diff --git a/packages/core/tests/config.spec.ts b/packages/core/tests/config.spec.ts index f9bc2a479..cd1007cce 100644 --- a/packages/core/tests/config.spec.ts +++ b/packages/core/tests/config.spec.ts @@ -17,26 +17,47 @@ test('resolveUserConfig', async () => { const config = await resolveConfig( { configPath: path.join(filePath, 'fixtures', 'config', 'farm.config.ts') }, new DefaultLogger(), - 'serve', 'development' ); - expect(config).toEqual({ - compilation: { - input: { - main: './main.tsx' - }, - external: builtinModules + expect(config.compilation.define).toEqual({ + FARM_HMR_HOST: true, + FARM_HMR_PATH: '/__hmr', + FARM_HMR_PORT: '9000', + FARM_PROCESS_ENV: { + NODE_ENV: 'test' }, - envMode: 'development', - configFileDependencies: [ + 'process.env.NODE_ENV': 'test' + }); + expect(config.compilation.input).toEqual({ + main: './main.tsx' + }); + expect(config.compilation.output).toEqual({ + path: './dist', + publicPath: '/' + }); + expect(config.compilation.lazyCompilation).toEqual(true); + expect(config.compilation.sourcemap).toEqual(true); + expect(config.compilation.minify).toEqual(false); + expect(config.compilation.presetEnv).toEqual(false); + expect(config.compilation.persistentCache).toEqual({ + buildDependencies: [ // path.join(filePath, '..', 'src', 'config.ts'), path.join(filePath, 'fixtures', 'config', 'farm.config.ts'), path.join(filePath, 'fixtures', 'config', 'util.ts'), - 'module' + 'module', + 'package-lock.json', + 'pnpm-lock.yaml', + 'yarn.lock' ], - server: normalizeDevServerOptions(config.server, 'development') + envs: { + NODE_ENV: 'test' + }, + moduleCacheKeyStrategy: {} }); + expect(config.server).toEqual( + normalizeDevServerOptions(config.server, 'development') + ); }); describe('normalize-dev-server-options', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index df94abdcc..31fd39d20 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -322,6 +322,10 @@ importers: version: 18.0.0 examples/hmr: + dependencies: + core-js: + specifier: ^3.34.0 + version: 3.34.0 devDependencies: '@farmfe/cli': specifier: workspace:^ @@ -6944,6 +6948,11 @@ packages: resolution: {integrity: sha512-lo0kOocUlLKmm6kv/FswQL8zbkH7mVsLJ/FULClOhv8WRVmKLVcs6XPNQAzstfeJTCHMyButEwG+z1kHxHoDZw==} requiresBuild: true + /core-js@3.34.0: + resolution: {integrity: sha512-aDdvlDder8QmY91H88GzNi9EtQi2TjvQhpCX6B1v/dAZHU1AuLgHvRh54RiOerpEhEW46Tkf+vgAViB/CWC0ag==} + requiresBuild: true + dev: false + /core-util-is@1.0.3: resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==}