Skip to content

new ESM implementation#26745

Merged
MylesBorins merged 1 commit intonodejs:masterfrom
MylesBorins:modules-lkgr
Mar 27, 2019
Merged

new ESM implementation#26745
MylesBorins merged 1 commit intonodejs:masterfrom
MylesBorins:modules-lkgr

Conversation

@MylesBorins
Copy link
Contributor

This PR updates the current --experimental-modules implementation based on the work of the modules team and reflects Phase 2 of our new modules plan.

A longer form description of these changes can be found in our draft blog post.

The largest differences from the current implementation include

  • packge.type which can be either module or commonjs
    • type: "commonjs":
      • .js is parsed as commonjs
      • default for entry point without an extension is commonjs
    • type: "module":
      • .js is parsed as esm
      • does not support loading JSON or Native Module by default
      • default for entry point without an extension is esm
  • --type=[mode] to let you set the type on entry point. Will override package.type for entry point.
  • A new file extension .cjs.
    • this is specifically to support importing commonjs in the module mode.
    • this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path.
  • --es-module-specifier-resolution=[type]
    • options are explicit (default) and node
    • by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one
    • by default our loader will not allow for importing directories that have an index file
    • developers can use --es-module-specifier-resolution=node to enable the commonjs specifier resolution algorithm
    • This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed
  • --experimental-json-loader
    • the only way to import json when "type": "module"
    • when enable all import 'thing.json' will go through the experimental loader independent of mode
    • based on JSON modules whatwg/html#4315
  • You can use package.main to set an entry point for a module
    • the file extensions used in main will be resolved based on the type of the module

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esm Issues and PRs related to the ECMAScript Modules implementation. experimental Issues and PRs related to experimental features. lib / src Issues and PRs related to general changes in the lib or src directory. module Issues and PRs related to the module subsystem. semver-minor PRs that contain new features and should be released in the next minor version. tsc-agenda Issues and PRs to discuss during the meetings of the TSC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.