Skip to content

Commit 3a0b234

Browse files
committed
Properly resolve on build as well as dev
1 parent 585d7fe commit 3a0b234

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

.pnp.cjs

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

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"devDependencies": {
3636
"@types/node": "^18.7.23",
3737
"esbuild": "^0.15.10",
38+
"rollup": "2.78.1",
3839
"typescript": "4.8.3",
3940
"vite": "^3.1.4",
4041
"vite-plugin-externalize-deps": "^0.1.5",
@@ -45,6 +46,9 @@
4546
"@rollup/plugin-inject": "^4.0.4",
4647
"node-stdlib-browser": "^1.2.0"
4748
},
49+
"peerDependencies": {
50+
"vite": "^2.0.0 || ^3.0.0"
51+
},
4852
"publishConfig": {
4953
"access": "public"
5054
},

src/env.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ declare module 'node-stdlib-browser/helpers/esbuild/plugin' {
44

55
export default function(options: typeof stdLibBrowser): Plugin
66
}
7+
8+
declare module 'node-stdlib-browser/helpers/rollup/plugin' {
9+
import type { WarningHandlerWithDefault } from 'rollup'
10+
11+
export const handleCircularDependancyWarning: WarningHandlerWithDefault
12+
}

src/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRequire } from 'node:module'
22
import inject from '@rollup/plugin-inject'
33
import stdLibBrowser from 'node-stdlib-browser'
4+
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
45
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
56
import type { Plugin } from 'vite'
67

@@ -12,6 +13,22 @@ export const nodePolyfills = (_options = {}): Plugin => {
1213
name: 'vite-plugin-node-polyfills',
1314
config: (_config, _env) => {
1415
return {
16+
build: {
17+
rollupOptions: {
18+
onwarn: (warning, rollupWarn) => {
19+
handleCircularDependancyWarning(warning, rollupWarn)
20+
},
21+
plugins: [
22+
{
23+
...inject({
24+
Buffer: 'Buffer',
25+
global: 'global',
26+
process: 'process',
27+
}),
28+
},
29+
],
30+
},
31+
},
1532
optimizeDeps: {
1633
esbuildOptions: {
1734
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L203-L209
@@ -28,20 +45,11 @@ export const nodePolyfills = (_options = {}): Plugin => {
2845
],
2946
},
3047
},
31-
plugins: [
32-
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L155-L179
33-
{
34-
...inject({
35-
Buffer: [globalShims, 'Buffer'],
36-
global: [globalShims, 'global'],
37-
process: [globalShims, 'process'],
38-
}),
39-
enforce: 'post',
40-
}
41-
],
4248
resolve: {
4349
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150
44-
alias: stdLibBrowser,
50+
alias: {
51+
...stdLibBrowser,
52+
},
4553
},
4654
}
4755
},

test/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { resolve } from 'node:path'
1+
import { resolve } from 'path'
22

33
console.log(resolve('.'))

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default defineConfig({
2121
exports: 'named',
2222
format: 'cjs',
2323
inlineDynamicImports: true,
24-
interop: 'esModule',
24+
interop: 'auto',
2525
},
2626
],
2727
},

yarn.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ __metadata:
21092109
languageName: node
21102110
linkType: hard
21112111

2112-
"rollup@npm:~2.78.0":
2112+
"rollup@npm:2.78.1, rollup@npm:~2.78.0":
21132113
version: 2.78.1
21142114
resolution: "rollup@npm:2.78.1"
21152115
dependencies:
@@ -2502,6 +2502,7 @@ __metadata:
25022502
"@types/node": ^18.7.23
25032503
esbuild: ^0.15.10
25042504
node-stdlib-browser: ^1.2.0
2505+
rollup: 2.78.1
25052506
typescript: 4.8.3
25062507
vite: ^3.1.4
25072508
vite-plugin-externalize-deps: ^0.1.5

0 commit comments

Comments
 (0)