Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial exploration of startService API #4484

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/Bundle.ts
Expand Up @@ -41,15 +41,37 @@ export default class Bundle {
private readonly graph: Graph
) {}

static async fromModules(
outputOptions: NormalizedOutputOptions,
unsetOptions: ReadonlySet<string>,
inputOptions: NormalizedInputOptions,
pluginDriver: PluginDriver,
graph: Graph,
entryModules: Module[],
modulesById: Map<string, Module | ExternalModule>
) {
const bundle = new Bundle(outputOptions, unsetOptions, inputOptions, pluginDriver, graph);

return bundle.generateInternal(false, entryModules, modulesById);
}

async generate(isWrite: boolean): Promise<OutputBundle> {
return this.generateInternal(isWrite);
}

async generateInternal(
isWrite: boolean,
entryModules = this.graph.entryModules,
modulesById = this.graph.modulesById
) {
timeStart('GENERATE', 1);
const outputBundle: OutputBundleWithPlaceholders = Object.create(null);
this.pluginDriver.setOutputBundle(outputBundle, this.outputOptions, this.facadeChunkByModule);
try {
await this.pluginDriver.hookParallel('renderStart', [this.outputOptions, this.inputOptions]);

timeStart('generate chunks', 2);
const chunks = await this.generateChunks();
const chunks = await this.generateChunks(entryModules, modulesById);
if (chunks.length > 1) {
validateOptionsForMultiChunkOutput(this.outputOptions, this.inputOptions.onwarn);
}
Expand Down Expand Up @@ -155,13 +177,16 @@ export default class Bundle {
}
}

private assignManualChunks(getManualChunk: GetManualChunk): Map<Module, string> {
private assignManualChunks(
getManualChunk: GetManualChunk,
modulesById: Map<string, Module | ExternalModule>
): Map<Module, string> {
const manualChunkAliasesWithEntry: [alias: string, module: Module][] = [];
const manualChunksApi = {
getModuleIds: () => this.graph.modulesById.keys(),
getModuleIds: () => modulesById.keys(),
getModuleInfo: this.graph.getModuleInfo
};
for (const module of this.graph.modulesById.values()) {
for (const module of modulesById.values()) {
if (module instanceof Module) {
const manualChunkAlias = getManualChunk(module.id, manualChunksApi);
if (typeof manualChunkAlias === 'string') {
Expand Down Expand Up @@ -203,30 +228,33 @@ export default class Bundle {
this.pluginDriver.finaliseAssets();
}

private async generateChunks(): Promise<Chunk[]> {
private async generateChunks(
entryModules = this.graph.entryModules,
modulesById = this.graph.modulesById
): Promise<Chunk[]> {
const { manualChunks } = this.outputOptions;
const manualChunkAliasByEntry =
typeof manualChunks === 'object'
? await this.addManualChunks(manualChunks)
: this.assignManualChunks(manualChunks);
: this.assignManualChunks(manualChunks, modulesById);
const chunks: Chunk[] = [];
const chunkByModule = new Map<Module, Chunk>();
for (const { alias, modules } of this.outputOptions.inlineDynamicImports
? [{ alias: null, modules: getIncludedModules(this.graph.modulesById) }]
? [{ alias: null, modules: getIncludedModules(modulesById) }]
: this.outputOptions.preserveModules
? getIncludedModules(this.graph.modulesById).map(module => ({
? getIncludedModules(modulesById).map(module => ({
alias: null,
modules: [module]
}))
: getChunkAssignments(this.graph.entryModules, manualChunkAliasByEntry)) {
: getChunkAssignments(entryModules, manualChunkAliasByEntry)) {
sortByExecutionOrder(modules);
const chunk = new Chunk(
modules,
this.inputOptions,
this.outputOptions,
this.unsetOptions,
this.pluginDriver,
this.graph.modulesById,
modulesById,
chunkByModule,
this.facadeChunkByModule,
this.includedNamespaces,
Expand Down
18 changes: 13 additions & 5 deletions src/Graph.ts
Expand Up @@ -135,6 +135,18 @@ export default class Graph {
return ast;
}

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.
const modules: Array<Module | ExternalModule> =
module instanceof Module ? this.modules : this.externalModules;

if (!modules.includes(module)) {
modules.push(module);
}
}
ggoodman marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +138 to +148
Copy link
Member

Choose a reason for hiding this comment

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

Just convert this.modules to a Set instead of doing this, should also have better performance for large bundles. We are mostly iterating over them, which works just as well for a Set, and it is a private variable anyway.

Choose a reason for hiding this comment

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


getCache(): RollupCache {
// handle plugin cache eviction
for (const name in this.pluginCache) {
Expand Down Expand Up @@ -166,11 +178,7 @@ 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.modules.push(module);
} else {
this.externalModules.push(module);
}
this.ensureModule(module);
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/Module.ts
Expand Up @@ -676,12 +676,16 @@ export default class Module {
this.addModulesToImportDescriptions(this.reexportDescriptions);
const externalExportAllModules: ExternalModule[] = [];
for (const source of this.exportAllSources) {
const module = this.graph.modulesById.get(this.resolvedIds[source].id)!;
const module = this.graph.modulesById.get(this.resolvedIds[source].id);
if (module instanceof ExternalModule) {
externalExportAllModules.push(module);
continue;
} else if (module instanceof Module) {
this.exportAllModules.push(module);
} else {
externalExportAllModules.push(
new ExternalModule(this.options, this.resolvedIds[source].id, true, {}, true)
);
}
this.exportAllModules.push(module);
}
this.exportAllModules.push(...externalExportAllModules);
}
Expand Down Expand Up @@ -1019,7 +1023,8 @@ export default class Module {
): void {
for (const specifier of importDescription.values()) {
const { id } = this.resolvedIds[specifier.source];
specifier.module = this.graph.modulesById.get(id)!;
specifier.module =
this.graph.modulesById.get(id) ?? new ExternalModule(this.options, id, true, {}, true);
}
}

Expand Down
8 changes: 6 additions & 2 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 Expand Up @@ -176,12 +177,15 @@ export class ModuleLoader {
}

public async preloadModule(
resolvedId: { id: string; resolveDependencies?: boolean } & Partial<PartialNull<ModuleOptions>>
resolvedId: { id: string; isEntry?: boolean; resolveDependencies?: boolean } & Partial<
PartialNull<ModuleOptions>
>
): Promise<ModuleInfo> {
const isEntry = resolvedId.isEntry !== false;
const module = await this.fetchModule(
this.getResolvedIdWithDefaults(resolvedId)!,
undefined,
false,
isEntry,
resolvedId.resolveDependencies ? RESOLVE_DEPENDENCIES : true
);
return module.info;
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';