Skip to content

Commit

Permalink
feat: make Api to receive a partial config
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Apr 26, 2020
1 parent d5df02f commit 70fb076
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 65 deletions.
7 changes: 3 additions & 4 deletions packages/shuvi/src/api/__tests__/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { Api } from '../api';
import { PluginApi } from '../pluginApi';
import { shuviConfig } from './utils';

describe('api', () => {
let gApi: Api;
beforeAll(() => {
gApi = new Api({
mode: 'development',
config: shuviConfig(),
config: {},
});
});

test('should has "production" be default mode', () => {
const prodApi = new Api({ config: shuviConfig() });
const prodApi = new Api({ config: {} });
expect(prodApi.mode).toBe('production');
});

test('plugins', () => {
let pluginApi: PluginApi;
const api = new Api({
config: shuviConfig({ plugins: [(api) => (pluginApi = api)] }),
config: { plugins: [(api) => (pluginApi = api)] },
});
expect(pluginApi!).toBeDefined();
expect(pluginApi!.paths).toBe(api.paths);
Expand Down
18 changes: 0 additions & 18 deletions packages/shuvi/src/api/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import path from 'path';
import { IConfig } from '@shuvi/types';
import { deepmerge } from '@shuvi/utils/lib/deepmerge';

export function resolvePlugin(name: string) {
return path.join(__dirname, 'fixtures', 'plugins', name);
}

export function shuviConfig(config: Partial<IConfig> = {}): IConfig {
return deepmerge(
{
ssr: false,
env: {},
rootDir: '/',
outputPath: 'dist',
publicPath: '',
router: {
history: 'auto',
},
},
config
);
}
18 changes: 10 additions & 8 deletions packages/shuvi/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
IConfig,
IApiConfig,
IApi,
ICallHookOpts,
IHookConfig,
Expand All @@ -14,11 +14,13 @@ import {
} from '@shuvi/types';
import { App, IRouteConfig, IFile } from '@shuvi/core';
import { joinPath } from '@shuvi/utils/lib/string';
import { deepmerge } from '@shuvi/utils/lib/deepmerge';
import { Hooks } from '../lib/hooks';
import { setRuntimeConfig } from '../lib/runtimeConfig';
import { serializeRoutes, normalizeRoutes } from '../lib/routes';
import { PUBLIC_PATH, ROUTE_RESOURCE_QUERYSTRING } from '../constants';
import { runtime } from '../runtime';
import { defaultConfig, IConfig } from '../config';
import { IResources, IBuiltResource, IPlugin } from './types';
import { Server } from '../server';
import { setupApp } from './setupApp';
Expand All @@ -32,7 +34,7 @@ const ServiceModes: IShuviMode[] = ['development', 'production'];
export class Api implements IApi {
mode: IShuviMode;
paths: IPaths;
config: IConfig;
config: IApiConfig;

private _hooks: Hooks;
private _app: App;
Expand All @@ -49,21 +51,21 @@ export class Api implements IApi {
} else {
this.mode = 'production';
}
this.config = config;
this.config = deepmerge(defaultConfig, config);
this.paths = getPaths({
rootDir: config.rootDir,
outputPath: config.outputPath
rootDir: this.config.rootDir,
outputPath: this.config.outputPath
});

this._hooks = new Hooks();
this._app = new App();
this._plugins = resolvePlugins(config.plugins || []);
this._plugins = resolvePlugins(this.config.plugins || []);
this._plugins.forEach(plugin => plugin.get()(this.getPluginApi()));

initCoreResource(this);

if (typeof config.runtimeConfig === 'object') {
setRuntimeConfig(config.runtimeConfig);
if (typeof this.config.runtimeConfig === 'object') {
setRuntimeConfig(this.config.runtimeConfig);
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/shuvi/src/cli/apis/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path';
import fse from 'fs-extra';
import { IConfig } from '@shuvi/types';
import formatWebpackMessages from '@shuvi/toolpack/lib/utils/formatWebpackMessages';
import { Api } from '../../api/api';
import { IConfig } from '../../config';
import { getBundler } from '../../bundler/bundler';
import { Renderer } from '../../renderer';
import { BUILD_CLIENT_DIR } from '../../constants';
Expand All @@ -12,7 +12,7 @@ export interface IBuildOptions {
}

const defaultBuildOptions = {
target: 'ssr',
target: 'ssr'
} as const;

async function bundle({ api }: { api: Api }) {
Expand Down Expand Up @@ -43,22 +43,22 @@ function copyPublicFolder(api: Api) {
api.paths.publicDir,
path.join(api.paths.buildDir, BUILD_CLIENT_DIR),
{
dereference: true,
dereference: true
}
);
}

async function buildHtml({
api,
pathname,
filename,
filename
}: {
api: Api;
pathname: string;
filename: string;
}) {
const html = await new Renderer({ api }).renderDocument({
url: pathname,
url: pathname
});
await fse.writeFile(
path.resolve(api.paths.buildDir, BUILD_CLIENT_DIR, filename),
Expand All @@ -72,7 +72,7 @@ export async function build(
) {
const opts: IBuildOptions = {
...defaultBuildOptions,
...options,
...options
};
const api = new Api({ mode: 'production', config });

Expand Down
7 changes: 3 additions & 4 deletions packages/shuvi/src/cli/cmds/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import program from 'commander';
import { IConfig } from '@shuvi/types';
import { loadConfig } from '../../config';
import { loadConfig, IConfig } from '../../config';
import { build } from '../apis/build';
//@ts-ignore
import pkgInfo from '../../../package.json';
Expand All @@ -16,7 +15,7 @@ const CliConfigMap: Record<string, string | ((config: any) => void)> = {
routerHistory: 'router.history',
target(config) {
config.ssr = false;
},
}
};

function set(obj: any, path: string, value: any) {
Expand All @@ -32,7 +31,7 @@ function set(obj: any, path: string, value: any) {
}

function applyCliOptions(cliOptions: Record<string, any>, config: IConfig) {
Object.keys(CliConfigMap).forEach((key) => {
Object.keys(CliConfigMap).forEach(key => {
if (typeof program[key] !== 'undefined') {
const value = CliConfigMap[key];
if (typeof value === 'function') {
Expand Down
15 changes: 8 additions & 7 deletions packages/shuvi/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { IConfig } from '@shuvi/types';
import { deepmerge } from '@shuvi/utils/lib/deepmerge';
import { IApiConfig } from '@shuvi/types';
import path from 'path';
import { CONFIG_FILE, PUBLIC_PATH } from '../constants';

export const defaultConfig: IConfig = {
export type IConfig = Partial<IApiConfig>

export const defaultConfig: IApiConfig = {
ssr: true,
env: {},
rootDir: process.cwd(),
outputPath: 'dist',
publicPath: PUBLIC_PATH,
router: {
history: 'auto',
},
history: 'auto'
}
};

async function loadConfigFromFile<T>(configPath: string): Promise<T> {
Expand All @@ -36,11 +37,11 @@ async function loadConfigFromFile<T>(configPath: string): Promise<T> {
export async function loadConfig(
dir: string = process.cwd()
): Promise<IConfig> {
const config = await loadConfigFromFile<Partial<IConfig>>(
const config = await loadConfigFromFile<IConfig>(
path.join(dir, CONFIG_FILE)
);

config.rootDir = dir;

return deepmerge(defaultConfig, config);
return config;
}
8 changes: 3 additions & 5 deletions packages/shuvi/src/shuvi/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { IConfig } from '@shuvi/types';
import { deepmerge } from '@shuvi/utils/lib/deepmerge';
import { defaultConfig } from '../config';
import { IConfig } from '../config';
import Shuvi, { IShuviConstructorOptions } from './shuvi.base';

export { Shuvi };

export interface ShuviOptions {
dev?: boolean;
config: Partial<IConfig>;
config: IConfig;
}

export function shuvi({ dev = false, config }: ShuviOptions): Shuvi {
Expand All @@ -18,5 +16,5 @@ export function shuvi({ dev = false, config }: ShuviOptions): Shuvi {
ShuviCtor = require('./shuvi.prod').default;
}

return new ShuviCtor({ config: deepmerge(defaultConfig, config) });
return new ShuviCtor({ config });
}
3 changes: 2 additions & 1 deletion packages/shuvi/src/shuvi/shuvi.base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IConfig, IShuviMode } from '@shuvi/types';
import { IShuviMode } from '@shuvi/types';
import {
IHTTPRequestHandler,
IIncomingMessage,
IServerResponse
} from '../server';
import { Api } from '../api';
import { IConfig } from '../config';
import { Renderer, isRedirect } from '../renderer';

export interface IShuviConstructorOptions {
Expand Down
4 changes: 2 additions & 2 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type IPluginConfig =

export type IRuntimeConfig = Record<string, string>;

export interface IConfig {
export interface IApiConfig {
outputPath: string;
rootDir: string;
ssr: boolean;
Expand Down Expand Up @@ -93,7 +93,7 @@ export interface ICallHookOpts<Name extends string = string, InitV = unknown> {
export interface IApi {
mode: IShuviMode;
paths: IPaths;
config: IConfig;
config: IApiConfig;

tap<Config extends IHookConfig>(
hook: Config['name'],
Expand Down
6 changes: 3 additions & 3 deletions test/utils/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IConfig } from '@shuvi/types';
import { IBuildOptions, build as shuviBuild } from 'shuvi/lib/cli/build';
import { IApiConfig } from '@shuvi/types';
import { IBuildOptions, build as shuviBuild } from 'shuvi/lib/cli/apis/build';

export async function build(config: IConfig, options?: IBuildOptions) {
export async function build(config: IApiConfig, options?: IBuildOptions) {
await shuviBuild(config, options);
}
8 changes: 4 additions & 4 deletions test/utils/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path';
import { IConfig } from '@shuvi/types';
import { IApiConfig } from '@shuvi/types';
import { deepmerge } from '@shuvi/utils/lib/deepmerge';
import { loadConfig } from 'shuvi/lib/config';
import { build } from './build';

export async function loadFixture(
fixture: string,
overrides: Partial<IConfig> = {}
): Promise<IConfig> {
overrides: Partial<IApiConfig> = {}
): Promise<IApiConfig> {
const rootDir = path.resolve(__dirname, '..', 'fixtures', fixture);
const config = await loadConfig(rootDir);

Expand All @@ -16,7 +16,7 @@ export async function loadFixture(

export async function buildFixture(
fixture: string,
overrides: Partial<IConfig> = {}
overrides: Partial<IApiConfig> = {}
) {
const config = await loadFixture(fixture, overrides);
await build(config);
Expand Down
6 changes: 3 additions & 3 deletions test/utils/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import qs from 'querystring';
import { IConfig } from '@shuvi/types';
import { IApiConfig } from '@shuvi/types';
import { shuvi, Shuvi } from 'shuvi';
import { loadFixture } from './fixture';
import { build } from './build';
Expand Down Expand Up @@ -43,7 +43,7 @@ async function createTextContext(app: Shuvi) {

export async function launchFixture(
name: string,
overrides: Partial<IConfig> = {}
overrides: Partial<IApiConfig> = {}
): Promise<AppCtx> {
const config = await loadFixture(name, overrides);
const shuviApp = shuvi({ dev: true, config });
Expand All @@ -52,7 +52,7 @@ export async function launchFixture(

export async function serveFixture(
name: string,
overrides: Partial<IConfig> = {}
overrides: Partial<IApiConfig> = {}
): Promise<AppCtx> {
const config = await loadFixture(name, overrides);
await build(config);
Expand Down

0 comments on commit 70fb076

Please sign in to comment.