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: {
patterns: [
{
match: /require\("..\/(.*?)"\)/,
match: /\{\/\* include\("(.*?)"\) \*\/\}/,
replacement: (match, filename) => {
return grunt.file.read(filename)
.replace(/\n$/, "")

View File

@ -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,

View File

@ -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"
}
}

View File

@ -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