简体   繁体   English

如何浏览器化浏览器化的模块?

[英]How to browserify a browserified module?

The title seems confusing but I'll give an example. 标题似乎令人困惑,但我举一个例子。

Let's say I create a module that uses ES6 that runs in the browser, so I use browserify with babelify to build everything. 假设我创建了一个使用ES6的模块,该模块在浏览器中运行,因此我将browserify与babelify结合使用来构建所有内容。

Now I want to include that same module in a project that uses browserify, but does not uses Babel to compile ES6, so I need the compiled version. 现在,我想在使用browserify但不使用Babel编译ES6的项目中包含相同的模块,因此需要编译后的版本。

I tried to require the "browserified" module like this: 我试图要求像这样的“浏览器化”模块:

// es5-project.js
require('./compiled-module-with-browserify');

But when I run browserify es5-project.js I start to get some errors like this: 但是,当我运行browserify es5-project.js我开始出现如下错误:

Error: Cannot find module './XXX' from '/Users/mauricio.oliveira/projects/project-name/dist-folder'

And that makes sense, since browserify compiled all modules into one file, it won't find the modules inside the compiled file. 这很有意义,因为browserify将所有模块都编译到一个文件中,所以它不会在编译文件中找到这些模块。

Does anyone have faced a problem like this one? 有谁遇到过这样的问题? if you did, how you solved it? 如果您做了,如何解决?

Thanks! 谢谢!

Found the answer! 找到了答案!

This will do the trick https://github.com/substack/browserify-handbook#browser-field 这将达到目的https://github.com/substack/browserify-handbook#browser-field

Just define a "browser" index in the package.json file, and point to the initial source file. 只需在package.json文件中定义一个“浏览器”索引,并指向初始源文件即可。

:) :)

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

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