egoist / tsup

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

Overcome path and fs dynamic import error #1130

Closed ridhwaans closed 1 month ago

ridhwaans commented 1 month ago

node v20.13.1

I am trying to run an express app after converting to ES modules, however I am running into a path error. Below is the package & TS configs. Also below is the node entrypoint & files.

I need some feedback if I am not properly bundling path & fs

package.json

"type": "module",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./dist/index.js"
    }
  },
"scripts": {
    "build": "tsup",
    "dev": "tsup --watch",
    "start": "node dist/index.js"
  },
  "devDependencies": {
    "express": "^4.19.2",
    "tsup": "^8.0.2"
  },

tsconfig.json

{
  "compilerOptions": {
    "tsBuildInfoFile": ".tsbuildinfo",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["**/*.ts", "**/*.mjs", "**/*.js", "**/*.cjs", ".eslintrc.cjs"],
  "exclude": ["dist", "node_modules"]
}

tsup.config.ts

import { defineConfig } from 'tsup';

export default defineConfig((opts) => ({
  entry: [
    './src/index.ts'
  ],
  format: ['esm'],
  splitting: true,
  sourcemap: true,
  minify: !opts.watch,
  clean: !opts.watch,
  dts: true,
  outDir: 'dist',
}));

Performing pnpm run build and pnpm run start returns

node dist/index.js

file:///home/vscode/Source/test/packages/core/dist/index.js:1
var px=Object.create;var t1=Object.defineProperty;var dx=Object.getOwnPropertyDescriptor;var lx=Object.getOwnPropertyNames;var cx=Object.getPrototypeOf,ux=Object.prototype.hasOwnProperty;var O=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(e,a)=>(typeof require<"u"?require:e)[a]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});var g=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),mx=(t,e)=>{for(var a in e)t1(t,a,{get:e[a],enumerable:!0})},fx=(t,e,a,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of lx(e))!ux.call(t,i)&&i!==a&&t1(t,i,{get:()=>e[i],enumerable:!(r=dx(e,i))||r.enumerable});return t};var La=(t,e,a)=>(a=t!=null?px(cx(t)):{},fx(e||!t||!t.__esModule?t1(a,"default",{value:t,enumerable:!0}):a,t));var wa=g((xL,I2)=>{"use strict";var hx=O("path").relative;I2.exports=xx;var vx=process.cwd();function D2(t,e){for(var a=t.split(/[ ,]+/),r=String(e).toLowerCase(),i=0;i<a.length;i++){var n=a[i];if(n&&(n==="*"||n.toLowerCase()===r))return!0}return!1}function yx(t,e,a){var r=Object.getOwnPropertyDescriptor(t,e),i=r.value;return r.get=function(){return i},r.writable&&(r.set=function(s){return i=s}),delete r.value,delete r.writable,Object.defineProperty(t,e,r),r}function gx(t){for(var e="",a=0;a<t;a++)e+=", arg"+a;return e.substr(2)}function bx(t){var e=this.name+": "+this.namespace;this.message&&(e+=" deprecated "+this.message);for(var a=0;a<t.length;a++)e+=`
                                                                                                                                                                                                                                                                                                                                                                                             ^

Error: Dynamic require of "path" is not supported
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:382
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:882
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:458
    at file:///home/vscode/Source/test/packages/core/dist/index.js:20:15607
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:458
    at file:///home/vscode/Source/test/packages/core/dist/index.js:51:609
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:458
    at file:///home/vscode/Source/test/packages/core/dist/index.js:51:1680
    at file:///home/vscode/Source/test/packages/core/dist/index.js:1:458
    at file:///home/vscode/Source/test/packages/core/dist/index.js:82:3623

index.ts

import express from "express";
import { daemon } from './lib/daemon';

const app = express();
const port = 3010;

app.get('/', (_req, res) => {
  res.send('Hello, World!');
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

./lib/daemon.ts

import { getStoreData, getItemData } from '../functions'
...

./functions/index.ts

import fs from 'fs';
import path from 'path';
...
// Some functions will use fs.statsync, path.dirname, path.basename  

Upvote & Fund

Fund with Polar

ridhwaans commented 1 month ago
export default defineConfig((opts) => ({
  format: ['cjs'], 
  ...
  esbuildOptions: (options) => {
    options.external = ['path', 'fs', 'os']; 
    options.platform = 'node'; 
  },
}));