Skip to content

Commit 78592d0

Browse files
committed
feat: migrate to publishing ES-modules with a CommonJS fallback. Improve emit-behavior. Fix bugs related to inclusion of module specifiers
1 parent d2084f3 commit 78592d0

File tree

89 files changed

+993
-631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+993
-631
lines changed

.github/workflows/workflow.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ jobs:
1010

1111
strategy:
1212
matrix:
13-
os: [windows-latest, macos-latest, ubuntu-18.04]
14-
node: [14, 16, 17]
13+
os: [windows-latest, macos-latest, ubuntu-latest]
14+
node: [14.19.0, 16, 17, 18]
1515

1616
steps:
1717
- name: Checkout code

README.md

+21-9
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ $ npx -p typescript -p cjstoesm cjstoesm
224224

225225
<!-- SHADOW_SECTION_INSTALL_END -->
226226

227+
## Engine
228+
229+
`cjstoesm` requires Node.js v14.19.0 or newer to function correctly.
230+
227231
<!-- SHADOW_SECTION_USAGE_START -->
228232

229233
## Usage
@@ -236,17 +240,29 @@ $ npx -p typescript -p cjstoesm cjstoesm
236240

237241
You can use this library as a CLI to convert your project files from using CommonJS to using ESM.
238242

239-
The following command transforms all files matched by the glob `**/*.*` and emits them to the folder `dist` from the current working directory:
243+
The following command transforms all files matched by the glob `**/*.*` and overwrites them in-place:
244+
245+
```
246+
cjstoesm **/*.*
247+
```
248+
249+
You can also just pass in a folder name, in which case all direct descendents of that folder will be transformed and overwritten:
240250

241251
```
242-
cjstoesm "**/*.*" dist
252+
cjstoesm some-folder
253+
```
254+
255+
You can also pass in a second argument, `outDir`, to avoid overwriting the source files. The following command transforms all files matched by the glob `**/*.*` and emits them to the folder `dist` from the current working directory:
256+
257+
```
258+
cjstoesm **/*.* dist
243259
```
244260

245261
Here's an overview of the options that can be passed via the CLI:
246262

247263
```
248264
$ cjstoesm --help
249-
Usage: cjstoesm transform [options] <input> <outDir>
265+
Usage: cjstoesm [options] <input> <outDir>
250266
251267
Transforms CJS to ESM modules based on the input glob
252268
@@ -260,9 +276,6 @@ Options:
260276
-h, --help display help for command
261277
```
262278

263-
For example, you can run `cjstoesm transform "**/*.*" dist` to transform all files matched by the glob `**/*.*` and emit them to the folder `dist` from the current working directory.
264-
You can also just run `cjstoesm "**/*.*" dist` which is an alias for the `transform` command.
265-
266279
The default behavior is to add file extensions to module specifiers to align with the implementation in [node.js](https://nodejs.org/dist/latest-v12.x/docs/api/esm.html#esm_mandatory_file_extensions) and across browsers.
267280
You can customize this with the `--preserve-module-specifiers` command line option. See the [API Options](#api-options) for documentation for the possible values you can pass for it.
268281

@@ -288,7 +301,6 @@ import {writeFileSync} from "fs";
288301

289302
const result = await transform({
290303
input: "src/**/*.*",
291-
outDir: "dist",
292304
write: false
293305
});
294306

@@ -307,9 +319,9 @@ interface TransformOptions {
307319
*/
308320
input: string[] | string;
309321
/**
310-
* The output directory to use
322+
* Optionally, the output directory to use. Defaults to inheriting that of the matched input files`
311323
*/
312-
outDir: string;
324+
outDir?: string;
313325
/**
314326
* If write is false, no files will be written to disk
315327
*/

bin/cjstoesm

-5
This file was deleted.

bin/cjstoesm.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
import "../dist/cli/index.js";
4+
process.title = 'cjstoesm';

package.json

+37-30
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
"generate:all": "pnpm run generate:sandhog && pnpm run generate:changelog",
99
"clean": "rimraf dist",
1010
"lint": "tsc --noEmit && eslint \"src/**/*.ts\" --color",
11-
"prettier": "prettier --write '{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}'",
11+
"prettier": "prettier --write \"{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}\"",
1212
"test": "ava",
1313
"prebuild": "pnpm run clean",
1414
"build": "pnpm run prebuild && pnpm run rollup",
1515
"build:built_in_module_map": "ts-node script/generate-built-in-module-map.ts",
1616
"prewatch": "pnpm run clean",
17-
"watch": "pnpm run prewatch && pnpm run rollup -- --watch",
17+
"watch": "pnpm run prewatch && pnpm run rollup:watch",
1818
"rollup": "rollup -c rollup.config.js",
19-
"preversion": "pnpm run lint && pnpm run build:built_in_module_map && pnpm run build",
19+
"rollup:watch": "rollup -c rollup.config.js --watch",
20+
"preversion": "pnpm run lint && pnpm run build:built_in_module_map && pnpm run build && dotcjs dist/cjs",
2021
"version": "pnpm run preversion && pnpm run generate:all && git add .",
2122
"release": "np --no-cleanup --no-yarn",
22-
"update:check": "pnpx npm-check-updates -x typescript-*,chalk,ava,@types/resolve --dep dev,prod",
23-
"update:commit": "pnpx npm-check-updates -u -x typescript-*,chalk,ava,@types/resolve --dep dev,prod && pnpm update && pnpm install"
23+
"update:check": "pnpx npm-check-updates -x typescript-*,ava,@types/resolve --dep dev,prod",
24+
"update:commit": "pnpx npm-check-updates -u -x typescript-*,ava,@types/resolve --dep dev,prod && pnpm update && pnpm install"
2425
},
2526
"keywords": [
2627
"typescript",
@@ -54,35 +55,37 @@
5455
"license": "MIT",
5556
"devDependencies": {
5657
"@rollup/plugin-node-resolve": "^13.3.0",
57-
"@types/node": "^17.0.35",
58-
"@types/prettier": "^2.6.1",
58+
"@types/node": "^17.0.36",
59+
"@types/prettier": "^2.6.3",
5960
"@types/reserved-words": "^0.1.0",
6061
"@types/resolve": "1.20.0",
6162
"@types/semver": "^7.3.9",
6263
"@typescript-eslint/eslint-plugin": "^5.26.0",
6364
"@typescript-eslint/parser": "^5.26.0",
6465
"@wessberg/ts-config": "2.0.2",
66+
"@wessberg/prettier-config": "1.0.0",
67+
"dotcjs": "1.0.0",
6568
"rollup-plugin-ts": "2.0.7",
6669
"ava": "3.15.0",
6770
"eslint": "^8.16.0",
6871
"eslint-config-prettier": "^8.5.0",
6972
"eslint-plugin-import": "^2.26.0",
70-
"eslint-plugin-jsdoc": "^39.3.1",
73+
"eslint-plugin-jsdoc": "^39.3.2",
7174
"husky": "^8.0.1",
72-
"memfs": "^3.4.3",
75+
"memfs": "^3.4.4",
7376
"np": "7.6.1",
7477
"npm-check-updates": "^13.0.3",
75-
"pnpm": "^7.1.5",
78+
"pnpm": "^7.1.7",
7679
"prettier": "^2.6.2",
7780
"pretty-quick": "^3.1.3",
7881
"rimraf": "^3.0.2",
79-
"rollup": "^2.74.1",
80-
"sandhog": "^1.0.43",
82+
"rollup": "^2.75.1",
83+
"sandhog": "^2.0.1",
8184
"semver": "7.3.7",
8285
"standard-changelog": "^2.0.27",
8386
"ts-node": "^10.8.0",
8487
"tslib": "^2.4.0",
85-
"typescript": "4.6.4",
88+
"typescript": "4.7.2",
8689
"typescript-3-2-1": "npm:[email protected]",
8790
"typescript-3-3-1": "npm:[email protected]",
8891
"typescript-3-4-1": "npm:[email protected]",
@@ -101,8 +104,8 @@
101104
},
102105
"dependencies": {
103106
"@wessberg/stringutil": "^1.0.19",
104-
"chalk": "^4.1.2",
105-
"commander": "^9.2.0",
107+
"chalk": "^5.0.1",
108+
"commander": "^9.3.0",
106109
"compatfactory": "^0.0.13",
107110
"crosspath": "^1.0.0",
108111
"fast-glob": "^3.2.11",
@@ -113,12 +116,14 @@
113116
"peerDependencies": {
114117
"typescript": ">=3.2.x || >= 4.x"
115118
},
116-
"main": "./dist/lib/cjs/index.js",
117-
"module": "./dist/lib/esm/index.js",
118-
"browser": "./dist/lib/esm/index.js",
119-
"types": "./dist/lib/esm/index.d.ts",
120-
"typings": "./dist/lib/esm/index.d.ts",
121-
"es2015": "./dist/lib/esm/index.js",
119+
"exports": {
120+
"import": "./dist/esm/index.js",
121+
"require": "./dist/cjs/index.cjs"
122+
},
123+
"type": "module",
124+
"types": "./dist/esm/index.d.ts",
125+
"main": "./dist/cjs/index.cjs",
126+
"module": "./dist/esm/index.js",
122127
"repository": {
123128
"type": "git",
124129
"url": "https://github.com/wessberg/cjstoesm.git"
@@ -127,23 +132,25 @@
127132
"url": "https://github.com/wessberg/cjstoesm/issues"
128133
},
129134
"engines": {
130-
"node": ">=10.0.0"
135+
"node": ">=14.9.0"
131136
},
137+
"prettier": "@wessberg/prettier-config",
132138
"ava": {
133139
"files": [
134140
"test/**.test.ts"
135141
],
136142
"verbose": true,
137-
"timeout": "400s",
138-
"extensions": [
139-
"ts"
143+
"nonSemVerExperiments": {
144+
"configurableModuleFormat": true
145+
},
146+
"extensions": {
147+
"ts": "module"
148+
},
149+
"nodeArguments": [
150+
"--loader=ts-node/esm"
140151
],
141152
"environmentVariables": {
142-
"NODE_OPTIONS": "--max_old_space_size=4096",
143153
"FORCE_COLOR": "3"
144-
},
145-
"require": [
146-
"ts-node/register/transpile-only"
147-
]
154+
}
148155
}
149156
}

0 commit comments

Comments
 (0)