@gjsify/esbuild-plugin-transform-ext 0.0.4 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -24
- package/esbuild.mjs +5 -36
- package/package.json +13 -8
- package/tsconfig.json +10 -5
- package/dist/cjs/index.cjs +0 -93
package/README.md
CHANGED
|
@@ -1,38 +1,46 @@
|
|
|
1
1
|
# @gjsify/esbuild-plugin-transform-ext
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
This can be useful if you want to bundle a module for Deno and Node.js.
|
|
3
|
+
esbuild plugin for transforming file extensions in import paths.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Installation
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
a();
|
|
15
|
-
b();
|
|
9
|
+
```bash
|
|
10
|
+
npm install @gjsify/esbuild-plugin-transform-ext
|
|
11
|
+
# or
|
|
12
|
+
yarn add @gjsify/esbuild-plugin-transform-ext
|
|
16
13
|
```
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
import {
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { build } from 'esbuild';
|
|
19
|
+
import { transformExtPlugin } from '@gjsify/esbuild-plugin-transform-ext';
|
|
22
20
|
|
|
23
|
-
await
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
outdir: "./dist/",
|
|
21
|
+
await build({
|
|
22
|
+
entryPoints: ['src/index.ts'],
|
|
23
|
+
outdir: 'dist/',
|
|
27
24
|
bundle: false,
|
|
28
|
-
format:
|
|
25
|
+
format: 'esm',
|
|
26
|
+
plugins: [transformExtPlugin({ outExtension: { '.ts': '.js' } })],
|
|
29
27
|
});
|
|
30
28
|
```
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
### Example
|
|
31
|
+
|
|
32
|
+
Input (`index.ts`):
|
|
33
|
+
```typescript
|
|
34
|
+
import { a } from './a.ts';
|
|
35
|
+
import { b } from './b.ts';
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Output (`index.js`):
|
|
39
|
+
```javascript
|
|
34
40
|
import { a } from './a.js';
|
|
35
41
|
import { b } from './b.js';
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT
|
package/esbuild.mjs
CHANGED
|
@@ -1,49 +1,18 @@
|
|
|
1
1
|
import { build } from 'esbuild';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
import { extname, dirname } from 'path';
|
|
4
2
|
import { EXTERNALS_NODE } from '@gjsify/resolve-npm';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
await readFile(
|
|
8
|
-
new URL('./package.json', import.meta.url), 'utf8'
|
|
9
|
-
)
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
if (!pkg.main && !pkg.module) {
|
|
13
|
-
throw new Error("package.json: The main or module property is required!");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const baseConfig = {
|
|
4
|
+
await build({
|
|
17
5
|
entryPoints: ['src/index.ts'],
|
|
18
6
|
bundle: true,
|
|
19
7
|
minify: false,
|
|
8
|
+
format: 'esm',
|
|
9
|
+
outfile: 'dist/esm/index.mjs',
|
|
20
10
|
external: [
|
|
21
11
|
...EXTERNALS_NODE,
|
|
22
12
|
'typescript',
|
|
23
13
|
'@deepkit/type-compiler',
|
|
24
14
|
'esbuild',
|
|
25
|
-
// '@gjsify/resolve-npm', can't be required in cjs builds
|
|
26
|
-
'@gjsify/esbuild-plugin-deno-loader',
|
|
27
15
|
'@gjsify/esbuild-plugin-deepkit',
|
|
28
16
|
'@gjsify/esbuild-plugin-gjsify',
|
|
29
|
-
]
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (pkg.main) {
|
|
33
|
-
build({
|
|
34
|
-
...baseConfig,
|
|
35
|
-
outdir: dirname(pkg.main),
|
|
36
|
-
format: 'cjs',
|
|
37
|
-
outExtension: {'.js': extname(pkg.main)},
|
|
38
|
-
platform: "node",
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (pkg.module) {
|
|
43
|
-
build({
|
|
44
|
-
...baseConfig,
|
|
45
|
-
outdir: dirname(pkg.module),
|
|
46
|
-
format: 'esm',
|
|
47
|
-
outExtension: {'.js': extname(pkg.module)},
|
|
48
|
-
});
|
|
49
|
-
}
|
|
17
|
+
],
|
|
18
|
+
});
|
package/package.json
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/esbuild-plugin-transform-ext",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Transform import file extensions plugin for esbuild",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "dist/
|
|
7
|
-
"module": "dist/esm/index.mjs",
|
|
6
|
+
"main": "dist/esm/index.mjs",
|
|
8
7
|
"types": "dist/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/types/index.d.ts",
|
|
11
|
+
"default": "./dist/esm/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
9
14
|
"scripts": {
|
|
10
|
-
"clear": "rm -rf dist tsconfig.tsbuildinfo",
|
|
11
|
-
"
|
|
12
|
-
"build": "yarn
|
|
15
|
+
"clear": "rm -rf dist tsconfig.tsbuildinfo || exit 0",
|
|
16
|
+
"check": "tsc --noEmit",
|
|
17
|
+
"build": "yarn build:js && yarn build:types",
|
|
13
18
|
"build:js": "node esbuild.mjs",
|
|
14
19
|
"build:types": "tsc --emitDeclarationOnly"
|
|
15
20
|
},
|
|
@@ -26,7 +31,7 @@
|
|
|
26
31
|
"plugin"
|
|
27
32
|
],
|
|
28
33
|
"devDependencies": {
|
|
29
|
-
"esbuild": "^0.
|
|
30
|
-
"typescript": "^
|
|
34
|
+
"esbuild": "^0.27.4",
|
|
35
|
+
"typescript": "^6.0.2"
|
|
31
36
|
}
|
|
32
37
|
}
|
package/tsconfig.json
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"module": "ESNext",
|
|
4
4
|
"target": "ESNext",
|
|
5
|
+
"rootDir": "src",
|
|
5
6
|
"outDir": "dist",
|
|
6
|
-
"types": [
|
|
7
|
+
"types": [
|
|
8
|
+
"node"
|
|
9
|
+
],
|
|
7
10
|
"declarationDir": "dist/types",
|
|
8
11
|
"declaration": true,
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
12
|
"moduleResolution": "bundler",
|
|
11
|
-
"allowImportingTsExtensions": true
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"strict": false
|
|
12
15
|
},
|
|
13
|
-
"files": [
|
|
14
|
-
|
|
16
|
+
"files": [
|
|
17
|
+
"src/index.ts"
|
|
18
|
+
]
|
|
19
|
+
}
|
package/dist/cjs/index.cjs
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/index.ts
|
|
20
|
-
var src_exports = {};
|
|
21
|
-
__export(src_exports, {
|
|
22
|
-
transformExtPlugin: () => transformExtPlugin,
|
|
23
|
-
transformImports: () => transformImports
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
// src/plugin.ts
|
|
28
|
-
var import_promises = require("fs/promises");
|
|
29
|
-
var IMPORT_PATTERN = /(import|from) ("|')\..*\.(js|ts|mjs|cjs|mts|cts)("|')/gm;
|
|
30
|
-
var TS_EXT_PATTERN = /\.(ts|mts|cts|tsx)/;
|
|
31
|
-
var JS_EXT_PATTERN = /\.(js|mjs|cjs|jsx)/;
|
|
32
|
-
var DEFAULT_EXTENSIONS = { ".js": ".js", ".ts": ".js", ".mts": ".js", ".cts": ".js", ".cjs": ".js", ".mjs": ".js" };
|
|
33
|
-
var transformImports = async (path, outExtension = {}, verbose = false) => {
|
|
34
|
-
let changed = false;
|
|
35
|
-
let contents = await (0, import_promises.readFile)(path, "utf8");
|
|
36
|
-
if (Object.keys(outExtension).length <= 0) {
|
|
37
|
-
outExtension = DEFAULT_EXTENSIONS;
|
|
38
|
-
}
|
|
39
|
-
const matches = Array.from(contents.matchAll(IMPORT_PATTERN));
|
|
40
|
-
for (const match of matches) {
|
|
41
|
-
const importStr = match[0];
|
|
42
|
-
let transformed = importStr;
|
|
43
|
-
for (const ext of Object.keys(outExtension)) {
|
|
44
|
-
transformed = transformed.replaceAll(ext, outExtension[ext]);
|
|
45
|
-
}
|
|
46
|
-
if (importStr === transformed) {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
changed = true;
|
|
50
|
-
contents = contents.replace(importStr, transformed);
|
|
51
|
-
if (verbose) {
|
|
52
|
-
console.debug(`[transform-ext] ${importStr} -> ${transformed}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (changed && verbose) {
|
|
56
|
-
console.debug(`[transform-ext] in ${path}
|
|
57
|
-
`);
|
|
58
|
-
}
|
|
59
|
-
return contents;
|
|
60
|
-
};
|
|
61
|
-
var transformExtPlugin = (pluginOptions) => {
|
|
62
|
-
const plugin = {
|
|
63
|
-
name: "transform-ext",
|
|
64
|
-
async setup(build) {
|
|
65
|
-
const outExtension = {};
|
|
66
|
-
if (pluginOptions.outExtension) {
|
|
67
|
-
Object.assign(outExtension, pluginOptions.outExtension);
|
|
68
|
-
} else if (build.initialOptions.outExtension) {
|
|
69
|
-
Object.assign(outExtension, build.initialOptions.outExtension);
|
|
70
|
-
} else {
|
|
71
|
-
Object.assign(outExtension, DEFAULT_EXTENSIONS);
|
|
72
|
-
}
|
|
73
|
-
const onLoad = async (loader, args) => {
|
|
74
|
-
let contents;
|
|
75
|
-
try {
|
|
76
|
-
contents = await transformImports(args.path, outExtension, pluginOptions.verbose);
|
|
77
|
-
} catch (error) {
|
|
78
|
-
console.error(error);
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
return { contents, loader };
|
|
82
|
-
};
|
|
83
|
-
build.onLoad({ filter: TS_EXT_PATTERN }, onLoad.bind(this, "ts"));
|
|
84
|
-
build.onLoad({ filter: JS_EXT_PATTERN }, onLoad.bind(this, "js"));
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
return plugin;
|
|
88
|
-
};
|
|
89
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
90
|
-
0 && (module.exports = {
|
|
91
|
-
transformExtPlugin,
|
|
92
|
-
transformImports
|
|
93
|
-
});
|