Skip to content

Commit

Permalink
feat: adapt mjs bundle for nodejs v12 (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored May 27, 2024
1 parent 73f0558 commit b3fcad2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ jobs:
- name: cjs smoke test
run: npm run test:smoke:cjs
- name: mjs smoke test
if: matrix.node-version != '12'
run: npm run test:smoke:mjs

smoke-ts:
Expand Down
22 changes: 11 additions & 11 deletions scripts/build-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,22 @@ if (bundle === 'src') {
if (hybrid) {
plugins.push(
hybridExportPlugin({
loader: 'import',
loader: 'require',
to: 'build',
toExt: '.js',
})
)
}

plugins.push(
{
name: 'deno',
setup(build) {
build.onEnd(() =>
fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
)
},
},
transformHookPlugin({
hooks: [
{
on: 'end',
if: !hybrid,
pattern: /\.js$/,
transform(contents) {
return injectCode(contents, `import './deno.js'`)
return injectCode(contents, `import { require } from './deno.js'`)
},
},
{
Expand Down Expand Up @@ -145,7 +137,15 @@ plugins.push(
extractHelpersPlugin({
cwd: 'build',
include: /\.cjs/,
})
}),
{
name: 'deno',
setup(build) {
build.onEnd(() =>
fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
)
},
}
)

function entryPointsToRegexp(entryPoints) {
Expand Down
12 changes: 9 additions & 3 deletions scripts/deno.polyfill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const __filename = new URL(import.meta.url).pathname
const __dirname = new URL('.', import.meta.url).pathname

if (globalThis.Deno) {
globalThis.require = createRequire(import.meta.url)
globalThis.__filename = new URL(import.meta.url).pathname
globalThis.__dirname = new URL('.', import.meta.url).pathname
globalThis.require = require
globalThis.__filename = __filename
globalThis.__dirname = __dirname
}

export { require, __dirname, __filename }

0 comments on commit b3fcad2

Please sign in to comment.