简体   繁体   English

如何从排除的模块(ES6)导入?

[英]How to import from excluded modules (ES6)?

I have a project for a browser app which I want to split in two parts. 我有一个浏览器应用程序项目,我想分为两个部分。 As a minimal example imagine that in ./core I have : 作为一个最小的例子,想象在./core中我有:

// core.js 
import SuperClass from "../core/SuperClass";    
export {SuperClass};

// SuperClass.js
class SuperClass
{
    constructor()
    {
        console.log('SuperClass constructor');
    }
}
export default SuperClass

and directory ./game has 和目录./game有

//game.js   
import SuperClass from "../core/SuperClass";
console.log(SuperClass);

If I build with game.js as entry point everything is fine as all dependencies go into the bundle. 如果我使用game.js作为切入点进行构建,那么一切都很好,因为所有依赖项都进入了捆绑包。

However, if I exclude the files from ./core (by using exclude function of browserify) and make a separate bundle with them and load before game.js with: 但是,如果我从./core文件中排除文件(通过使用browserify的排除功能),并与它们制作一个单独的捆绑包,并在game.js之前加载:

<script src="scripts/core.js"></script>
<script src="scripts/game.js"></script>

I get : Uncaught Error: Cannot find module '../core/SuperClass' from game.js 我得到:未捕获的错误:无法从game.js中找到模块'../core/SuperClass'

What is the way to provide those dependencies to game.js at runtime ? 在运行时将那些依赖项提供给game.js的方式是什么? My build process uses gulp , babelify and browserify. 我的构建过程使用gulp,babelify和browserify。

If you are adding the code via a <script> tag placed before your code, you can assume that is there, as a global variable/class/library, you don't need to import it because it is already present. 如果要通过放置在代码之前的<script>标记添加代码,则可以假定它在那里,作为全局变量/类/库,您不需要导入它,因为它已经存在。 It's like using the good old JQuery,for example: as you placed the file in your HTML file, you knew that $ was omnipresent 就像使用良好的旧JQuery一样,例如:将文件放在HTML文件中时,您知道$无处不在

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

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