Skip to content

Commit 9446b1a

Browse files
committed
keep plugin as object instead of destructuring
to preserve `this` context within the object methods
1 parent 50f1234 commit 9446b1a

File tree

1 file changed

+10
-12
lines changed
  • packages/rollup-plugin/src

1 file changed

+10
-12
lines changed

packages/rollup-plugin/src/lib.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import MagicString, { Bundle as MagicStringBundle } from 'magic-string';
33
import type { ModuleInfo, PluginContext } from 'rollup';
44

55
/** Generate a CSS bundle from Rollup context */
6-
export function generateCssBundle({
7-
getModuleIds,
8-
getModuleInfo,
9-
warn,
10-
}: Pick<PluginContext, 'getModuleIds' | 'getModuleInfo' | 'warn'>): {
6+
export function generateCssBundle(
7+
plugin: Pick<PluginContext, 'getModuleIds' | 'getModuleInfo' | 'warn'>,
8+
): {
119
bundle: MagicStringBundle;
1210
extractedCssIds: Set<string>;
1311
} {
@@ -16,15 +14,15 @@ export function generateCssBundle({
1614

1715
// 1. identify CSS files to bundle
1816
const cssFiles: Record<string, ImportChain> = {};
19-
for (const id of getModuleIds()) {
17+
for (const id of plugin.getModuleIds()) {
2018
if (cssFileFilter.test(id)) {
21-
cssFiles[id] = buildImportChain(id, { getModuleInfo, warn });
19+
cssFiles[id] = buildImportChain(id, plugin);
2220
}
2321
}
2422

2523
// 2. build bundle from import order
2624
for (const id of sortModules(cssFiles)) {
27-
const { importedIdResolutions } = getModuleInfo(id) ?? {};
25+
const { importedIdResolutions } = plugin.getModuleInfo(id) ?? {};
2826
for (const resolution of importedIdResolutions ?? []) {
2927
if (resolution.meta.css && !extractedCssIds.has(resolution.id)) {
3028
extractedCssIds.add(resolution.id);
@@ -45,9 +43,9 @@ export type ImportChain = [id: string, order: number][];
4543
/** Trace a file back through its importers, building an ordered list */
4644
export function buildImportChain(
4745
id: string,
48-
{ getModuleInfo, warn }: Pick<PluginContext, 'getModuleInfo' | 'warn'>,
46+
plugin: Pick<PluginContext, 'getModuleInfo' | 'warn'>,
4947
): ImportChain {
50-
let mod: ModuleInfo | null = getModuleInfo(id)!;
48+
let mod: ModuleInfo | null = plugin.getModuleInfo(id)!;
5149
if (!mod) {
5250
return [];
5351
}
@@ -61,14 +59,14 @@ export function buildImportChain(
6159
break;
6260
}
6361
if (chain.some(([id]) => id === lastImporterId)) {
64-
warn(
62+
plugin.warn(
6563
`Circular import detected. Can’t determine ideal import order of module.\n${chain
6664
.reverse()
6765
.join('\n → ')}`,
6866
);
6967
break;
7068
}
71-
mod = getModuleInfo(lastImporterId);
69+
mod = plugin.getModuleInfo(lastImporterId);
7270
if (!mod) {
7371
break;
7472
}

0 commit comments

Comments
 (0)