Skip to content

fix: resolve relative paths #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<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]

// 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)};`
},
}
}