Skip to content

Commit ab135fa

Browse files
committed
wip: stashing latest changes
1 parent 905b858 commit ab135fa

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ jobs:
2929
- uses: ./.github/actions/install-dependencies
3030
- run: pnpm build:core
3131
- run: pnpm build:shims
32-
- run: pnpm -r test
32+
- run: pnpm test:unit
33+
- run: pnpm test:build
3334
typecheck:
3435
runs-on: ubuntu-latest
3536
steps:

examples/vue/vite.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
44

55
// https://vitejs.dev/config/
66
export default defineConfig({
7+
build: {
8+
lib: {
9+
entry: './src/main.ts',
10+
fileName: 'index',
11+
formats: ['es', 'cjs'],
12+
},
13+
minify: false,
14+
},
715
plugins: [
816
nodePolyfills(),
917
vue(),

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@
9898
"dependencies": {
9999
"@rollup/plugin-inject": "^5.0.5",
100100
"browser-resolve": "^2.0.0",
101-
"node-stdlib-browser": "^1.2.0"
101+
"node-stdlib-browser": "^1.2.0",
102+
"pkg-dir": "^8.0.0",
103+
"resolve-esm": "^2.0.3"
102104
},
103105
"devDependencies": {
104106
"@playwright/test": "^1.40.1",

pnpm-lock.yaml

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ import browserResolve from 'browser-resolve'
44
import stdLibBrowser from 'node-stdlib-browser'
55
import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rollup/plugin'
66
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
7+
import { packageDirectorySync } from 'pkg-dir'
8+
import { importMetaResolve } from 'resolve-esm'
79
import type { Plugin } from 'vite'
8-
import { compareModuleNames, isEnabled, isNodeProtocolImport, resolvePolyfill, toEntries, toRegExp, withoutNodeProtocol } from './utils'
10+
import {
11+
compareModuleNames,
12+
isEnabled,
13+
isNodeProtocolImport,
14+
resolve,
15+
resolvePolyfill,
16+
toEntries,
17+
toRegExp,
18+
withoutNodeProtocol,
19+
} from './utils'
920

1021
export type BareModuleName<T = ModuleName> = T extends `node:${infer P}` ? P : never
1122
export type BareModuleNameWithSubpath<T = ModuleName> = T extends `node:${infer P}` ? `${P}/${string}` : never
@@ -266,6 +277,9 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
266277
// https://esbuild.github.io/plugins/#on-resolve
267278
build.onResolve({ filter: globalShimsFilter }, () => {
268279
const resolved = browserResolve.sync(globalShimPath)
280+
const newResolved = importMetaResolve(globalShimPath)
281+
282+
console.log(newResolved)
269283

270284
return {
271285
// https://github.com/evanw/esbuild/blob/edede3c49ad6adddc6ea5b3c78c6ea7507e03020/internal/bundler/bundler.go#L1468
@@ -306,7 +320,18 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
306320
return await resolvePolyfill(this, override)
307321
}
308322

309-
return browserResolve.sync(modulePath)
323+
const newResolved = new URL(importMetaResolve(modulePath))
324+
325+
console.log('importMetaResolve', newResolved)
326+
console.log('resolve', resolve(moduleName))
327+
328+
const resolved = browserResolve.sync(modulePath)
329+
const pkgDir = packageDirectorySync({ cwd: resolved })
330+
331+
console.log('browserResolve', resolved)
332+
console.log('pkgDir', pkgDir)
333+
334+
return resolved
310335
}
311336
}
312337
},

src/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { importMetaResolve } from 'resolve-esm'
12
import type { PluginContext } from 'rollup'
23
import type { BareModuleName, BooleanOrBuildTarget, ModuleName } from './index'
34

@@ -19,6 +20,20 @@ export const isNodeProtocolImport = (name: string) => {
1920
return name.startsWith('node:')
2021
}
2122

23+
export const resolve = (name: string) => {
24+
const consumerResolved = importMetaResolve(name, process.cwd())
25+
26+
if (consumerResolved) {
27+
return consumerResolved
28+
}
29+
30+
const providerResolved = importMetaResolve(name)
31+
32+
if (providerResolved) {
33+
return providerResolved
34+
}
35+
}
36+
2237
export const resolvePolyfill = async (context: PluginContext, name: string) => {
2338
const consumerResolved = await context.resolve(name)
2439

0 commit comments

Comments
 (0)