Skip to content

Commit

Permalink
cleanup startDevServer() interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Oct 18, 2020
1 parent 07a1216 commit 6c1dad9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 57 deletions.
4 changes: 2 additions & 2 deletions plugins/web-test-runner-plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To Resolve:
name: 'snowpack-plugin',
async serverStart({fileWatcher}) {
fileWatcher.add(Object.keys(config.mount));
server = await snowpack.startServer({
server = await snowpack.startDevServer({
cwd,
config,
lockfile: null,
Expand Down Expand Up @@ -50,7 +50,7 @@ To Resolve:
source.indexOf('?') === -1 ? undefined : source.indexOf('?'),
);
const sourcePath = path.join(cwd, reqPath);
const mountedUrl = snowpack.unstable__getUrlForFile(sourcePath, config);
const mountedUrl = snowpack.getUrlForFile(sourcePath, config);
if (!mountedUrl) {
throw new Error(`${source} could not be mounted!`);
}
Expand Down
53 changes: 4 additions & 49 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
transformFileImports,
} from '../rewrite-imports';
import {matchDynamicImportValue} from '../scan-imports';
import {CommandOptions, ImportMap, SnowpackBuildMap} from '../types/snowpack';
import {CommandOptions, ImportMap, SnowpackBuildMap, LoadResult, SnowpackDevServer} from '../types/snowpack';
import {
BUILD_CACHE,
checkLockfileHash,
Expand All @@ -84,52 +84,6 @@ import {getInstallTargets, run as installRunner} from './install';
import {getPort, getServerInfoMessage, paintDashboard, paintEvent} from './paint';
import {isBinaryFile} from 'isbinaryfile';

interface LoadResult<T = Buffer | string> {
contents: T;
originalFileLoc: string | null;
responseFileName: string;
checkStale?: () => Promise<void>;
}

export interface ServerResult {
loadUrl: {
(
reqUrl: string,
opt?:
| {
isSSR?: boolean | undefined;
allowStale?: boolean | undefined;
encoding?: undefined;
}
| undefined,
): Promise<LoadResult<Buffer | string>>;
(
reqUrl: string,
opt: {
isSSR?: boolean;
allowStale?: boolean;
encoding: BufferEncoding;
},
): Promise<LoadResult<string>>;
(
reqUrl: string,
opt: {
isSSR?: boolean;
allowStale?: boolean;
encoding: null;
},
): Promise<LoadResult<Buffer>>;
};
handleRequest: (
req: http.IncomingMessage,
res: http.ServerResponse,
options?: {handleError?: boolean},
) => Promise<void>;
sendResponseFile: typeof sendResponseFile;
sendResponseError: typeof sendResponseError;
shutdown(): Promise<void>;
}

interface FoundFile {
fileLoc: string;
isStatic: boolean;
Expand Down Expand Up @@ -325,7 +279,7 @@ function handleResponseError(req, res, err: Error | NotFoundError) {
return;
}

export async function startServer(commandOptions: CommandOptions): Promise<ServerResult> {
export async function startDevServer(commandOptions: CommandOptions): Promise<SnowpackDevServer> {
// Start the startup timer!
let serverStart = performance.now();

Expand Down Expand Up @@ -1433,6 +1387,7 @@ export async function startServer(commandOptions: CommandOptions): Promise<Serve
depWatcher.on('unlink', onDepWatchEvent);

return {
port,
loadUrl,
handleRequest,
sendResponseFile,
Expand All @@ -1446,7 +1401,7 @@ export async function startServer(commandOptions: CommandOptions): Promise<Serve

export async function command(commandOptions: CommandOptions) {
try {
await startServer(commandOptions);
await startDevServer(commandOptions);
} catch (err) {
logger.error(err.message);
logger.debug(err.stack);
Expand Down
14 changes: 8 additions & 6 deletions snowpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import {clearCache, readLockfile} from './util.js';
export * from './types/snowpack';

// Stable API:
export {startServer, ServerResult} from './commands/dev';
export {startDevServer} from './commands/dev';
export {loadAndValidateConfig};

// Unstable: These APIs are in progress and subject to change across minor versions
export {getUrlForFile as unstable__getUrlForFile} from './build/file-urls';
export {getUrlForFile} from './build/file-urls';

/** DEPRECATED: Promoted to startServer() **/
export const unstable__startServer = () => {
throw new Error(`[snowpack 2.15] unstable__startServer is now startServer`);
throw new Error(`[snowpack 2.15] unstable__startServer() is now startServer()`);
};
/** DEPRECATED: Promoted to loadAndValidateConfig() **/
export const unstable__loadAndValidateConfig = () => {
throw new Error(`[snowpack 2.15] unstable__loadAndValidateConfig is now loadAndValidateConfig`);
throw new Error(`[snowpack 2.15] unstable__loadAndValidateConfig() is now loadAndValidateConfig()`);
};
/** DEPRECATED: Promoted to getUrlForFile() **/
export const unstable__getUrlForFile = () => {
throw new Error(`[snowpack 2.15] unstable__getUrlForFile() is now getUrlForFile()`);
};

const cwd = process.cwd();
Expand Down
53 changes: 53 additions & 0 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,59 @@ export type DeepPartial<T> = {
: DeepPartial<T[P]>;
};


export interface LoadResult<T = Buffer | string> {
contents: T;
originalFileLoc: string | null;
responseFileName: string;
checkStale?: () => Promise<void>;
}

export interface SnowpackDevServer {
port: number;
loadUrl: {
(
reqUrl: string,
opt?:
| {
isSSR?: boolean | undefined;
allowStale?: boolean | undefined;
encoding?: undefined;
}
| undefined,
): Promise<LoadResult<Buffer | string>>;
(
reqUrl: string,
opt: {
isSSR?: boolean;
allowStale?: boolean;
encoding: BufferEncoding;
},
): Promise<LoadResult<string>>;
(
reqUrl: string,
opt: {
isSSR?: boolean;
allowStale?: boolean;
encoding: null;
},
): Promise<LoadResult<Buffer>>;
};
handleRequest: (
req: http.IncomingMessage,
res: http.ServerResponse,
options?: {handleError?: boolean},
) => Promise<void>;
sendResponseFile: (
req: http.IncomingMessage,
res: http.ServerResponse,
{contents, originalFileLoc, responseFileName}: LoadResult,
) => void;
sendResponseError: (req: http.IncomingMessage, res: http.ServerResponse, status: number) => void;
shutdown(): Promise<void>;
}


export type SnowpackBuiltFile = {
code: string | Buffer;
map?: string;
Expand Down

1 comment on commit 6c1dad9

@vercel
Copy link

@vercel vercel bot commented on 6c1dad9 Oct 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.