Skip to content

Commit

Permalink
fix: test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Dec 24, 2023
1 parent 0fa607d commit 100c7f5
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 86 deletions.
3 changes: 3 additions & 0 deletions examples/hmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"devDependencies": {
"@farmfe/cli": "workspace:^",
"@farmfe/core": "workspace:^"
},
"dependencies": {
"core-js": "^3.34.0"
}
}
2 changes: 1 addition & 1 deletion examples/vite-adapter-svelet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function defineFarmConfig(
export async function resolveConfig(
inlineOptions: FarmCLIOptions,
logger: Logger,
command: 'serve' | 'build',
mode?: CompilationMode
): Promise<ResolvedUserConfig> {
// Clear the console according to the cli command
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) => {

Check warning on line 258 in packages/core/src/config/index.ts

View workflow job for this annotation

GitHub Actions / TS Code Lint

Unexpected any. Specify a different type
env[`process.env.${key}`] = userConfig.env[key];
return env;
}, {})
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export async function start(
const resolvedUserConfig = await resolveConfig(
inlineConfig,
logger,
'serve',
'development'
);
const {
Expand Down Expand Up @@ -113,7 +112,6 @@ export async function build(
const resolvedUserConfig = await resolveConfig(
inlineConfig,
logger,
'build',
'production'
);

Expand All @@ -138,7 +136,6 @@ export async function preview(inlineConfig: FarmCLIOptions): Promise<void> {
const resolvedUserConfig = await resolveConfig(
inlineConfig,
logger,
'serve',
'production'
);

Expand Down Expand Up @@ -218,7 +215,6 @@ export async function watch(
const resolvedUserConfig = await resolveConfig(
inlineConfig,
logger,
'build',
'development'
);

Expand Down
56 changes: 0 additions & 56 deletions packages/core/src/plugin/js/farm-to-vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/plugin/js/vite-plugin-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
} from './vite-server-adapter.js';
import { farmContextToViteContext } from './farm-to-vite-context.js';
import {
farmNormalConfigToViteConfig,
farmUserConfigToViteConfig,
proxyViteConfig,
viteConfigToFarmConfig
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions packages/core/tests/binding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
});
Expand Down
6 changes: 1 addition & 5 deletions packages/core/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export async function getCompiler(
input?: Record<string, string>
): Promise<Compiler> {
const compilationConfig = await normalizeUserCompilationConfig(
null,
{
root,
compilation: {
Expand All @@ -28,17 +27,14 @@ export async function getCompiler(
sourcemap: false,
persistentCache: false
},
server: {
hmr: false
},
plugins
},
new DefaultLogger(),
'production'
);

return new Compiler({
config: compilationConfig.config,
config: compilationConfig,
jsPlugins: plugins,
rustPlugins: []
});
Expand Down
43 changes: 32 additions & 11 deletions packages/core/tests/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 100c7f5

Please sign in to comment.