Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update babel deps to babel 7 (#214)
* fix: Update babel deps to babel 7 * chore: Replaced @babel/transfor-umd-modules with a small custom babel transformer script * test: Added minimal integration test as smoke test for importing polyfill as es6 module
- Loading branch information
Showing
9 changed files
with
121 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const {template, types} = require("@babel/core"); | ||
|
||
const buildWrapper = template(` | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(AMD_MODULE_NAME, ["module"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(module); | ||
} else { | ||
var mod = { exports: {} }; | ||
factory(mod); | ||
global.GLOBAL = mod.exports; | ||
} | ||
})( | ||
typeof globalThis !== "undefined" ? globalThis | ||
: typeof self !== "undefined" ? self | ||
: this, | ||
function(module) { | ||
} | ||
) | ||
`); | ||
|
||
module.exports = (api, options = {}) => { | ||
api.assertVersion(7); | ||
|
||
if (typeof options.globalName != "string") { | ||
throw new Error("globalName option is mandatory"); | ||
} | ||
|
||
if (typeof options.amdModuleName != "string") { | ||
throw new Error("amdModuleName is mandatory"); | ||
} | ||
|
||
return { | ||
name: "transform-to-umd-module", | ||
|
||
visitor: { | ||
Program: { | ||
exit(path) { | ||
const {body, directives} = path.node; | ||
path.node.directives = []; | ||
path.node.body = []; | ||
|
||
const umdWrapper = path.pushContainer( | ||
"body", | ||
buildWrapper({ | ||
AMD_MODULE_NAME: types.stringLiteral(options.amdModuleName), | ||
GLOBAL: options.globalName, | ||
}) | ||
)[0]; | ||
const umdFactory = umdWrapper | ||
.get("expression.arguments")[1] | ||
.get("body"); | ||
|
||
umdFactory.pushContainer("directives", directives); | ||
umdFactory.pushContainer("body", body); | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang='en'> | ||
<head> | ||
<meta charset='utf-8'> | ||
</head> | ||
<body> | ||
<script type='module' src='browser-polyfill.js'></script> | ||
<script type='module' src='background.js'></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
browser.runtime.onMessage.addListener(async (msg, sender) => { | ||
return {bgReceived: msg}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
test("Test browser.runtime.onMessage on polyfill loaded as es6 module", async (t) => { | ||
const msg = "send-message-to-background-page"; | ||
const res = await browser.runtime.sendMessage(msg); | ||
t.deepEqual(res, {bgReceived: msg}, "Got the expected reply"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "test-import-as-es6-module", | ||
"version": "0.1", | ||
"description": "test-import-as-es6-module", | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"http://localhost/*" | ||
], | ||
"js": [ | ||
"browser-polyfill.js", | ||
"tape.js", | ||
"content.js" | ||
] | ||
} | ||
], | ||
"permissions": [], | ||
"background": { | ||
"page": "background.html" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
require("babel-core/register")({ | ||
presets: ["es2017"], | ||
require("@babel/register")({ | ||
presets: [["@babel/env", { | ||
targets: { | ||
node: "current", | ||
}, | ||
}]], | ||
}); |