Skip to content
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

[Bug]: Cannot use default import statement in modern.config.ts within esm package #6015

Closed
yf-yang opened this issue Jul 30, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@yf-yang
Copy link

yf-yang commented Jul 30, 2024

Version

System:
    OS: macOS 13.2.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 656.93 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Browsers:
    Chrome: 127.0.6533.72
    Safari: 16.3
  npmPackages:
    @modern-js/app-tools: ^2.56.2 => 2.56.2 
    @modern-js/runtime: ^2.56.2 => 2.56.2 
    @modern-js/tsconfig: ^2.56.2 => 2.56.2

Details

I've checked the source code.


It currently only supports cjs output.

As a result, code like

import turboConsole from 'unplugin-turbo-console/rspack';

is not correctly transformed and can't execute.

Reproduce link

https://github.com/yf-yang/modernjs-monorepo-example/tree/unplugin-bug

Reproduce Steps

cd apps/app
pnpm dev

If remove "type": "module" from app package's package.json, the bug goes away.

@yf-yang yf-yang added the bug Something isn't working label Jul 30, 2024
@yf-yang
Copy link
Author

yf-yang commented Jul 30, 2024

Seems my investigation is not completely right 🤔, it must be transformed to a cjs output to be required.

@yf-yang
Copy link
Author

yf-yang commented Jul 30, 2024

Try to bundle the file myself. There is a slight difference:
modern.js output:

var import_rspack = __toESM(require("unplugin-turbo-console/rspack"), 1);

my output:

var import_rspack = __toESM(require("unplugin-turbo-console/rspack"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));

That's the root cause, not sure if it is an esbuild issue or something else

@yf-yang
Copy link
Author

yf-yang commented Jul 30, 2024

Check evanw/esbuild#3852 (comment)
Close the issue.

@yf-yang yf-yang closed this as completed Jul 30, 2024
@yf-yang
Copy link
Author

yf-yang commented Jul 30, 2024

Workaround evanw/esbuild#2480 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant