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

fix(compiler): support rollup's external input option for collection dependencies #3839

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/compiler/build/build-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class BuildContext implements d.BuildCtx {
indexBuildCount = 0;
indexDoc: Document = undefined;
isRebuild = false;
isExternal: (id: string, importer: string, isResolved: boolean) => boolean;
moduleFiles: d.Module[] = [];
outputs: d.BuildOutput[] = [];
packageJson: d.PackageJsonData = {};
Expand All @@ -68,6 +69,7 @@ export class BuildContext implements d.BuildCtx {
this.buildId = ++this.compilerCtx.activeBuildId;

this.debug = config.logger.debug.bind(config.logger);
this.isExternal = getIdMatcher(config.rollupConfig?.inputOptions?.external);
}

start() {
Expand Down Expand Up @@ -215,6 +217,46 @@ export const getBuildTimestamp = () => {
return timestamp;
};

export function ensureArray<T>(items: (T | false | null | undefined)[] | T | false | null | undefined): T[] {
if (Array.isArray(items)) {
return items.filter(Boolean) as T[];
}
if (items) {
return [items];
}
return [];
}

const getIdMatcher = <T extends Array<any>>(
option:
| undefined
| boolean
| string
| RegExp
| (string | RegExp)[]
| ((id: string, ...parameters: T) => boolean | null | void)
): ((id: string, ...parameters: T) => boolean) => {
if (option === true) {
return () => true;
}
if (typeof option === 'function') {
return (id, ...parameters) => (!id.startsWith('\0') && option(id, ...parameters)) || false;
}
if (option) {
const ids = new Set<string>();
const matchers: RegExp[] = [];
for (const value of ensureArray(option)) {
if (value instanceof RegExp) {
matchers.push(value);
} else {
ids.add(value);
}
}
return (id: string, ..._arguments) => ids.has(id) || matchers.some((matcher) => matcher.test(id));
}
return () => false;
};

const getProgress = (completedTasks: d.BuildTask[]) => {
let progressIndex = 0;
const taskKeys = Object.keys(ProgressTask);
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/bundle/bundle-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export const getRollupOptions = (
onwarn: createOnWarnFn(buildCtx.diagnostics),

cache: compilerCtx.rollupCache.get(bundleOpts.id),

external: config.rollupConfig?.inputOptions?.external,
};

return rollupOptions;
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/config/test/validate-rollup-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('validateStats', () => {
config.rollupConfig = {
inputOptions: {
context: 'window',
external: 'external_symbol',
notAnOption: {},
},
outputOptions: {
Expand All @@ -76,6 +77,7 @@ describe('validateStats', () => {
rollupConfig: {
inputOptions: {
context: 'window',
external: 'external_symbol',
},
outputOptions: {
globals: {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/config/validate-rollup-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getCleanRollupConfig = (rollupConfig: d.RollupConfig): d.RollupConfig => {
if (rollupConfig.inputOptions && isObject(rollupConfig.inputOptions)) {
cleanRollupConfig = {
...cleanRollupConfig,
inputOptions: pluck(rollupConfig.inputOptions, ['context', 'moduleContext', 'treeshake']),
inputOptions: pluck(rollupConfig.inputOptions, ['context', 'moduleContext', 'treeshake', 'external']),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const parseCollectionManifest = (

const compilerVersion: d.CollectionCompilerVersion = collectionManifest.compiler || ({} as any);

const isExternal = buildCtx.isExternal(collectionName, 'collection', true);

const collection: d.CollectionCompilerMeta = {
collectionName: collectionName,
moduleId: collectionName,
Expand All @@ -27,10 +29,13 @@ export const parseCollectionManifest = (
typescriptVersion: compilerVersion.typescriptVersion || '',
},
bundles: parseBundles(collectionManifest),
isExternal,
};

parseGlobal(config, compilerCtx, buildCtx, collectionDir, collectionManifest, collection);
parseCollectionComponents(config, compilerCtx, buildCtx, collectionDir, collectionManifest, collection);
if (!isExternal) {
parseGlobal(config, compilerCtx, buildCtx, collectionDir, collectionManifest, collection);
parseCollectionComponents(config, compilerCtx, buildCtx, collectionDir, collectionManifest, collection);
}

return collection;
};
Expand Down
2 changes: 2 additions & 0 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export interface BuildCtx {
indexBuildCount: number;
indexDoc: Document;
isRebuild: boolean;
isExternal: (id: string, importer: string, isResolved: boolean) => boolean;
moduleFiles: Module[];
packageJson: PackageJsonData;
pendingCopyTasks: Promise<CopyResults>[];
Expand Down Expand Up @@ -474,6 +475,7 @@ export interface CollectionCompilerMeta {
bundles?: {
components: string[];
}[];
isExternal?: boolean;
}

export interface CollectionCompilerVersion {
Expand Down
5 changes: 5 additions & 0 deletions src/declarations/stencil-public-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,11 @@ export interface RollupInputOptions {
context?: string;
moduleContext?: ((id: string) => string) | { [id: string]: string };
treeshake?: boolean;
external?:
| (string | RegExp)[]
| string
| RegExp
| ((source: string, importer: string | undefined, isResolved: boolean) => boolean | null | undefined);
}

export interface RollupOutputOptions {
Expand Down