isaacs / tshy

Other
894 stars 18 forks source link

Issue bundling src code with "zx" package / `{"type": "module"}` issue. #97

Closed reggi closed 1 month ago

reggi commented 1 month ago

Without {"type": "module"} and run tsc --noEmit I get this error:

src/deps.ts:1:21 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("zx")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Users/thomasreggi/Desktop/commands/packages/command-center/package.json'.

1 import {which} from 'zx'
                      ~~~~

Found 1 error in src/deps.ts:1

Adding {"type": "module"} all good ✅

When I run npm run prepare aka tshy I get the same error regardless of {"type": "module"} .

This is because durning the script, it's creating a ./src/package.json file with {"type": "commonjs"} 🥲

The only workaround is to use a .mts files.

The fix should be to check the root package.json and get the type from there.

reggi commented 1 month ago

I keep getting this too, i feel like I'm missing something 💀

error TS18003: No inputs were found in config file '/Users/thomasreggi/Desktop/commands/packages/command-center/.tshy/commonjs.json'. Specified 'include' paths were '["../src//*.ts","../src/*/.cts","../src//*.tsx","../src/*/.json"]' and 'exclude' paths were '["../src/*/.mts","../src/package.json"]'.

 command-center git:(main) ✗ npm run prepare

> @reggi/command-center@1.0.0 prepare
> tshy

error TS18003: No inputs were found in config file '/Users/thomasreggi/Desktop/commands/packages/command-center/.tshy/commonjs.json'. Specified 'include' paths were '["../src/**/*.ts","../src/**/*.cts","../src/**/*.tsx","../src/**/*.json"]' and 'exclude' paths were '["../src/**/*.mts","../src/package.json"]'.

Found 1 error.

writing tsconfig files...
using existing tsconfig.json
building /Users/thomasreggi/Desktop/commands/packages/command-center
tshy config {
  exports: { './package.json': './package.json', '.': './src/index.mts' }
}
exports {
  './package.json': './package.json',
  '.': {
    import: {
      types: './dist/esm/index.d.mts',
      default: './dist/esm/index.mjs'
    }
  }
}
set dialect { where: 'src', mode: 'esm' }
building esm
set dialect { where: '.tshy-build/esm', mode: 'esm' }
built esm
set dialect { where: 'src', mode: 'commonjs' }
building commonjs
build failed
reggi commented 1 month ago

When I just covert the 3 files that have zx in tree to .mts it works ✅ When I convert the whole project to .mts it fails ❌ the project needs at least one .ts file 😢

isaacs commented 1 month ago

If you don't want to build a hybrid module (ie both esm and commonjs) then you really don't need to use tshy, which is a typescript hybridizer

if you want tshy to only build one sort of dialect, you can put "tshy": { "dialects": ["esm"] } in package.json.

If you want to use a module that is only esm, from both esm and CJS, you'll need to use a dynamic import.

reggi commented 1 month ago

I want esm and commonjs

zx is not a esm only module though zx has exports require that point to cjs (but it also has type module)

https://github.com/google/zx/blob/main/package.json