Skip to content

Commit

Permalink
Merge pull request #63 from davidmyersdev/47-unexpected-string
Browse files Browse the repository at this point in the history
Improve global shims
  • Loading branch information
davidmyersdev authored Dec 22, 2023
2 parents f0aecbe + d01862f commit b02e6c9
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- uses: ./.github/actions/install-dependencies
- run: pnpm build:core
- run: pnpm build:shims
- run: pnpm build:shims:banner
- run: pnpm build:banner
- run: pnpm test
typecheck:
needs:
Expand All @@ -39,5 +39,5 @@ jobs:
- uses: ./.github/actions/install-dependencies
- run: pnpm build:core
- run: pnpm build:shims
- run: pnpm build:shims:banner
- run: pnpm build:banner
- run: pnpm typecheck
32 changes: 25 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@
},
"./shims/banner": {
"require": "./shims/banner/dist/index.cjs",
"import": "./shims/banner/dist/index.cjs"
"import": "./shims/banner/dist/index.js"
},
"./shims/buffer": {
"require": "./shims/buffer/dist/index.cjs",
"import": "./shims/buffer/dist/index.js"
},
"./shims/global": {
"require": "./shims/global/dist/index.cjs",
"import": "./shims/global/dist/index.js"
},
"./shims/process": {
"require": "./shims/process/dist/index.cjs",
"import": "./shims/process/dist/index.js"
}
},
"main": "./dist/index.cjs",
Expand All @@ -44,10 +56,13 @@
"shims"
],
"scripts": {
"build": "run-s build:core build:shims build:shims:banner build:types",
"build": "run-s build:core build:shims build:banner build:types",
"build:banner": "vite-node ./shims/banner/build.ts",
"build:core": "vite build",
"build:shims": "vite build ./shims",
"build:shims:banner": "vite-node ./shims/banner/build.ts",
"build:shims": "run-p build:shims:*",
"build:shims:buffer": "vite build -c ./shims/vite.config.ts ./shims/buffer",
"build:shims:global": "vite build -c ./shims/vite.config.ts ./shims/global",
"build:shims:process": "vite build -c ./shims/vite.config.ts ./shims/process",
"build:types": "run-s typecheck",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
Expand All @@ -70,16 +85,16 @@
},
"dependencies": {
"@rollup/plugin-inject": "^5.0.5",
"buffer-polyfill": "npm:buffer@^6.0.3",
"node-stdlib-browser": "^1.2.0",
"process": "^0.11.10"
"node-stdlib-browser": "^1.2.0"
},
"devDependencies": {
"@types/node": "^18.18.8",
"buffer": "6.0.3",
"esbuild": "^0.19.8",
"eslint": "^8.54.0",
"eslint-config-artisan": "^0.2.1",
"npm-run-all": "^4.1.5",
"process": "^0.11.10",
"rollup": "^4.6.0",
"typescript": "4.8.3",
"vite": "^5.0.2",
Expand All @@ -92,6 +107,9 @@
"access": "public"
},
"pnpm": {
"overrides": {
"buffer": "6.0.3"
},
"patchedDependencies": {
"buffer@6.0.3": "patches/buffer@6.0.3.patch"
}
Expand Down
28 changes: 10 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion shims/banner/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ await build({
bundle: true,
entryPoints: [join(thisDir, './index.cjs')],
format: 'iife',
inject: [join(thisDir, '..')],
inject: [
join(thisDir, '../buffer'),
join(thisDir, '../global'),
join(thisDir, '../process'),
],
outdir: join(thisDir, './dist'),
outExtension: { '.js': '.cjs' },
})
5 changes: 5 additions & 0 deletions shims/banner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "module",
"sideEffects": false,
"main": "./dist/index.cjs"
}
5 changes: 5 additions & 0 deletions shims/buffer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line unicorn/prefer-node-protocol
import { Buffer } from 'buffer'

export { Buffer }
export default Buffer
12 changes: 12 additions & 0 deletions shims/buffer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"sideEffects": false,
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js"
}
5 changes: 5 additions & 0 deletions shims/global/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const global = globalThis || this || self

export { global }
export default global
12 changes: 12 additions & 0 deletions shims/global/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"sideEffects": false,
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js"
}
12 changes: 12 additions & 0 deletions shims/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
},
"./buffer": {
"require": "./buffer/dist/index.cjs",
"import": "./buffer/dist/index.js"
},
"./global": {
"require": "./global/dist/index.cjs",
"import": "./global/dist/index.js"
},
"./process": {
"require": "./process/dist/index.cjs",
"import": "./process/dist/index.js"
}
},
"main": "./dist/index.cjs",
Expand Down
10 changes: 4 additions & 6 deletions shims/index.ts → shims/process/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-invalid-this
const global = globalThis || this || self

export { Buffer } from 'buffer-polyfill'
// We cannot use `process-polyfill` as the package name due to a bug in Yarn v1. The errors results in a dependency
// conflict with `node-stdlib-browser` which fails to import `process/browser.js`.
// https://github.com/yarnpkg/yarn/issues/6907
// eslint-disable-next-line unicorn/prefer-node-protocol
export { default as process } from 'process'
export { global }
import process from 'process'

export { process }
export default process
12 changes: 12 additions & 0 deletions shims/process/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "module",
"sideEffects": false,
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.js"
}
11 changes: 4 additions & 7 deletions shims/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { defineConfig } from 'vite'
import { externalizeDeps } from 'vite-plugin-externalize-deps'

// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
entry: './index.ts',
fileName: 'index',
entry: {
index: './index.ts',
},
},
minify: false,
rollupOptions: {
external: [/^node:.*$/],
output: [
Expand All @@ -19,15 +20,11 @@ export default defineConfig({
{
exports: 'named',
format: 'cjs',
inlineDynamicImports: true,
interop: 'auto',
},
],
},
sourcemap: true,
target: 'esnext',
},
plugins: [
externalizeDeps(),
],
})
Loading

0 comments on commit b02e6c9

Please sign in to comment.