Rescript-compiler: Platform JS scripts in ES6 format should use .mjs file extension

Created on 26 Oct 2020  路  2Comments  路  Source: rescript-lang/rescript-compiler

The bs-platform library files in ES6 format (lib/es6/* in the NPM package) are supplied using .js extensions. This is invalid under the rules for NodeJS ESM. For this to be valid ESM, the package.json for the library must have "module": "true". However adding this would break other module styles.

As an alternative, the files can be renamed to .mjs and then they can co-exist with other forms of JS. Unfortunately, while the compiler allows .mjs files to be specified as the suffix, which works very well, this does not apply to the supplied library files.

I have created a branch of a bindings project I maintain to show what happens when I attempt to use ES6 output with NodeJS ESM tooling:
https://github.com/TheSpyder/bs-fast-check/tree/es6

Replication instructions:

  • Install NodeJS >13 (or use v12 with --experimental-modules)
  • npm run build
  • npm run test

Error:

$ npm run test

> [email protected] test /Users/spyder/work/bs-fast-check
> mocha

(node:83360) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

/Users/spyder/work/bs-fast-check/node_modules/bs-platform/lib/es6/js_dict.js:3
import * as Caml_option from "./caml_option.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:180:29)
    at ModuleJob.run (internal/modules/esm/module_job.js:146:23)
    at async Loader.import (internal/modules/esm/loader.js:165:24)
    at async Object.exports.loadFilesAsync (/Users/spyder/work/bs-fast-check/node_modules/mocha/lib/esm-utils.js:33:20)
    at async singleRun (/Users/spyder/work/bs-fast-check/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/spyder/work/bs-fast-check/node_modules/mocha/lib/cli/run.js:362:5)

Expected:

Tests run successfully (as they do on the master branch using CJS output).

HIGH

Most helpful comment

yes, this is a known issue, we plan to change the suffix of shipped es6 format to .mjs in next major release 9 since it's a potentially breaking change

All 2 comments

yes, this is a known issue, we plan to change the suffix of shipped es6 format to .mjs in next major release 9 since it's a potentially breaking change

Ah very good!

it's a potentially breaking change

Is it though? The common wisdom is to do a full recompile whenever bs-platform changes which would redirect all of the compiled files to the new .mjs suffix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jordwalke picture jordwalke  路  4Comments

bobzhang picture bobzhang  路  4Comments

paparga picture paparga  路  5Comments

bobzhang picture bobzhang  路  5Comments

bobzhang picture bobzhang  路  3Comments