-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
43 lines (39 loc) · 1.15 KB
/
index.ts
File metadata and controls
43 lines (39 loc) · 1.15 KB
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
import { readFile } from 'fs/promises'
type Options = {
prefix?: string
}
const PLUGIN_ID = 'rollup-plugin-inline-code'
export default (options: Options = {}) => {
const { prefix = 'inline!' } = options
return {
name: 'rollup-plugin-inline-code',
resolveId: async function (
sourcePath: string,
importer: string | undefined,
options: {
attribute: Record<string, string>
custom: { [plugin: string]: any }
isEntry: boolean
},
): Promise<{ id: string; moduleSideEffects: boolean } | null> {
if (sourcePath.includes(prefix)) {
const sourceArray = sourcePath.split(prefix)
const name = sourceArray[sourceArray.length - 1] ?? {
id: sourceArray[sourceArray.length - 1],
}
//@ts-ignore
const resolvePath = await this.resolve(name, importer, options)
return { id: `\0${PLUGIN_ID}:${resolvePath.id}`, moduleSideEffects: true }
}
return null
},
load: async function (id: string) {
if (!id.startsWith(`\0${PLUGIN_ID}:`)) {
return null
}
const filePath = id.slice(`\0${PLUGIN_ID}:`.length)
const code = await readFile(filePath, 'utf-8')
return `export default ${JSON.stringify(code)};`
},
}
}