rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

make default import less painful

Open bobzhang opened this issue 4 years ago • 3 comments

Note ES6's default import can not be faithfully translated into commonjs, so if you design an API, please don't use default exports/imports. This issue is to help reduce the main of dealing with js libraries using default.

Current state:

@module("express") external express: unit => express = "default"

In ES6, it is compiled into

import Express from "express";

In CommonJS, it is compiled into

var Express = require('express').default

This conforms to Babel's convention, however NodeJS and Babel disagree on such convention, both convention make sense based on their tradeoffs.

Here I am proposing treating another external style as ES6 import:

@module external express: unit => express = "express"

This in commonjs is compiled into

var Express = require("express")

In ES6, it is compiled into

import * as Express from "express";

Note from binding's point of view, import * as E rarely makes sense, sine in ES6, you can not export the whole module as an object, so I am proposing in ES6, it compiles into

import Express from 'express'

With my proposed change, we have two ways to express default import, one way is to match babel semantics, the other is to match Node semantics

bobzhang avatar May 24 '21 12:05 bobzhang

What is the status of this?

danwetherald avatar Jun 21 '21 21:06 danwetherald

This is preventing me from importing Mongoose. Repro:

import * as mongoose from "mongoose"
console.log(mongoose.connection) // <-- undefined

vs.

const mongoose = require("mongoose")
console.log(mongoose.connection) // <-- has value

The first example is generated by ReScript, causing everything to break. Any help appreciated.

tx46 avatar Jan 08 '23 17:01 tx46