From 31d778c2cde2b15843fbecc5c18392ddb4c2948e Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Tue, 11 Apr 2017 13:50:22 +0200 Subject: [PATCH] fix: Changes to the UMD built file and npm package publishing This patch introduces on top of #17 some minor changes from the review comments, in particular: - do not replace require("../filename") to include the api-metadata.json (restored the original '{/* include("...") */}' placeholder) - raise an appropriate error message when the source file is used by mistake (or the "dist/" file has not been built correctly). - set the generated UMD wrapped module as the package.json main entrypoint - do not include api-metadata.json and src dir from the files included in the published npm package - run both build and test npm scripts on prepublish --- Gruntfile.js | 2 +- README.md | 14 +++++++------- package.json | 6 ++---- src/browser-polyfill.js | 11 ++++++++--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index b5f2582..ba0b13b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -28,7 +28,7 @@ module.exports = function(grunt) { options: { patterns: [ { - match: /require\("..\/(.*?)"\)/, + match: /\{\/\* include\("(.*?)"\) \*\/\}/, replacement: (match, filename) => { return grunt.file.read(filename) .replace(/\n$/, "") diff --git a/README.md b/README.md index 503a22a..9cd59a8 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,18 @@ Chrome. ## Building -To build, assuming you're already installed [npm](https://www.npmjs.com/), -simply run: +To build, assuming you're already installed [node >= 6](https://nodejs.org) and +[npm](https://www.npmjs.com/), simply run: ```sh npm install +npm run build +npm run test ``` -This will build both non-minified and minified versions of the final library, -and output them to `dist/browser-polyfill.js` and `dist/browser-polyfill.min.js`, -respectively. - +This will install all the npm dependencies and build both non-minified and minified versions +of the final library, and output them to `dist/browser-polyfill.js` and `dist/browser-polyfill.min.js`, +respectively, and finally executes the unit tests on the generated dist files. ## Basic Setup @@ -74,7 +75,6 @@ browser.tabs.executeScript({file: "content.js"}).then(result => { }); ``` - ## Using the Promise-based APIs The Promise-based APIs in the `browser` namespace work, for the most part, diff --git a/package.json b/package.json index 521d9ee..357b010 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,8 @@ "name": "webextension-polyfill", "version": "0.1.0", "description": "A lightweight polyfill library for Promise-based WebExtension APIs in Chrome.", - "main": "src/browser-polyfill.js", + "main": "dist/browser-polyfill.js", "files": [ - "api-metadata.json", - "src", "dist" ], "repository": { @@ -46,6 +44,6 @@ "test": "mocha", "test-coverage": "COVERAGE=y nyc mocha", "publish-coverage": "grunt coveralls", - "prepublish": "npm run build" + "prepublish": "npm run build && npm run test" } } diff --git a/src/browser-polyfill.js b/src/browser-polyfill.js index be7f52e..ecf6773 100644 --- a/src/browser-polyfill.js +++ b/src/browser-polyfill.js @@ -13,9 +13,14 @@ if (typeof browser === "undefined") { // never actually need to be called, this allows the polyfill to be included // in Firefox nearly for free. const wrapAPIs = () => { - // Note that `require` does NOT work in general. See discussion here: - // https://github.com/mozilla/webextension-polyfill/pull/17#discussion_r99170958 - const apiMetadata = require("../api-metadata.json"); // eslint-disable-line no-undef + // NOTE: apiMetadata is associated to the content of the api-metadata.json file + // at build time by replacing the following "include" with the content of the + // JSON file. + const apiMetadata = {/* include("api-metadata.json") */}; + + if (Object.keys(apiMetadata).length === 0) { + throw new Error("api-metadata.json has not been included in browser-polyfill"); + } /** * A WeakMap subclass which creates and stores a value for any key which does