|
1 |
| -import path from 'path' |
| 1 | +import { readFile } from 'fs/promises' |
2 | 2 |
|
3 | 3 | type Options = {
|
4 | 4 | prefix?: string
|
5 | 5 | }
|
| 6 | +const PLUGIN_ID = 'rollup-plugin-inline-code' |
6 | 7 |
|
7 | 8 | export default (options: Options = {}) => {
|
8 | 9 | const { prefix = 'inline!' } = options
|
9 | 10 |
|
10 |
| - const paths = new Map() |
11 |
| - |
12 | 11 | return {
|
13 | 12 | name: 'rollup-plugin-inline-code',
|
14 |
| - resolveId: (sourcePath: string) => { |
| 13 | + resolveId: async function ( |
| 14 | + sourcePath: string, |
| 15 | + importer: string | undefined, |
| 16 | + options: { |
| 17 | + attribute: Record<string, string> |
| 18 | + custom: { [plugin: string]: any } |
| 19 | + isEntry: boolean |
| 20 | + }, |
| 21 | + ): Promise<{ id: string; moduleSideEffects: boolean } | null> { |
15 | 22 | if (sourcePath.includes(prefix)) {
|
16 | 23 | const sourceArray = sourcePath.split(prefix)
|
17 | 24 | const name = sourceArray[sourceArray.length - 1]
|
18 |
| - |
19 |
| - // target - name |
20 |
| - paths.set(name, name) |
21 |
| - |
22 |
| - return name |
| 25 | + //@ts-ignore |
| 26 | + const resolvePath = await this.resolve(name, importer, options) |
| 27 | + return { id: `\0${PLUGIN_ID}:${resolvePath.id}`, moduleSideEffects: true } |
23 | 28 | }
|
24 | 29 | return null
|
25 | 30 | },
|
26 |
| - transform: (codeContent: string, id: string | null) => { |
27 |
| - if (!paths.has(id)) { |
| 31 | + load: async function (id: string) { |
| 32 | + if (!id.startsWith(`\0${PLUGIN_ID}:`)) { |
28 | 33 | return null
|
29 | 34 | }
|
30 | 35 |
|
31 |
| - const code = `export default ${JSON.stringify(codeContent.trim())};` |
32 |
| - const map = { mappings: '' } |
33 |
| - |
34 |
| - return { |
35 |
| - code, |
36 |
| - map, |
37 |
| - } |
| 36 | + const filePath = id.slice(`\0${PLUGIN_ID}:`.length) |
| 37 | + const code = await readFile(filePath, 'utf-8') |
| 38 | + return `export default ${JSON.stringify(code)};` |
38 | 39 | },
|
39 | 40 | }
|
40 | 41 | }
|
0 commit comments