Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Expose a consistent API for both ESM and CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Jul 13, 2020
1 parent 3638638 commit b5ed36b
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 31 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -12,3 +12,4 @@ Dockerfile
# white-list files we want to process
!*.js
!*.md
!*.mjs
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -12,8 +12,8 @@ before_install:
- git checkout -
- npm install -g npm;
script:
- npm run test-ci
- npm run build
- npm run test-ci
- npm run lint
- npm run prettier-ci
- npm run typecheck
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -23,7 +23,7 @@ TODO: add a command line script ([issue #9](https://github.com/mozilla/sign-addo
Here is how to retrieve a signed version of an [XPI file](https://developer.mozilla.org/en-US/docs/Mozilla/XPI):

```javascript
var signAddon = require('sign-addon').default;
var { signAddon } = require('sign-addon');

signAddon({
// Required arguments:
Expand Down Expand Up @@ -88,7 +88,7 @@ signAddon({
In ES6 code, you can import it more concisely:

```javascript
import signAddon from 'sign-addon';
import { signAddon } from 'sign-addon';
```

## Dealing With Extension IDs
Expand Down
67 changes: 44 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -12,13 +12,13 @@
"build": "rimraf dist/ && webpack",
"changelog": "conventional-changelog -p angular -u",
"changelog-lint": "commitlint --from master",
"eslint": "eslint .",
"eslint": "eslint . --ext mjs --ext js",
"lint": "npm run eslint",
"prettier": "prettier --write '**'",
"prettier-ci": "prettier --list-different '**' || (echo '\n\nThis failure means you did not run `yarn prettier-dev` before committing\n\n' && exit 1)",
"prettier-ci": "prettier --list-different '**' || (echo '\n\nThis failure means you did not run `npm run prettier-dev` before committing\n\n' && exit 1)",
"prettier-dev": "pretty-quick --branch master",
"test": "jest",
"test-ci": "yarn test --coverage && codecov",
"test-ci": "npm run test --coverage && codecov",
"typecheck": "tsc"
},
"dependencies": {
Expand All @@ -30,8 +30,10 @@
"jsonwebtoken": "8.5.1",
"mz": "2.7.0",
"request": "2.88.2",
"shelljs": "0.8.4",
"source-map-support": "0.5.19",
"stream-to-promise": "3.0.0"
"stream-to-promise": "3.0.0",
"tmp": "0.2.1"
},
"devDependencies": {
"@babel/core": "7.10.4",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -141,4 +141,4 @@ export const signAddonAndExit = async (
}
};

export default signAddon;
export default { signAddon, signAddonAndExit };
11 changes: 11 additions & 0 deletions tests/fixtures/import-as-esm/test-import.mjs
@@ -0,0 +1,11 @@
import assert from 'assert';

// eslint-disable-next-line import/no-unresolved
import signAddon from 'sign-addon';

assert.deepEqual(
Object.keys(signAddon).sort(),
['signAddon', 'signAddonAndExit'].sort(),
);
assert.equal(typeof signAddon.signAddon, 'function');
assert.equal(typeof signAddon.signAddonAndExit, 'function');
11 changes: 11 additions & 0 deletions tests/fixtures/require-as-cjs/test-require.js
@@ -0,0 +1,11 @@
const assert = require('assert');

// eslint-disable-next-line import/no-unresolved
const signAddon = require('sign-addon');

assert.deepEqual(
Object.keys(signAddon).sort(),
['signAddon', 'signAddonAndExit'].sort(),
);
assert.equal(typeof signAddon.signAddon, 'function');
assert.equal(typeof signAddon.signAddonAndExit, 'function');
48 changes: 48 additions & 0 deletions tests/functional/imports.spec.js
@@ -0,0 +1,48 @@
import { execSync } from 'child_process';
import path from 'path';

import shell from 'shelljs';

import { withTempDir } from '../test-util';

describe(__filename, () => {
const node = shell.which('node');
const npm = shell.which('npm');
const fixturesDir = path.join(__dirname, '..', 'fixtures');
const fixtureEsmImport = path.join(fixturesDir, 'import-as-esm');
const fixtureCjsRequire = path.join(fixturesDir, 'require-as-cjs');

describe('imported as a library', () => {
beforeAll(() => {
execSync(`${npm} link`, {
cwd: path.resolve(path.join(__dirname, '..', '..')),
});
});

afterAll(() => {
execSync(`${npm} unlink`, {
cwd: path.resolve(path.join(__dirname, '..', '..')),
});
});

it('can be imported as an ESM module', async () => {
await withTempDir(async (tmpDir) => {
execSync(`${npm} link sign-addon`, { cwd: tmpDir.path() });
shell.cp('-rf', `${fixtureEsmImport}/*`, tmpDir.path());
execSync(`${node} --experimental-modules test-import.mjs`, {
cwd: tmpDir.path(),
});
});
});

it('can be imported as a CommonJS module', async () => {
await withTempDir(async (tmpDir) => {
execSync(`${npm} link sign-addon`, { cwd: tmpDir.path() });
shell.cp('-rf', `${fixtureCjsRequire}/*`, tmpDir.path());
execSync(`${node} --experimental-modules test-require.js`, {
cwd: tmpDir.path(),
});
});
});
});
});

0 comments on commit b5ed36b

Please sign in to comment.