Skip to content

Commit 31dcb12

Browse files
committed
fix: resolve relative paths
imports such as inline!../path/to/file don't work. Also it doesn't allow to import the same file with the same path without the prefix.
1 parent 909325e commit 31dcb12

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
import path from 'path'
1+
import { readFile } from 'fs/promises'
22

33
type Options = {
44
prefix?: string
55
}
6+
const PLUGIN_ID = 'rollup-plugin-inline-code'
67

78
export default (options: Options = {}) => {
89
const { prefix = 'inline!' } = options
910

10-
const paths = new Map()
11-
1211
return {
1312
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> {
1522
if (sourcePath.includes(prefix)) {
1623
const sourceArray = sourcePath.split(prefix)
1724
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 }
2328
}
2429
return null
2530
},
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}:`)) {
2833
return null
2934
}
3035

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)};`
3839
},
3940
}
4041
}

0 commit comments

Comments
 (0)