简体   繁体   English

使用ES6导出类(Babel)

[英]Exporting a class with ES6 (Babel)

I'm writing some frontend code with ECMAScript 6 (transpiled with BabelJS, and then browserified with Browserify) so that I can have a class in one file, export it and import it in another file. 我正在用ECMAScript 6编写一些前端代码(用BabelJS编译,然后用Browserify进行浏览),这样我就可以在一个文件中创建一个类,将其导出并导入到另一个文件中。

The way I'm doing this is: 我这样做的方式是:

export class Game {
    constructor(settings) {

    ...

    }
}

And then on the file that imports the class I do: 然后在导入我的类的文件上:

import {Game} from "../../lib/pentagine_browserified.js";
var myGame = new Game(settings);

I then compile it with grunt , this is my Gruntfile : 然后我用grunt编译它,这是我的Gruntfile

module.exports = function(grunt) {
  "use strict";

  grunt.loadNpmTasks('grunt-babel');
  grunt.loadNpmTasks('grunt-browserify');

  grunt.initConfig({
    "babel": {
      options: {

        sourceMap: false
      },
      dist: {
        files: {
          "lib/pentagine_babel.js": "lib/pentagine.js",
          "demos/helicopter_game/PlayState_babel.js": "demos/helicopter_game/PlayState.js"
        }
      }
    },

    "browserify": {
      dist: {
        files: {
          "lib/pentagine_browserified.js": "lib/pentagine_babel.js",
          "demos/helicopter_game/PlayState_browserified.js": "demos/helicopter_game/PlayState_babel.js"
        }
      }
    }
  });

  grunt.registerTask("default", ["babel", "browserify"]);
};

However, on the new Game( call, I get the following error: 但是,在new Game(通话,我得到以下错误:

Uncaught TypeError: undefined is not a function

As so, what I did was analyse the generated code by Babel and Browserify and I found this line on PlayState_browserified.js : 因此,我所做的是通过Babel和Browserify分析生成的代码,我在PlayState_browserified.js上找到了这一行:

var Game = require("../../lib/pentagine_browserified.js").Game;

I decided to print the require output: 我决定打印require输出:

console.log(require("../../lib/pentagine_browserified.js"));

And it is nothing but an empty object. 它只不过是一个空洞的物体。 I decided to check out the pentagine_browserified.js file: 我决定查看pentagine_browserified.js文件:

var Game = exports.Game = (function () {

It seems like it is correctly exporting the class, but for some other reason it is not being required on the other file. 看起来它正确地导出了类,但由于其他原因,在其他文件中不需要它。

Also, I'm sure the file is being required properly because changing the string "../../lib/pentagine_browserified.js" spits out a Not Found error, so it is going for the right file, that I'm sure about. 此外,我确定该文件是正确的,因为更改字符串"../../lib/pentagine_browserified.js"发出一个Not Found错误,所以它会Not Found正确的文件,我确定关于。

Browserify is meant to be fed a single "entry point" file, through which it recursively traverses all of your require statements, importing the code from other modules. Browserify旨在提供单个“入口点”文件,通过该文件递归遍历所有require语句,从其他模块导入代码。 So you should be require 'ing the _babel.js versions of modules, not _browserified.js ones. 所以你应该require _babel.js版本的模块,而不是_browserified.js

From the looks of it, you intend for your app's "entry point" to be demos/helicopter_game/PlayState_browserified.js , yeah? 从它的外观来看,你打算让你的应用程序的“入口点”为demos/helicopter_game/PlayState_browserified.js ,是吗? If that's the case: 如果是这样的话:

  • In PlayState.js, change it to import {Game} from "../../lib/pentagine_babel.js"; 在PlayState.js中,将其更改为import {Game} from "../../lib/pentagine_babel.js"; .
  • In Gruntfile.js, remove "lib/pentagine_browserified.js": "lib/pentagine_babel.js" . 在Gruntfile.js中,删除"lib/pentagine_browserified.js": "lib/pentagine_babel.js"

Works for me. 适合我。 Let me know if that suffices or I am misunderstanding your requirements here. 如果这样就足够了,或者我在这里误解了您的要求,请告诉我。

PS You can use babelify to avoid having separate Grunt tasks for Babel and Browserify. PS你可以使用babelify来避免为Babel和Browserify分别执行Grunt任务。 See my answer here for an example. 见我的答案在这里的一个例子。

I had a slightly different file configuration, that gave me some difficulty to get the "require" syntax to work in Node, but this post gave me the hint on how to used the babel-ified version of the file name. 我有一个稍微不同的文件配置,这让我在Node中使用“require”语法有些困难,但这篇文章给了我关于如何使用文件名的babel-ified版本的提示。

I am using WebStorm with the FileWatcher option set to Babel, and I have the FileWatcher configured to watch all files with suffix .jsx, and rename the compiled output file from {my_file}.jsx to {my_file}-compiled.js. 我使用WebStorm并将FileWatcher选项设置为Babel,并且我将FileWatcher配置为监视所有带后缀.jsx的文件,并将编译后的输出文件从{my_file} .jsx重命名为{my_file} -compiled.js。

So in my test case, I have 2 files: 所以在我的测试用例中,我有2个文件:

Person.jsx: Person.jsx:

class Person { ... }

export { Person as default}

and another file that wants to import it: 和另一个想要导入它的文件:

Test.jsx: Test.jsx:

var Person = require('./Person-compiled.js');

I couldn't get the "require" statement to find the module until I started the file path with './' and also add '-compiled.js' to properly specify the file name so that Node es5 could find the module. 在用'./'启动文件路径之前,我无法获取“require”语句来查找模块,并添加'-compiled.js'以正确指定文件名,以便Node es5可以找到模块。

I was also able to use the "import" syntax: 我还能够使用“import”语法:

import Person from './Person-compiled.js';

Since I have set up my WebStorm project as a Node ES5 project, I have to run 'Test-compiled.js' (not 'Test.jsx'). 由于我已将我的WebStorm项目设置为Node ES5项目,因此我必须运行'Test-compiled.js'(而不是'Test.jsx')。

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

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