diff --git a/src/index.ts b/src/index.ts index 9d45df0..232ae22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,40 +1,41 @@ -import path from 'path' +import { readFile } from 'fs/promises' type Options = { prefix?: string } +const PLUGIN_ID = 'rollup-plugin-inline-code' export default (options: Options = {}) => { const { prefix = 'inline!' } = options - const paths = new Map() - return { name: 'rollup-plugin-inline-code', - resolveId: (sourcePath: string) => { + resolveId: async function ( + sourcePath: string, + importer: string | undefined, + options: { + attribute: Record + 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] - - // target - name - paths.set(name, name) - - return name + //@ts-ignore + const resolvePath = await this.resolve(name, importer, options) + return { id: `\0${PLUGIN_ID}:${resolvePath.id}`, moduleSideEffects: true } } return null }, - transform: (codeContent: string, id: string | null) => { - if (!paths.has(id)) { + load: async function (id: string) { + if (!id.startsWith(`\0${PLUGIN_ID}:`)) { return null } - const code = `export default ${JSON.stringify(codeContent.trim())};` - const map = { mappings: '' } - - return { - code, - map, - } + const filePath = id.slice(`\0${PLUGIN_ID}:`.length) + const code = await readFile(filePath, 'utf-8') + return `export default ${JSON.stringify(code)};` }, } }