Commit b3fcad2

Anton Golub <antongolub@antongolub.com>
2024-05-27 22:23:43
feat: adapt mjs bundle for nodejs v12 (#828)
1 parent 73f0558
Changed files (3)
.github/workflows/test.yml
@@ -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:
scripts/build-js.mjs
@@ -77,7 +77,7 @@ if (bundle === 'src') {
 if (hybrid) {
   plugins.push(
     hybridExportPlugin({
-      loader: 'import',
+      loader: 'require',
       to: 'build',
       toExt: '.js',
     })
@@ -85,14 +85,6 @@ if (hybrid) {
 }
 
 plugins.push(
-  {
-    name: 'deno',
-    setup(build) {
-      build.onEnd(() =>
-        fs.copyFileSync('./scripts/deno.polyfill.js', './build/deno.js')
-      )
-    },
-  },
   transformHookPlugin({
     hooks: [
       {
@@ -100,7 +92,7 @@ plugins.push(
         if: !hybrid,
         pattern: /\.js$/,
         transform(contents) {
-          return injectCode(contents, `import './deno.js'`)
+          return injectCode(contents, `import { require } from './deno.js'`)
         },
       },
       {
@@ -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) {
scripts/deno.polyfill.js
@@ -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 }