Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update babel deps to babel 7 #214

Merged
merged 3 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = function(grunt) {
options: {
babelrc: false,
comments: false,
presets: ["babili"],
presets: ["minify"],
sourceMap: true,
},
files: {
Expand All @@ -72,11 +72,9 @@ module.exports = function(grunt) {
babelrc: false,
comments: true,
plugins: [
["transform-es2015-modules-umd", {
globals: {
"webextension-polyfill": "browser",
},
exactGlobals: true,
["./scripts/babel-transform-to-umd-module", {
globalName: "browser",
amdModuleName: "webextension-polyfill",
}],
],
sourceMap: true,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
},
"homepage": "https://github.com/mozilla/webextension-polyfill",
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"async-wait-until": "^1.1.5",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.3",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-babili": "^0.0.10",
"babel-preset-es2017": "^6.24.1",
"babel-preset-minify": "^0.5.1",
"browserify": "^16.2.2",
"chai": "^4.2.0",
"chromedriver": "^78.0.1",
Expand All @@ -32,7 +32,7 @@
"geckodriver": "^1.11.2",
"global-replaceify": "^1.0.0",
"grunt": "^1.0.1",
"grunt-babel": "^6.0.0",
"grunt-babel": "^8.0.0",
"grunt-contrib-concat": "^1.0.1",
"grunt-coveralls": "^2.0.0",
"grunt-replace": "^1.0.1",
Expand Down
61 changes: 61 additions & 0 deletions scripts/babel-transform-to-umd-module.js
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);
},
},
},
};
};
10 changes: 10 additions & 0 deletions test/fixtures/import-as-es6-extension/background.html
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>
3 changes: 3 additions & 0 deletions test/fixtures/import-as-es6-extension/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
browser.runtime.onMessage.addListener(async (msg, sender) => {
rpl marked this conversation as resolved.
Show resolved Hide resolved
return {bgReceived: msg};
});
5 changes: 5 additions & 0 deletions test/fixtures/import-as-es6-extension/content.js
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");
});
22 changes: 22 additions & 0 deletions test/fixtures/import-as-es6-extension/manifest.json
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"
}
}
5 changes: 5 additions & 0 deletions test/integration/test-extensions-in-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const {defineExtensionTests} = require("./setup");

defineExtensionTests({
description: "polyfill imported as an ES6 module",
extensions: ["import-as-es6-extension"],
});

defineExtensionTests({
description: "browser.runtime.onMessage/sendMessage",
extensions: ["runtime-messaging-extension"],
Expand Down
8 changes: 6 additions & 2 deletions test/mocha-babel.js
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",
},
}]],
});