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

Flag for ESM loader to override package.json "type" #1342

Closed
cspotcode opened this issue May 24, 2021 · 0 comments · Fixed by #1371
Closed

Flag for ESM loader to override package.json "type" #1342

cspotcode opened this issue May 24, 2021 · 0 comments · Fixed by #1371
Milestone

Comments

@cspotcode
Copy link
Collaborator

cspotcode commented May 24, 2021

Desired Behavior

Allow running e.g a webpack.config.ts as CommonJS in an otherwise ESM project.

ESM hook can be configured to override package.json "type" field for certain files to accomplish this.

Is this request related to a problem?

In a project:
using webpack.config.ts
with package.json "type": "module" and tsconfig "module": "esnext"
using ts-node's ESM loader for unit tests
does not want to pass NODE_OPTIONS=--loader ts-node/esm to webpack

In these projects, package.json is forcing all .ts files to execute as ESM. Attempting to require them will throw ERR_REQUIRE_ESM (#1232) This means webpack.config.ts always fails: webpack tries to require it, gets the error, tries to import it, and it fails because the emitted JS calls require in an ESM context.

We can enable our ESM loader to override package.json "type"

Possible solutions

Ways to do this:

  • set glob patterns to match; those files get overridden "type"
  • simple boolean; if true, "type" always matches compilerOptions; never package.json
//tsconfig.json
{
  "ts-node": {
    // modelled after "files", "include", "exclude" configs: can use globs, all relative to tsconfig.json
    // modelled after gitignore / gitattributes: later entries override earlier ones
    "moduleTypes": {
      "webpack.config.ts": "commonjs"
    }
  }

If webpack.config.ts loads other files, they may also need the override.
Would require: "moduleTypes": {"*": "commonjs"} in a special webpack-only tsconfig (or make a special test-only tsconfig without it)

Needs to play nice with eventual composite project support.

Alternatives considered

  • Projects can place their webpack.config.ts in a subdirectory, e.g. ./config/webpack.config.ts, and put a package.json in that subdirectory.

  • Add a commonjsEntrypoints config
    Any file matched by commonjsEntrypoints will be compiled and run as CJS, and anything it require()s will also compile and run as CJS.

  • Add an autoCommonjs option
    Automatically switches to commonjs emit and moduleType when ESM loader is not loaded

Additional context

jestjs/jest#11453

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant