Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 modules in extensions in Chrome version 61

This is not the same question as ES6 Modules In Google Chrome Extension Development (unexpected token) as that is both outdated and already answered.

Google produced a news release claiming Chrome supports ES6 modules. I am trying to load a module from within an extension. I am able to load a module from within a normal page, but not from within an extension.

Here is the html, this is a page in an extension context:

<script src="test.js" type="module"></script>

When I open the page, I see the following error message in the console:

Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

Does anyone have any advice? Is this a bug that should be reported to Chrome? Or is it just not yet supported? I could not locate any straightforward explanation.

like image 342
Josh Avatar asked Aug 17 '17 03:08

Josh


People also ask

How do I use ES6 modules in browser?

Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax. Here's what they look like. Simply add type="module" to your script tags and the browser will load them as ES Modules. The browser will follow all import paths, downloading and executing each module only once.

Are ES6 modules supported?

As ES6 refers to a huge specification and browsers have various levels of support, "Supported" means at least 95% of the spec is supported.

What are the modules in ES6 with example?

A module is nothing more than a chunk of JavaScript code written in a file. By default, variables and functions of a module are not available for use. Variables and functions within a module should be exported so that they can be accessed from within other files. Modules in ES6 work only in strict mode.

What is the ES6 module?

An ES6 module is a file containing JS code. There's no special module keyword; a module mostly reads just like a script. There are two differences. ES6 modules are automatically strict-mode code, even if you don't write "use strict"; in them. You can use import and export in modules.


1 Answers

As user wOxxOm mentioned in comments, see https://crbug.com/738739.

9-18-17 update: https://bugs.chromium.org/p/chromium/issues/detail?id=769012 looks like it is being fixed!

10-19-17 update: https://bugs.chromium.org/p/chromium/issues/detail?id=728377#c18 reported as working in chrome 64 (currently canary)

11-13-17 update: final update, testing in Chrome 63, modules are now working. Note that if you need to load a module in the background page of the extension, you cannot do it via scripts property in manifest.json, instead set page to background.html, and specify type module in script tag, and that will bypass the manifest issue.

like image 165
Josh Avatar answered Sep 29 '22 02:09

Josh