egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.48k stars 209 forks source link

Shim syntax #1086

Open ScreamZ opened 4 months ago

ScreamZ commented 4 months ago

Using shim: true generate

// ../../node_modules/.pnpm/tsup@8.0.2_typescript@5.3.3/node_modules/tsup/assets/esm_shims.js
import { fileURLToPath } from "url";
import path from "path";
var getFilename = () => fileURLToPath(import.meta.url);
var getDirname = () => path.dirname(getFilename());
var __dirname = /* @__PURE__ */ getDirname();

But deno complains with syntax "path" and required "node:path" is there any way to change this ? If you want to use a built-in Node module, add a "node:" prefix (ex. "node:path").

Upvote & Fund

Fund with Polar

theoephraim commented 2 weeks ago

I ran into this issue as well. Here is a workaround using a simple esbuild plugin. I'll likely publish this as well as an npm module, but here it is for now. Note that it is not foolproof, as you could have an installed module with the same name as a node built-in, but in most cases it should work, and you could always adjust the regex if necessary.

import { defineConfig } from 'tsup';

const NODE_BUILTIN_MODULE_REGEX = /^(assert|buffer|child_process|cluster|crypto|dgram|dns|domain|events|fs|http|https|net|os|path|punycode|querystring|readline|stream|string_decoder|timers|tls|tty|url|util|v8|vm|zlib)$/;

// plugin to add missing `node:` prefix - which is required for deno
function addNodeImportPrefix() {
  return {
    name: 'addNodeImportPrefix',
    setup(build) {
      build.onResolve({ filter: NODE_BUILTIN_MODULE_REGEX }, (args) => ({
        path: `node:${args.path}`,
        external: true,
      }))
    },
  }
}

export default defineConfig({
  // ...
  esbuildPlugins: [addNodeImportPrefix()],
});