egoist / tsup

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

how to bundle a cli? #800

Open takegine opened 1 year ago

takegine commented 1 year ago

I'm trying to write cli for my project. But it doesn't work,I need help.


D:\github\packages\rigger\dist\bin.js:44
var import_ora2 = __toESM(require("ora"));
                          ^

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\github\node_modules\.pnpm\ora@6.1.2\node_modules\ora\index.js from D:\github\packages\rigger\dist\bin.js not supported.
Instead change the require of index.js in D:\github\packages\rigger\dist\bin.js to a dynamic import() which is available in all CommonJS modules. 
    at Object.<anonymous> (D:\github\packages\rigger\dist\bin.js:44:27) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.12.1
//tsup.config.json
{
    "entry": [
        "src/index.ts",
        "src/bin.ts"
    ],
    "minify": false,
    "splitting": false,
    "clean": true,
    "dts": true,
    "skipNodeModulesBundle": true
}
//tsconfig.json
{
    "compilerOptions": {
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "strict": true,
        "noEmit": false,
        "target": "ESNext",
        "moduleResolution": "Node",
        "allowSyntheticDefaultImports":true
    }
}
//package.json
{

  "main": "dist/index",
  "types": "dist/index",
  "files": [
    "dist/*.{js,d.ts}",
    "schema/*.json"
  ],
  "bin" : "dist/bin.js",
  "scripts": {
    "build": "tsup",
    "dev": "tsup --watch",
    "prepublish": "tsup"
  },
  "dependencies": {
    "chalk": "^2.4.1",
    "child_process": "^1.0.2",
    "chokidar": "^3.5.3",
    "commander": "^9.4.0",
    "fs-extra": "^10.1.0",
    "node-xlsx": "^0.21.0",
    "ora": "^6.1.2",
    "steam-game-path": "2.0.1"
  },
  "devDependencies": {
    "@types/fs-extra": "^9.0.13",
    "@types/node": "^17.0.45",
    "@types/node-xlsx": "^0.15.3"
  }
}
//dits/bin.ts
#!/usr/bin/env node

import { program } from 'commander';
import { version } from '../package.json';
import { tools } from '.';
import { spawn } from 'child_process';
import { existsSync, writeFile } from 'fs-extra';
import ora from 'ora';
import { join, relative } from 'path';

program
    .version(version, "-v, --version")
    .on('--help', () => {
        let text = ora('hello world')
        text.start()
    })
    .option("aaa", "aaa", tools )
    .parse(process.argv)

Upvote & Fund

Fund with Polar

cuppachino commented 1 year ago

try and use chalk before everything else and see if you get the same error. Idk what ora is but the message looks identical to the one you get with chalk. Does it work if you compile to ESM exclusively?

ImLunaHey commented 1 year ago

I would highly suggest moving ora to devDeps so tsup can inline it instead of using require.

0zcl commented 1 year ago

ora@6.1.2 is a pure esm package.

add type: "modules" in your package.json of project