简体   繁体   English

是否可以在Chrome扩展程序中使用ES6?

[英]Is it possible to use ES6 in a Chrome Extension?

I've just started building a Chrome Extension and was curious if I'd be able to use ES6 with it. 我刚刚开始构建Chrome扩展程序,如果我能够使用ES6,我很好奇。

In the following compatibility table , Chrome 41 shows that it currently has 41% compatibility. 在以下兼容性表中 ,Chrome 41显示它目前具有41%的兼容性。 A couple key features like class are not included in that 41% and so I was curious if there were other options, such as transpiling. 41%这样的一些关键功能(如class不包括在内,所以我很好奇是否还有其他选项,例如转换。

I've already used Babel , an ES6 transpiler, with Ember CLI and it has worked great. 我已经在Ember CLI上使用了Babel ,一个ES6转换器,它运行得很好。

However, I find the build process a bit different when developing a chrome extension. 但是,在开发chrome扩展时,我发现构建过程有点不同。 For example, when testing an extension I'm developing, I load it into the browser via the "Load unpacked extension" option which points directly to the source code. 例如,在测试我正在开发的扩展时,我通过“加载解压扩展”选项将其加载到浏览器中,该选项直接指向源代码。

For the extension I am building, I am using the yeoman chrome extension generator as a base. 对于我正在构建的扩展,我使用yeoman chrome扩展生成器作为基础。 Ideally, I'd like to be able to set up some sort of grunt task that hooks into the debug task and then transpiles the code any time it changes to a separate directory. 理想情况下,我希望能够设置一些挂钩到debug任务的grunt任务,然后在它更改到单独的目录时转换代码。 From there, I could load the contents of that directory via the load unpacked extension option. 从那里,我可以通过load unpacked extension选项load unpacked extension该目录的内容。 However, I'm not exactly sure how to do this or if there are other options instead. 但是,我不确定如何做到这一点,或者是否还有其他选择。

Update This answer was originally written in 2015. The compatibility table link shows Chrome, FF, Edge, and Safari are more compatible than Babel now. 更新此答案最初写于2015年。兼容性表链接显示Chrome,FF,Edge和Safari现在比Babel 兼容。

Chrome 49+, FF 45+ may not benefit from Babel. Bab 49,Chrome 49 +,FF 45+可能无法受益。 Other browsers, may need transpiling. 其他浏览器,可能需要转换。

Original 原版的

I am also currently developing a Chrome Extension in ES6. 我目前还在开发ES6中的Chrome扩展程序。 Your questions seems to be more general so I try to answer this based on my experiences. 你的问题看起来更一般,所以我试着根据我的经验回答这个问题。

In the following compatibility table , Chrome 41 shows that it currently has 41% compatibility. 在以下兼容性表中 ,Chrome 41显示它目前具有41%的兼容性。 A couple key features like class are not included in that 41% and so I was curious if there were other options, such as transpiling. 41%这样的一些关键功能(如class不包括在内,所以我很好奇是否还有其他选项,例如转换。

This is true. 这是真的。 You can easily use all existing ES6 Features without worries and transpiling. 您可以轻松使用所有现有的ES6功能,无需担心和转换。 Every new Chrome release has some more features, which makes it quite exciting to wait ;) Chrome 44 now covers 48%, including class . 每个新的Chrome版本都有一些更多的功能,这使得等待非常令人兴奋;)Chrome 44现在占48%,包括class

However, if you want go full ES6 then a compiler is still the best option. 但是,如果你想要完整的ES6,那么编译器仍然是最好的选择。 You don't have to worry about supported features and simply write ES6 code, which will be compiled to proper ES5 code. 您不必担心支持的功能,只需编写ES6代码,这些代码将编译为正确的ES5代码。

My current setup is Browserify (via grunt-browserify ) using Babelify as compiler. 我目前的设置是使用Babelify作为编译器的Browserify (通过grunt-browserify browserify )。 Browserify enables you to also use ES6 Modules, which give you the full power of ES6. Browserify使您还可以使用ES6模块,它可以为您提供ES6的全部功能。

Gruntfile.js Gruntfile.js

browserify: {
    dist: {
        options: {
            transform: [
                ['babelify', { loose: 'all' }]
            ],
            browserifyOptions: { debug: true },
            exclude: ''
        },
        files: {
            'path/to/es5/app.js': ['path/to/es6/**/*.js']
        }
    }
}
// Then it will be uglified and copied to dist.

That means my Extension still runs with ES5 code though, but that doesn't matters much for me as I still can write in ES6. 这意味着我的扩展程序仍然使用ES5代码运行,但这对我来说并不重要,因为我仍然可以在ES6中编写。

If you really want to use available ES6 features (which I did before), it can be quite frustrating/annoying as you always have to look up whats possible in Chrome and mostly it's waiting for a new Chrome release. 如果你真的想要使用可用的ES6功能(我之前做过),那可能会非常令人沮丧/烦恼,因为你总是要在Chrome中查找可能的内容,而且大部分时间都在等待新的Chrome版本。

However, I find the build process a bit different when developing a chrome extension. 但是,在开发chrome扩展时,我发现构建过程有点不同。 For example, when testing an extension I'm developing, I load it into the browser via the "Load unpacked extension" option which points directly to the source code. 例如,在测试我正在开发的扩展时,我通过“加载解压扩展”选项将其加载到浏览器中,该选项直接指向源代码。

Well it's not really different to any other project I think. 嗯,这与我认为的任何其他项目并没有什么不同。 Depending on what Chrome specific features you are using, you could either just develop your project and later test it via loading it into the browser or just link the "Load unpacked extension" path to your generated dist/build/public folder. 根据您使用的Chrome特定功能,您可以开发项目,然后通过将其加载到浏览器中进行测试,或者只需将“加载解压扩展”路径链接到生成的dist/build/public文件夹。 Doing that, it's always the current state. 这样做,它始终是当前状态。 This works for me the best. 这对我来说是最好的。

Hope that helps a bit :) 希望那有所帮助 :)

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

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