@esbuild-kit/cjs-loader 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/README.md +49 -0
  2. package/dist/index.js +1 -0
  3. package/package.json +67 -0
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # cjs-loader
2
+
3
+ Node.js `require()` hook to instantaneously transform ESM & TypeScript to CommonJS on demand using [esbuild](https://esbuild.github.io/).
4
+
5
+ ### Features
6
+ - Transforms ESM & TypeScript to CommonJS on demand
7
+ - Supports TS extensions `.cjs` & `.mjs` (`.cts` & `.mts`)
8
+ - Cached for performance boost
9
+ - Supports Node.js v12.16.2+
10
+ - Handles `node:` import prefixes
11
+
12
+ > **Tip:**
13
+ >
14
+ > _cjs-loader_ doesn't hook into dynamic `import()` calls.
15
+ >
16
+ > Use this with [esm-loader](https://github.com/esbuild-kit/esm-loader) for `import()` support. Alternatively, use [tsx](https://github.com/esbuild-kit/tsx) to handle them both automatically.
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ npm install --save-dev @esbuild-kit/cjs-loader
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Pass `@esbuild/cjs-loader` into the [`--require`](https://nodejs.org/api/cli.html#-r---require-module) flag
27
+ ```sh
28
+ node -r @esbuild/cjs-loader ./file.js
29
+ ```
30
+
31
+ ### TypeScript configuration
32
+ The following properties are used from `tsconfig.json` in the working directory:
33
+ - `jsxFactory`
34
+ - `jsxFragmentFactory`
35
+
36
+ ### Cache
37
+ Modules transformations are cached in the system cache directory ([`TMPDIR`](https://en.wikipedia.org/wiki/TMPDIR)). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
38
+
39
+ Set environment variable `ESBK_DISABLE_CACHE` to a truthy value to disable the cache:
40
+
41
+ ```sh
42
+ ESBK_DISABLE_CACHE=1 node -r @esbuild/cjs-loader ./file.js
43
+ ```
44
+
45
+ ## Related
46
+
47
+ - [tsx](https://github.com/esbuild-kit/tsx) - Node.js runtime powered by esbuild using `@esbuild-kit/cjs-loader` and `@esbuild-kit/esb-loader`.
48
+
49
+ - [@esbuild-kit/esm-loader](https://github.com/esbuild-kit/esm-loader) - TypeScript to ESM transpiler using the Node.js loader API.
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var u=require("fs"),l=require("module"),r=require("@esbuild-kit/core-utils"),d=require("get-tsconfig");function c(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var p=c(u),n=c(l),m=c(d);const _=/\.[cm]?tsx?$/,a=m.default(),v=a==null?void 0:a.config,j=r.installSourceMapSupport();function f(e,t){process.send&&process.send({type:"dependency",path:t});const s=p.default.readFileSync(t,"utf8"),o=r.transformSync(s,t,{format:"cjs",tsconfigRaw:v});o.map&&j.set(t,o.map),e._compile(o.code,t)}const i=n.default._extensions;[".js",".ts",".tsx",".jsx"].forEach(e=>{i[e]=f}),[".cjs",".mjs",".cts",".mts"].forEach(e=>{Object.defineProperty(i,e,{value:f,enumerable:!1})});const y=n.default._resolveFilename;n.default._resolveFilename=function(...e){const[t,s]=e;return t.startsWith("node:")&&(e[0]=t.slice(5)),/\.[cm]js$/.test(t)&&s&&_.test(s.filename)&&(e[0]=`${t.slice(0,-2)}ts`),y.apply(this,e)};
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@esbuild-kit/cjs-loader",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Node.js loader for compiling ESM & TypeScript modules to CommonJS",
8
+ "keywords": [
9
+ "esbuild",
10
+ "loader",
11
+ "node",
12
+ "cjs",
13
+ "commonjs",
14
+ "esm",
15
+ "typescript"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": "esbuild-kit/cjs-loader",
19
+ "author": {
20
+ "name": "Hiroki Osame",
21
+ "email": "hiroki.osame@gmail.com"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "main": "./dist/index.js",
27
+ "exports": "./dist/index.js",
28
+ "scripts": {
29
+ "build": "pkgroll --target node12.20 --minify",
30
+ "lint": "eslint .",
31
+ "pretest": "npm run build",
32
+ "test": "node --require ./dist/index.js tests/index.ts"
33
+ },
34
+ "dependencies": {
35
+ "@esbuild-kit/core-utils": "^1.0.0",
36
+ "get-tsconfig": "^3.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "@pvtnbr/eslint-config": "^0.21.0",
40
+ "@types/node": "^17.0.31",
41
+ "@types/semver": "^7.3.9",
42
+ "eslint": "^8.15.0",
43
+ "execa": "^6.1.0",
44
+ "get-node": "^12.1.0",
45
+ "manten": "^0.0.3",
46
+ "pkgroll": "^1.2.2",
47
+ "semver": "^7.3.7",
48
+ "typescript": "^4.6.4"
49
+ },
50
+ "eslintConfig": {
51
+ "extends": "@pvtnbr",
52
+ "ignorePatterns": [
53
+ "tests/fixtures"
54
+ ],
55
+ "rules": {
56
+ "import/no-unresolved": "off",
57
+ "@typescript-eslint/no-shadow": [
58
+ "error",
59
+ {
60
+ "allow": [
61
+ "describe"
62
+ ]
63
+ }
64
+ ]
65
+ }
66
+ }
67
+ }