简体   繁体   English

使用子文件夹发布es6和commonjs

[英]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 . 我可以用CommonJS的指定主文件"main": "./index.js" ,并与ES6 "module": "./index.es.js"package.json

But how does it works when I import my-package/myfile ? 但是,当我导入my-package/myfile时,它如何工作? Is myfile.js used or myfile.es.js 是使用myfile.js还是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 在您提到的博客文章中,没有太多魔术,他只是在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 如果您使用的是与emodule兼容的捆绑器(例如webpack或汇总),他们将读取module密钥

import someFunction from 'your-module';

Would import the es module unless you are using something like browserify which would take the commonjs version. 将导入es模块,除非您使用的浏览器版本类似commonjs版本。

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. 如果在模块后添加/ ,那么您将违反主路径或模块路径约定,并询问所需的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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