Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

Commit 024e993

Browse files
committed
feat: prefix node_modules imports with npm:
1 parent 3121c9e commit 024e993

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"test": "pnpm lint && tsc --noEmit && vitest run"
2323
},
2424
"dependencies": {
25-
"@rollup/plugin-inject": "^5.0.1"
25+
"@rollup/plugin-inject": "^5.0.1",
26+
"magic-string": "^0.26.7",
27+
"mlly": "^0.5.16"
2628
},
2729
"devDependencies": {
2830
"@nuxtjs/eslint-config-typescript": "^11.0.0",

pnpm-lock.yaml

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { dirname, resolve } from 'node:path'
1+
import { dirname, isAbsolute, resolve } from 'node:path'
22
import { builtinModules } from 'node:module'
33
import { fileURLToPath } from 'node:url'
44

5+
import MagicString from 'magic-string'
6+
import { findStaticImports } from 'mlly'
57
import inject from '@rollup/plugin-inject'
68

79
const pluginDir = dirname(fileURLToPath(import.meta.url))
@@ -39,6 +41,27 @@ export default function rollupPluginNodeDeno () {
3941
id: resolve(pluginDir, 'deno/empty.mjs')
4042
}
4143
}
44+
if (isHTTPImport(id)) {
45+
return {
46+
id,
47+
external: true
48+
}
49+
}
50+
},
51+
renderChunk (code) {
52+
const s = new MagicString(code)
53+
const imports = findStaticImports(code)
54+
for (const i of imports) {
55+
if (!i.specifier.startsWith('.') && !isAbsolute(i.specifier) && !isHTTPImport(i.specifier) && !i.specifier.startsWith('npm:')) {
56+
s.replace(i.code, i.code.replace(new RegExp(`(?<quote>['"])${i.specifier}\\k<quote>`), JSON.stringify(resolveDeno(i.specifier) ?? ('npm:' + i.specifier))))
57+
}
58+
}
59+
if (s.hasChanged()) {
60+
return {
61+
code: s.toString(),
62+
map: s.generateMap({ includeContent: true })
63+
}
64+
}
4265
}
4366
} as import('rollup').Plugin
4467
}
@@ -91,3 +114,7 @@ const denoExtras = [
91114
'node-fetch',
92115
'chalk'
93116
]
117+
118+
function isHTTPImport (id: string) {
119+
return id.startsWith('https://') || id.startsWith('http://')
120+
}

0 commit comments

Comments
 (0)