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
This commit is contained in:
Luca Greco 2017-04-11 13:50:22 +02:00
parent f9248e62e7
commit 31d778c2cd
4 changed files with 18 additions and 15 deletions

View File

@ -28,7 +28,7 @@ module.exports = function(grunt) {
options: { options: {
patterns: [ patterns: [
{ {
match: /require\("..\/(.*?)"\)/, match: /\{\/\* include\("(.*?)"\) \*\/\}/,
replacement: (match, filename) => { replacement: (match, filename) => {
return grunt.file.read(filename) return grunt.file.read(filename)
.replace(/\n$/, "") .replace(/\n$/, "")

View File

@ -9,17 +9,18 @@ Chrome.
## Building ## Building
To build, assuming you're already installed [npm](https://www.npmjs.com/), To build, assuming you're already installed [node >= 6](https://nodejs.org) and
simply run: [npm](https://www.npmjs.com/), simply run:
```sh ```sh
npm install npm install
npm run build
npm run test
``` ```
This will build both non-minified and minified versions of the final library, This will install all the npm dependencies and build both non-minified and minified versions
and output them to `dist/browser-polyfill.js` and `dist/browser-polyfill.min.js`, of the final library, and output them to `dist/browser-polyfill.js` and `dist/browser-polyfill.min.js`,
respectively. respectively, and finally executes the unit tests on the generated dist files.
## Basic Setup ## Basic Setup
@ -74,7 +75,6 @@ browser.tabs.executeScript({file: "content.js"}).then(result => {
}); });
``` ```
## Using the Promise-based APIs ## Using the Promise-based APIs
The Promise-based APIs in the `browser` namespace work, for the most part, The Promise-based APIs in the `browser` namespace work, for the most part,

View File

@ -2,10 +2,8 @@
"name": "webextension-polyfill", "name": "webextension-polyfill",
"version": "0.1.0", "version": "0.1.0",
"description": "A lightweight polyfill library for Promise-based WebExtension APIs in Chrome.", "description": "A lightweight polyfill library for Promise-based WebExtension APIs in Chrome.",
"main": "src/browser-polyfill.js", "main": "dist/browser-polyfill.js",
"files": [ "files": [
"api-metadata.json",
"src",
"dist" "dist"
], ],
"repository": { "repository": {
@ -46,6 +44,6 @@
"test": "mocha", "test": "mocha",
"test-coverage": "COVERAGE=y nyc mocha", "test-coverage": "COVERAGE=y nyc mocha",
"publish-coverage": "grunt coveralls", "publish-coverage": "grunt coveralls",
"prepublish": "npm run build" "prepublish": "npm run build && npm run test"
} }
} }

View File

@ -13,9 +13,14 @@ if (typeof browser === "undefined") {
// never actually need to be called, this allows the polyfill to be included // never actually need to be called, this allows the polyfill to be included
// in Firefox nearly for free. // in Firefox nearly for free.
const wrapAPIs = () => { const wrapAPIs = () => {
// Note that `require` does NOT work in general. See discussion here: // NOTE: apiMetadata is associated to the content of the api-metadata.json file
// https://github.com/mozilla/webextension-polyfill/pull/17#discussion_r99170958 // at build time by replacing the following "include" with the content of the
const apiMetadata = require("../api-metadata.json"); // eslint-disable-line no-undef // 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 * A WeakMap subclass which creates and stores a value for any key which does