Skip to content

Commit 46faad9

Browse files
committed
Convert this library to ES Modules
1 parent 8d82be4 commit 46faad9

24 files changed

+255
-329
lines changed

.eslintrc

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
]
1717
}
1818
],
19-
"import/extensions": ["error", "never", {
20-
"json": "always",
21-
}],
19+
// Disabled due to https://github.com/import-js/eslint-plugin-import/issues/2104
20+
"import/extensions": [0, "never"],
2221
// This rule conflicts with our convention.
2322
"jest/valid-describe": "off",
2423
// This is not needed for this project.
@@ -32,20 +31,13 @@
3231
"@typescript-eslint/explicit-function-return-type": "off",
3332
"@typescript-eslint/no-explicit-any": "error",
3433
"@typescript-eslint/explicit-member-accessibility": "off",
35-
"import/no-unresolved": ["error", {
36-
ignore: ['estree'],
37-
}],
34+
// This is sad but we have to disable this rule to allow TS to generate
35+
// correct a ESM package.
36+
"import/no-unresolved": "off",
3837
// This rule is annoying and does not add any value.
3938
"@typescript-eslint/camelcase": "off",
4039
"@typescript-eslint/ban-ts-comment": ["error", {
4140
"ts-expect-error": "allow-with-description",
42-
}],
43-
},
44-
"settings": {
45-
"import/resolver": {
46-
"node": {
47-
"extensions": [".js", ".ts"]
48-
}
49-
}
41+
}]
5042
}
5143
}

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
module.exports = {
1+
export default {
22
preset: 'ts-jest',
3+
moduleNameMapper: {
4+
// Replace imports ending with `.js` with imports without extensions.
5+
'^(\\.{1,2}/.*)\\.js$': '$1',
6+
},
37
testEnvironment: 'node',
48
testPathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/node_modules/'],
59
};

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
"name": "addons-scanner-utils",
33
"version": "7.0.0",
44
"description": "Various addons related helpers to build CLIs.",
5-
"main": "dist/index.js",
6-
"types": "dist/index.d.ts",
5+
"type": "module",
6+
"main": "./dist/index.js",
7+
"types": "./dist/index.d.ts",
8+
"exports": {
9+
".": "./dist/index.js",
10+
"./io": "./dist/io/index.js",
11+
"./test-helpers": "./dist/test-helpers.js"
12+
},
713
"files": [
8-
"dist/**/*"
14+
"dist/**"
915
],
1016
"author": "Mozilla Add-ons Team",
1117
"license": "MPL-2.0",
@@ -30,7 +36,6 @@
3036
"@types/express": "4.17.13",
3137
"@types/jest": "^27.0.0",
3238
"@types/node": "^14.0.0",
33-
"@types/safe-compare": "^1.1.0",
3439
"@types/sinon": "^10.0.0",
3540
"@types/supertest": "^2.0.8",
3641
"@typescript-eslint/eslint-plugin": "^5.0.0",
@@ -50,12 +55,12 @@
5055
"supertest": "^6.0.0",
5156
"ts-jest": "^28.0.0",
5257
"type-coverage": "^2.3.0",
53-
"typescript": "^4.0.0"
58+
"typescript": "^4.7.2"
5459
},
5560
"scripts": {
5661
"eslint": "eslint --ext ts --ext js src/",
5762
"lint": "yarn eslint",
58-
"prepack": "rimraf dist/ && tsc --outDir dist/ && rimraf dist/**/*.spec.* dist/*.spec.*",
63+
"prepack": "rimraf dist/ && tsc && rimraf dist/**/*.spec.* dist/*.spec.*",
5964
"prettier": "prettier --write '**'",
6065
"prettier-ci": "prettier --list-different '**' || (echo '\n\nThis failure means you did not run `yarn prettier-dev` before committing\n\n' && exit 1)",
6166
"prettier-dev": "pretty-quick --branch master",

src/@types/safe-compare/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The `@types/safe-compare` package does not declare a module and that breaks
2+
// the type checker...
3+
declare module 'safe-compare' {
4+
function safeCompare(a: string, b: string): boolean;
5+
6+
export = safeCompare;
7+
}

src/const.spec.ts renamed to src/constants.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ECMA_VERSION } from './const';
1+
import { ECMA_VERSION } from './constants.js';
22

3-
describe(__filename, () => {
3+
describe('constants', () => {
44
it('exports an ECMA_VERSION constant', () => {
55
expect(ECMA_VERSION).toEqual(13);
66
});
File renamed without changes.

src/functions.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
RequestWithFiles,
99
createApiError,
1010
createExpressApp,
11-
} from './functions';
11+
} from './functions.js';
1212

13-
describe(__filename, () => {
13+
describe('functions', () => {
1414
describe('createApiError', () => {
1515
it('returns an API error with a message', () => {
1616
const message = 'oops, error';

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from './const';
2-
export * from './functions';
3-
export * from './io';
4-
export * from './stdio';
5-
export * from './errors';
1+
export * from './constants.js';
2+
export * from './functions.js';
3+
export * from './io/index.js';
4+
export * from './stdio.js';
5+
export * from './errors.js';

src/io/base.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { IOBase } from './base';
2-
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH } from './const';
3-
import { createFakeStderr } from '../test-helpers';
1+
import { IOBase } from './base.js';
2+
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH } from './constants.js';
3+
import { createFakeStderr } from '../test-helpers.js';
44

5-
describe(__filename, () => {
5+
describe('io/base', () => {
66
const createIOBase = ({
77
filePath = 'foo/bar/',
88
stderr = createFakeStderr(),

src/io/base.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { Readable } from 'stream';
22

33
import { oneLine } from 'common-tags';
44

5-
import { FLAGGED_FILE_MAGIC_NUMBERS_LENGTH, MAX_FILE_SIZE_MB } from './const';
6-
import { Stderr } from '../stdio';
5+
import {
6+
FLAGGED_FILE_MAGIC_NUMBERS_LENGTH,
7+
MAX_FILE_SIZE_MB,
8+
} from './constants.js';
9+
import { Stderr } from '../stdio.js';
710

811
type ScanFileFunction = (_path: string, isDirectory: boolean) => boolean;
912

0 commit comments

Comments
 (0)