In Chrome 61, support for modules in JavaScript was added. Right now I am running Chrome 63.
I am trying to use import
/export
syntax in Chrome extension content script to use modules.
In manifest.json
:
content_scripts: [
{
js: [
content.js
],
}
]
In my-script.js
(same directory as content.js
):
'use strict';
const injectFunction = () => window.alert('hello world');
export default injectFunction;
In content.js
:
'use strict';
import injectFunction from './my-script.js';
injectFunction();
I receive this error: Uncaught SyntaxError: Unexpected identifier
If I change the import syntax to import {injectFunction} from './my-script.js';
I get this error: Uncaught SyntaxError: Unexpected token {
Is there some issue with using this syntax in content.js
in Chrome extension (since in HTML you have to use <script type=module src=script.js>
syntax), or am I doing something wrong? It seems strange that Google would ignore support for extensions.