简体   繁体   中英

Publishing both es6 and commonjs with subfolders

I can specify the main file for commonjs with "main": "./index.js" and for es6 with "module": "./index.es.js" in package.json .

But how does it works when I import my-package/myfile ? Is myfile.js used or myfile.es.js

And why it isn't possible to specify a subfolder instead of a single main file ?

There's not a lot of magic going on in that blog post you mentioned, he's just asking for node_modules/his-module/P.js

The bundlers need a single entry because (in theory) that is where you have your

module.exports = {}

code which is what lets the bundlers access your functions.

If you are using an esmodule compatible bundler like webpack or rollup, they will read the module key

import someFunction from 'your-module';

Would import the es module unless you are using something like browserify which would take the commonjs version.

You can also ask explicitly for a different file

import someFunction from 'your-module/lib/index.min.js';

Whatever file you ask for here, it'll import that. If you add a / after your-module you are now breaking out of the main or module path conventions and asking for whatever file you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM