Skip to content

Commit

Permalink
Initial exploration of startService API
Browse files Browse the repository at this point in the history
  • Loading branch information
ggoodman committed May 2, 2022
1 parent 241a0d2 commit fe2f06a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/Graph.ts
Expand Up @@ -166,9 +166,20 @@ export default class Graph {
throw new Error('You must supply options.input to rollup');
}
for (const module of this.modulesById.values()) {
if (module instanceof Module) {
this.ensureModule(module);
}
}

ensureModule(module: Module | ExternalModule) {
// Make sure that the same module / external module can only be added to the
// graph once since it may be requested multiple times over the life of a
// service.
if (module instanceof Module) {
if (this.modules.indexOf(module) === -1) {
this.modules.push(module);
} else {
}
} else {
if (this.externalModules.indexOf(module) === -1) {
this.externalModules.push(module);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ModuleLoader.ts
Expand Up @@ -40,6 +40,7 @@ import transform from './utils/transform';
export interface UnresolvedModule {
fileName: string | null;
id: string;
implicitlyLoadedAfter?: string[];
importer: string | undefined;
name: string | null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/browser-entry.ts
@@ -1,2 +1,2 @@
export { default as rollup, defineConfig } from './rollup/rollup';
export { default as rollup, defineConfig, startService } from './rollup/rollup';
export { version as VERSION } from 'package.json';
2 changes: 1 addition & 1 deletion src/node-entry.ts
@@ -1,3 +1,3 @@
export { default as rollup, defineConfig } from './rollup/rollup';
export { default as rollup, defineConfig, startService } from './rollup/rollup';
export { default as watch } from './watch/watch-proxy';
export { version as VERSION } from 'package.json';
65 changes: 62 additions & 3 deletions src/rollup/rollup.ts
Expand Up @@ -23,22 +23,62 @@ import type {
RollupBuild,
RollupOptions,
RollupOutput,
RollupService,
RollupWatcher
} from './types';

export default function rollup(rawInputOptions: GenericConfigObject): Promise<RollupBuild> {
return rollupInternal(rawInputOptions, null);
}

export async function rollupInternal(
export async function startService(
rawInputOptions: GenericConfigObject,
watcher: RollupWatcher | null
): Promise<RollupBuild> {
): Promise<RollupService> {
const { graph, inputOptions } = await graphSetup(rawInputOptions, watcher);

const service: RollupService = {
async close(err?: any) {
if (service.closed) return;

service.closed = true;

if (err) {
const watchFiles = Object.keys(graph.watchFiles);
if (watchFiles.length > 0) {
err.watchFiles = watchFiles;
}
}

await graph.pluginDriver.hookParallel('buildEnd', err ? [err] : []);
await graph.pluginDriver.hookParallel('closeBundle', []);
},
closed: false,
resolve(source: string, importer?: string | undefined) {
return graph.moduleLoader.resolveId(source, importer, undefined, false);
},
load(id: string) {
return graph.moduleLoader.preloadModule({ id, resolveDependencies: true });
}
};

await catchUnfinishedHookActions(graph.pluginDriver, async () => {
try {
await graph.pluginDriver.hookParallel('buildStart', [inputOptions]);
} catch (err: any) {
await service.close(err);
throw err;
}
});

return service;
}

async function graphSetup(rawInputOptions: GenericConfigObject, watcher: RollupWatcher | null) {
const { options: inputOptions, unsetOptions: unsetInputOptions } = await getInputOptions(
rawInputOptions,
watcher !== null
);
initialiseTimers(inputOptions);

const graph = new Graph(inputOptions, watcher);

Expand All @@ -47,6 +87,25 @@ export async function rollupInternal(
delete inputOptions.cache;
delete rawInputOptions.cache;

return {
graph,
inputOptions,
unsetInputOptions,
useCache
};
}

export async function rollupInternal(
rawInputOptions: GenericConfigObject,
watcher: RollupWatcher | null
): Promise<RollupBuild> {
const { graph, inputOptions, unsetInputOptions, useCache } = await graphSetup(
rawInputOptions,
watcher
);

initialiseTimers(inputOptions);

timeStart('BUILD', 1);

await catchUnfinishedHookActions(graph.pluginDriver, async () => {
Expand Down
13 changes: 13 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -830,6 +830,17 @@ export interface RollupBuild {
write: (options: OutputOptions) => Promise<RollupOutput>;
}

export interface RollupService {
close: (err?: any) => Promise<void>;
closed: boolean;
load(id: string): Promise<ModuleInfo>;
resolve(
source: string,
importer?: string,
options?: { custom?: CustomPluginOptions; isEntry?: boolean }
): Promise<ResolvedId | null>;
}

export interface RollupOptions extends InputOptions {
// This is included for compatibility with config files but ignored by rollup.rollup
output?: OutputOptions | OutputOptions[];
Expand All @@ -841,6 +852,8 @@ export interface MergedRollupOptions extends InputOptions {

export function rollup(options: RollupOptions): Promise<RollupBuild>;

export function startService(options: RollupOptions): Promise<RollupService>;

export interface ChokidarOptions {
alwaysStat?: boolean;
atomic?: boolean | number;
Expand Down

0 comments on commit fe2f06a

Please sign in to comment.