-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
createRollupPluginContextAdapter.ts
126 lines (111 loc) · 3.28 KB
/
createRollupPluginContextAdapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import path from 'path';
import {
DevServerCoreConfig,
FSWatcher,
Plugin as WdsPlugin,
Context,
ResolveOptions,
} from '@web/dev-server-core';
import {
PluginContext,
MinimalPluginContext,
TransformPluginContext,
CustomPluginOptions,
ModuleInfo,
} from 'rollup';
export function createRollupPluginContextAdapter<
T extends PluginContext | MinimalPluginContext | TransformPluginContext,
>(
pluginContext: T,
wdsPlugin: WdsPlugin,
config: DevServerCoreConfig,
fileWatcher: FSWatcher,
context: Context,
pluginMetaPerModule: Map<string, CustomPluginOptions>,
) {
return {
...pluginContext,
getModuleInfo(id: string): Partial<ModuleInfo> {
return {
id,
code: context.body as string,
ast: null,
dynamicallyImportedIds: [],
dynamicallyImportedIdResolutions: [],
dynamicImporters: [],
hasDefaultExport: false,
implicitlyLoadedBefore: [],
implicitlyLoadedAfterOneOf: [],
importedIds: [],
importedIdResolutions: [],
importers: [],
isEntry: false,
isExternal: false,
isIncluded: false,
hasModuleSideEffects: false,
moduleSideEffects: false,
syntheticNamedExports: false,
meta: pluginMetaPerModule.get(id) ?? {},
};
},
addWatchFile(id: string) {
const filePath = path.join(process.cwd(), id);
fileWatcher.add(filePath);
},
emitAsset() {
throw new Error('Emitting files is not yet supported');
},
emitFile() {
throw new Error('Emitting files is not yet supported');
},
getAssetFileName() {
throw new Error('Emitting files is not yet supported');
},
getFileName() {
throw new Error('Emitting files is not yet supported');
},
setAssetSource() {
throw new Error('Emitting files is not yet supported');
},
async resolve(source: string, importer: string, options: ResolveOptions) {
if (!context) throw new Error('Context is required.');
const { skipSelf, ...resolveOptions } = options;
if (skipSelf) wdsPlugin.resolveImportSkip?.(context, source, importer);
for (const pl of config.plugins ?? []) {
if (pl.resolveImport && (!skipSelf || pl !== wdsPlugin)) {
const result = await pl.resolveImport({
source,
context,
resolveOptions,
});
let resolvedId: string | undefined;
if (typeof result === 'string') {
resolvedId = result;
} else if (typeof result === 'object') {
resolvedId = result?.id;
}
if (resolvedId) {
const importerDir = path.dirname(importer);
return {
id: path.isAbsolute(resolvedId) ? resolvedId : path.join(importerDir, resolvedId),
};
}
}
}
},
async resolveId(
source: string,
importer: string,
options: { isEntry: boolean; skipSelf: boolean; custom: Record<string, unknown> },
) {
const resolveResult = await this.resolve(source, importer, options);
if (typeof resolveResult === 'string') {
return resolveResult;
}
if (typeof resolveResult === 'object') {
return resolveResult?.id;
}
return resolveResult;
},
};
}