-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript [4.8.2] is adding invalid javascript for *.cjs files #50647
Comments
|
Yes! That fixed it. I changed to |
I am unblocked here. I do still think it's not ideal to be emitting invalid cjs but at least I have a workaround. :) |
Agreed. Also related to #48854. @weswigham thoughts? |
I ran into this issue with exactly the same scenario - ESM module with a single .cjs file. But in my case, my tsconfig didn't have any value set for "module" at all, and TS 4.7.4 was totally happy with whatever it was using by default. So it was really unexpected to see the invalid javascript emitted by TS 4.8.2 https://github.com/igordanchenko/sandbox/tree/typescript-50647/main |
Well, I spoke too soon. Setting "module" to "nodenext" did not resolve my issue. It did fix the .cjs file, but now all .ts files are emitted as CommonJS (event though the package.json "type" is set to "module"). I tried setting "moduleResolution" to "nodenext", but tsc compilation completely fails in this case.
Any advise will be appreciated. Here is a minimal repro - https://github.com/igordanchenko/sandbox/tree/typescript-50647/nodenext |
Have the same issue. Worked on In my use case I need to have a |
Got the same problem, anyone knows how to avoid TypeScript appending the empty export to .csj files? Does it still on version 4.9.3. It's just the last little issue allowing me to easily mix ESM and a single CommonJS file (auto-generated) in same project. I'll fix it with a manual copy or replace, but would be nice to avoid that. |
@sondreb I'm investigating issues with this now in version 4.9.3. Bumping from 4.7.x to 4.9.x indeed broke the compiled output by injecting |
If it expects cjs, you can't reference ESM code under it. CJS can only import ESM using dynamic import. You have to compile to CJS. You can't use |
Any updates on this? Experiencing the same issue where an empty |
probably means that you need to add a |
Ah thank you! You're right; turns out I needed to import
My ts build now works with |
@slimshreydy, nice catch! Importing the named export indeed solves @andrewbranch, do you have any thoughts on why importing the default
Here is a minimal repro - https://github.com/igordanchenko/sandbox/tree/typescript-50647/nodenext-1 |
Because its typings are wrong. clsx is a CJS library, but its typings say |
@weswigham honestly, I think we should consider making |
FYI, the
|
This should have been closed by #54567 |
Well, maybe not until |
I just ran into this problem with TS 5.3: Input: // eslint-disable-next-line import/no-extraneous-dependencies -- This isn't included in the bundle
import { Configuration } from "webpack";
const { resolve } = require("path");
const config: Configuration = {
entry: "./src/index.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules|\.d\.ts$/,
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
filename: "bundle.js",
path: resolve(__dirname, "dist"),
library: "bbcode_ast",
},
devtool: "source-map",
};
module.exports = config; Output: const { resolve } = require("path");
const config = {
entry: "./src/index.ts",
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules|\.d\.ts$/,
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
filename: "bundle.js",
path: resolve(__dirname, "dist"),
library: "bbcode_ast",
},
devtool: "source-map",
};
module.exports = config;
export {};
//# sourceMappingURL=webpack.config.cjs.map |
This was backed out of 5.5—hoping to reintroduce a fix in 5.6 via #58825. |
Bug Report
My project is set to emit ESNext modules. I have a variety of TS files, but I also have a single
.cjs
file, which needs to remain CommonJS to try and require legacy packages.My input looks like this:
I expect the output I get from TypeScript 4.7.4:
When I upgrade to TypeScript 4.8.2, it now emits an extra
export {}
at the end of the .cjs file:This causes Node to fail parsing the
.cjs
file becauseexport
does not exist in the .cjs context.🕗 Version & Regression Information
Worked in 4.7.4, broken in 4.8.2, also broken in current nightly (4.9.0-dev.20220905).
Repro here
https://github.com/dzearing/ts-repro-cjs
The text was updated successfully, but these errors were encountered: