standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

problem with mainFields #794

Open sibelius opened 5 years ago

sibelius commented 5 years ago

I'm trying to make a cli using @babel/register and esm to make work in development easier for us

this is the cli

babel-register-esm.js

const config = require('./babel.config');

require = require('esm')(module, {
  mainFields: [
    'module',
 ],
});

require('@babel/register')({
  ...config,
  extensions: [".es6", ".es", ".jsx", ".js", ".mjs", '.ts', '.tsx'],
});

require(process.argv[2]);

My use case is to use module package.json field in development and main field in production (compiled build)

When I run this in my monorepo I've got this error:

Error: Cannot find module '@entria/mypackage'. Please verify that the package.json has a valid "main" entry

this is the package.json of my @entria/mypackage'

"main": "dist/index.js",
"module": "src/index.ts",

is there anything that I'm missing?

jdalton commented 5 years ago

Try:

  mainFields: [
    'module',
    'main'
 ]

If you create a small repro repo I can dig in a bit more.

sibelius commented 5 years ago

here is the repo https://github.com/sibelius/babel-register-esm

I've added a readme with info of how to run

let me know if you need anything else

sibelius commented 5 years ago

I think we need to do something like this

https://github.com/davewasmer/main-dir/blob/master/index.js#L123

modify the Module._findPath

sibelius commented 4 years ago

for now I'm using @webpack to fix mainFields behavior

https://twitter.com/sseraphini/status/1255118394371837955