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

Expose a consistent API for both ESM and CJS #555

Merged
merged 3 commits into from Jul 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
65 changes: 64 additions & 1 deletion package-lock.json

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

10 changes: 7 additions & 3 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",
willdurand marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -44,6 +44,8 @@
"@types/jsonwebtoken": "8.5.0",
"@types/mz": "2.7.1",
"@types/request": "2.48.5",
"@types/shelljs": "0.8.8",
"@types/tmp": "0.2.0",
"babel-eslint": "10.1.0",
"babel-jest": "26.1.0",
"babel-loader": "8.1.0",
Expand All @@ -57,6 +59,8 @@
"prettier": "2.0.5",
"pretty-quick": "2.0.1",
"rimraf": "3.0.2",
"shelljs": "0.8.4",
"tmp": "0.2.1",
"typescript": "3.7.5",
"webpack": "4.44.0",
"webpack-cli": "3.3.12",
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');

// @ts-ignore
const signAddon = require('sign-addon'); // eslint-disable-line import/no-unresolved
willdurand marked this conversation as resolved.
Show resolved Hide resolved

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

import shell from 'shelljs';
import tmp from 'tmp';

describe(__filename, () => {
tmp.setGracefulCleanup();

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');

const makeTempDir = () =>
new Promise((resolve, reject) => {
tmp.dir(
{
prefix: 'tmp-sign-addon-',
// This allows us to remove a non-empty tmp dir.
unsafeCleanup: true,
},
(err, aPath, aCleanupCallback) => {
if (err) {
reject(err);
return;
}

resolve([aPath, aCleanupCallback]);
},
);
});

describe('imported as a library', () => {
beforeAll(() => {
execSync(`${npm} link`, {
cwd: path.resolve(path.join(__dirname, '..', '..')),
});
willdurand marked this conversation as resolved.
Show resolved Hide resolved
});

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

it('can be imported as an ESM module', async () => {
const [cwd, cleanupCallback] = await makeTempDir();

execSync(`${npm} link sign-addon`, { cwd });
shell.cp('-rf', `${fixtureEsmImport}/*`, cwd);
execSync(`${node} --experimental-modules test-import.mjs`, { cwd });

cleanupCallback();
willdurand marked this conversation as resolved.
Show resolved Hide resolved
});

it('can be imported as a CommonJS module', async () => {
const [cwd, cleanupCallback] = await makeTempDir();

execSync(`${npm} link sign-addon`, { cwd });
shell.cp('-rf', `${fixtureCjsRequire}/*`, cwd);
execSync(`${node} test-require.js`, { cwd });

cleanupCallback();
});
});
});
3 changes: 3 additions & 0 deletions webpack.config.js
Expand Up @@ -14,6 +14,9 @@ module.exports = {
path: path.join(__dirname, 'dist'),
filename: 'sign-addon.js',
libraryTarget: 'commonjs2',
// Force webpack bundled module to export the content of the default
// export.
libraryExport: 'default',
},
module: {
rules: [
Expand Down