I have struggled with this for some time. I have this now in lib-a:
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES5",
"lib": ["es2020"],
"outDir": "host",
"rootDir": ".",
"sourceMap": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strictNullChecks": true,
"strict": true,
"esModuleInterop": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"noErrorTruncation": true,
"types": ["node"],
"typeRoots": ["node_modules/@types"],
"noImplicitAny": true,
"declaration": true
},
"include": ["*.ts", "**/*.ts", "*.d.ts"],
"exclude": ["node_modules", "host"]
}
I have this in lib-b, which does import thedefault from 'lib-a':
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES5",
"lib": ["es2020", "dom"],
"outDir": "host",
"rootDir": ".",
"sourceMap": true,
"moduleResolution": "node",
"strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"esModuleInterop": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"allowJs": true,
"noErrorTruncation": true,
"types": ["node"],
"typeRoots": ["node_modules/@types"],
"noImplicitAny": false,
"paths": {
"~/*": ["./*"],
"~": ["./index.ts"]
}
},
"include": ["*.ts", "**/*.ts", "*.d.ts"],
"exclude": ["node_modules", "./host/**/*"]
}
I am seeing this in lib-b on thedefault:
console.log(thedefault)
// { default: [Function: myDefaultFunction] }
Here is what I would like:
- Compile the
.js,.d.ts, and.js.mapoutput for each.tsfile in each project, so it works on ES5 systems (you can just load the file and it works, don’t get errors likeimport is not a functionor whatever, because theimportstatements were left in the build/*.js files). - The output *.js files don’t have
import,export, etc., they are compiled down to systems that don’t include ES6 features. - Don’t want to use the
type: 'module'option in package.json, or to specify.mjsfiles. I want to have plain.tsfiles and do it without that type option. - I would like to use all ESNext features in my TS code.
How do I do this?
I have tried:
{
"compilerOptions": {
"module": "ESNext",
"target": "ES5",
"lib": ["es2020", "dom"],
"outDir": "host",
"rootDir": ".",
"sourceMap": true,
"moduleResolution": "node",
"strictNullChecks": true,
"strict": true,
"esModuleInterop": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"allowJs": true,
"noErrorTruncation": true,
"types": ["node"],
"typeRoots": ["node_modules/@types"],
"noImplicitAny": false,
"paths": {
"~/*": ["./*"],
"~": ["./index.ts"]
}
},
"include": ["*.ts", "**/*.ts", "*.d.ts"],
"exclude": ["node_modules", "./host/**/*"]
}
That is:
"module": "ESNext",
"target": "ES5",
That would make sense to me, but anything other than module: CommonJS results in including the import and export ES6 features in the build files.
Do I need webpack for this or something?
It should be straightforward, some combination of TypeScript config options, but I can’t figure it out.
If I do do:
"module": "ESNext",
"target": "ES5",
Then it works at least in node:
console.log(thedefault)
// [Function: myDefaultFunction]
But, then I need webpack to get it to work in the browser. And in Next.js I need to add this to the webpack build via the transpilePackages option, which is annoying. Any way to avoid that and I can just import my compiled module anywhere and it “just works”?
