@gjsify/esbuild-plugin-transform-ext 0.0.2
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 +38 -0
- package/dist/cjs/index.cjs +93 -0
- package/dist/esm/index.mjs +66 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/plugin.d.ts +12 -0
- package/dist/types/types/extension.d.ts +3 -0
- package/dist/types/types/index.d.ts +2 -0
- package/dist/types/types/plugin-options.d.ts +5 -0
- package/esbuild.mjs +49 -0
- package/example/dist/index.js +4 -0
- package/example/esbuild.mjs +12 -0
- package/example/src/a.ts +3 -0
- package/example/src/b.ts +3 -0
- package/example/src/index.ts +4 -0
- package/package.json +28 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +91 -0
- package/src/types/extension.ts +1 -0
- package/src/types/index.ts +2 -0
- package/src/types/plugin-options.ts +6 -0
- package/tsconfig.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @gjsify/esbuild-plugin-transform-ext
|
|
2
|
+
|
|
3
|
+
Transform import file extensions plugin for `esbuild`.
|
|
4
|
+
This can be useful if you want to bundle a module for Deno and Node.js.
|
|
5
|
+
|
|
6
|
+
## Example
|
|
7
|
+
|
|
8
|
+
This example transforms the typescript entrypoint with `.ts` imports into a javascript file with `.js` imports.
|
|
9
|
+
|
|
10
|
+
index:ts:
|
|
11
|
+
```ts
|
|
12
|
+
import { a } from './a.ts';
|
|
13
|
+
import { b } from './b.ts';
|
|
14
|
+
a();
|
|
15
|
+
b();
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
esbuild.mjs:
|
|
19
|
+
```js
|
|
20
|
+
import esbuild from 'esbuild';
|
|
21
|
+
import { transformExtPlugin } from "@gjsify/esbuild-plugin-transform-ext";
|
|
22
|
+
|
|
23
|
+
await esbuild.build({
|
|
24
|
+
plugins: [transformExtPlugin({ outExtension: {'.ts': '.js'}})],
|
|
25
|
+
entryPoints: ["./src/index.ts"],
|
|
26
|
+
outdir: "./dist/",
|
|
27
|
+
bundle: false,
|
|
28
|
+
format: "esm",
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
output index:js:
|
|
33
|
+
```ts
|
|
34
|
+
import { a } from './a.js';
|
|
35
|
+
import { b } from './b.js';
|
|
36
|
+
a();
|
|
37
|
+
b();
|
|
38
|
+
```
|
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import { readFile } from "fs/promises";
|
|
3
|
+
var IMPORT_PATTERN = /(import|from) ("|')\..*\.(js|ts|mjs|cjs|mts|cts)("|')/gm;
|
|
4
|
+
var TS_EXT_PATTERN = /\.(ts|mts|cts|tsx)/;
|
|
5
|
+
var JS_EXT_PATTERN = /\.(js|mjs|cjs|jsx)/;
|
|
6
|
+
var DEFAULT_EXTENSIONS = { ".js": ".js", ".ts": ".js", ".mts": ".js", ".cts": ".js", ".cjs": ".js", ".mjs": ".js" };
|
|
7
|
+
var transformImports = async (path, outExtension = {}, verbose = false) => {
|
|
8
|
+
let changed = false;
|
|
9
|
+
let contents = await readFile(path, "utf8");
|
|
10
|
+
if (Object.keys(outExtension).length <= 0) {
|
|
11
|
+
outExtension = DEFAULT_EXTENSIONS;
|
|
12
|
+
}
|
|
13
|
+
const matches = Array.from(contents.matchAll(IMPORT_PATTERN));
|
|
14
|
+
for (const match of matches) {
|
|
15
|
+
const importStr = match[0];
|
|
16
|
+
let transformed = importStr;
|
|
17
|
+
for (const ext of Object.keys(outExtension)) {
|
|
18
|
+
transformed = transformed.replaceAll(ext, outExtension[ext]);
|
|
19
|
+
}
|
|
20
|
+
if (importStr === transformed) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
changed = true;
|
|
24
|
+
contents = contents.replace(importStr, transformed);
|
|
25
|
+
if (verbose) {
|
|
26
|
+
console.debug(`[transform-ext] ${importStr} -> ${transformed}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (changed && verbose) {
|
|
30
|
+
console.debug(`[transform-ext] in ${path}
|
|
31
|
+
`);
|
|
32
|
+
}
|
|
33
|
+
return contents;
|
|
34
|
+
};
|
|
35
|
+
var transformExtPlugin = (pluginOptions) => {
|
|
36
|
+
const plugin = {
|
|
37
|
+
name: "transform-ext",
|
|
38
|
+
async setup(build) {
|
|
39
|
+
const outExtension = {};
|
|
40
|
+
if (pluginOptions.outExtension) {
|
|
41
|
+
Object.assign(outExtension, pluginOptions.outExtension);
|
|
42
|
+
} else if (build.initialOptions.outExtension) {
|
|
43
|
+
Object.assign(outExtension, build.initialOptions.outExtension);
|
|
44
|
+
} else {
|
|
45
|
+
Object.assign(outExtension, DEFAULT_EXTENSIONS);
|
|
46
|
+
}
|
|
47
|
+
const onLoad = async (loader, args) => {
|
|
48
|
+
let contents;
|
|
49
|
+
try {
|
|
50
|
+
contents = await transformImports(args.path, outExtension, pluginOptions.verbose);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(error);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return { contents, loader };
|
|
56
|
+
};
|
|
57
|
+
build.onLoad({ filter: TS_EXT_PATTERN }, onLoad.bind(this, "ts"));
|
|
58
|
+
build.onLoad({ filter: JS_EXT_PATTERN }, onLoad.bind(this, "js"));
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
return plugin;
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
transformExtPlugin,
|
|
65
|
+
transformImports
|
|
66
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Plugin } from "esbuild";
|
|
2
|
+
import type { PluginOptions, Extension } from './types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* You can use this method to transform the imports of a file by yourself without the need of the esbuild plugin
|
|
5
|
+
* @param path The file path you want to transform
|
|
6
|
+
* @param outExtension The extension map you want to transform
|
|
7
|
+
* @param options
|
|
8
|
+
* @returns The transformed file content
|
|
9
|
+
*/
|
|
10
|
+
export declare const transformImports: (path: string, outExtension?: Extension, verbose?: boolean) => Promise<string>;
|
|
11
|
+
export declare const transformExtPlugin: (pluginOptions: PluginOptions) => Plugin;
|
|
12
|
+
export default transformExtPlugin;
|
package/esbuild.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { build } from 'esbuild';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import { extname, dirname } from 'path';
|
|
4
|
+
import { EXTERNALS_NODE } from '@gjsify/resolve-npm';
|
|
5
|
+
|
|
6
|
+
const pkg = JSON.parse(
|
|
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 = {
|
|
17
|
+
entryPoints: ['src/index.ts'],
|
|
18
|
+
bundle: true,
|
|
19
|
+
minify: false,
|
|
20
|
+
external: [
|
|
21
|
+
...EXTERNALS_NODE,
|
|
22
|
+
'typescript',
|
|
23
|
+
'@deepkit/type-compiler',
|
|
24
|
+
'esbuild',
|
|
25
|
+
// '@gjsify/resolve-npm', can't be required in cjs builds
|
|
26
|
+
'@gjsify/esbuild-plugin-deno-loader',
|
|
27
|
+
'@gjsify/esbuild-plugin-deepkit',
|
|
28
|
+
'@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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Run this with `node esbuild.mjs`
|
|
2
|
+
|
|
3
|
+
import esbuild from 'esbuild';
|
|
4
|
+
import { transformExtPlugin } from "@gjsify/esbuild-plugin-transform-ext";
|
|
5
|
+
|
|
6
|
+
await esbuild.build({
|
|
7
|
+
plugins: [transformExtPlugin({ outExtension: {'.ts': '.js'}})],
|
|
8
|
+
entryPoints: ["./src/index.ts"],
|
|
9
|
+
outdir: "./dist/",
|
|
10
|
+
bundle: false,
|
|
11
|
+
format: "esm",
|
|
12
|
+
});
|
package/example/src/a.ts
ADDED
package/example/src/b.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/esbuild-plugin-transform-ext",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Transform import file extensions plugin for esbuild",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/cjs/index.cjs",
|
|
7
|
+
"module": "dist/esm/index.mjs",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"clear": "rm -rf dist tsconfig.tsbuildinfo",
|
|
11
|
+
"print:name": "echo '@gjsify/esbuild-plugin-transform-ext'",
|
|
12
|
+
"build": "yarn print:name && yarn build:js && yarn build:types",
|
|
13
|
+
"build:js": "node esbuild.mjs",
|
|
14
|
+
"build:types": "tsc --emitDeclarationOnly"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"gjs",
|
|
18
|
+
"gjsify",
|
|
19
|
+
"esbuild"
|
|
20
|
+
],
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"esbuild": "^0.17.19"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"esbuild": "^0.18.2",
|
|
26
|
+
"typescript": "^5.1.3"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
|
|
3
|
+
import type { Plugin, OnLoadArgs, OnLoadResult } from "esbuild";
|
|
4
|
+
import type { PluginOptions, Extension } from './types/index.js';
|
|
5
|
+
|
|
6
|
+
const IMPORT_PATTERN = /(import|from) ("|')\..*\.(js|ts|mjs|cjs|mts|cts)("|')/gm;
|
|
7
|
+
const TS_EXT_PATTERN = /\.(ts|mts|cts|tsx)/;
|
|
8
|
+
const JS_EXT_PATTERN = /\.(js|mjs|cjs|jsx)/;
|
|
9
|
+
const DEFAULT_EXTENSIONS: Extension = {'.js': '.js', '.ts': '.js', '.mts': '.js', '.cts': '.js', '.cjs': '.js', '.mjs': '.js'};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* You can use this method to transform the imports of a file by yourself without the need of the esbuild plugin
|
|
13
|
+
* @param path The file path you want to transform
|
|
14
|
+
* @param outExtension The extension map you want to transform
|
|
15
|
+
* @param options
|
|
16
|
+
* @returns The transformed file content
|
|
17
|
+
*/
|
|
18
|
+
export const transformImports = async (path: string, outExtension: Extension = {}, verbose = false) => {
|
|
19
|
+
|
|
20
|
+
let changed = false;
|
|
21
|
+
|
|
22
|
+
let contents = await readFile(path, 'utf8');
|
|
23
|
+
|
|
24
|
+
if(Object.keys(outExtension).length <= 0) {
|
|
25
|
+
outExtension = DEFAULT_EXTENSIONS;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const matches = Array.from(contents.matchAll(IMPORT_PATTERN));
|
|
29
|
+
|
|
30
|
+
for (const match of matches) {
|
|
31
|
+
const importStr = match[0];
|
|
32
|
+
let transformed = importStr;
|
|
33
|
+
for (const ext of Object.keys(outExtension)) {
|
|
34
|
+
transformed = transformed.replaceAll(ext, outExtension[ext])
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if(importStr === transformed) {
|
|
38
|
+
continue
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
changed = true;
|
|
42
|
+
|
|
43
|
+
contents = contents.replace(importStr, transformed)
|
|
44
|
+
if(verbose) {
|
|
45
|
+
console.debug(`[transform-ext] ${importStr} -> ${transformed}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if(changed && verbose) {
|
|
50
|
+
console.debug(`[transform-ext] in ${path}\n`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return contents
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const transformExtPlugin = (pluginOptions: PluginOptions) => {
|
|
57
|
+
const plugin: Plugin = {
|
|
58
|
+
name: 'transform-ext',
|
|
59
|
+
async setup(build) {
|
|
60
|
+
|
|
61
|
+
const outExtension: {[ext: string]: string;} = {};
|
|
62
|
+
|
|
63
|
+
if(pluginOptions.outExtension) {
|
|
64
|
+
Object.assign(outExtension, pluginOptions.outExtension)
|
|
65
|
+
} else if(build.initialOptions.outExtension) {
|
|
66
|
+
Object.assign(outExtension, build.initialOptions.outExtension)
|
|
67
|
+
} else {
|
|
68
|
+
Object.assign(outExtension, DEFAULT_EXTENSIONS)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const onLoad = async (loader: 'js'|'ts', args: OnLoadArgs): Promise<OnLoadResult | null | undefined> => {
|
|
72
|
+
let contents: string;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
contents = await transformImports(args.path, outExtension, pluginOptions.verbose)
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(error);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { contents, loader };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
build.onLoad({filter: TS_EXT_PATTERN}, onLoad.bind(this, 'ts'));
|
|
85
|
+
build.onLoad({filter: JS_EXT_PATTERN}, onLoad.bind(this, 'js'));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return plugin;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export default transformExtPlugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Extension = { [ext: string]: string }
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "ESNext",
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"outDir": "dist",
|
|
6
|
+
"types": ["node"],
|
|
7
|
+
"declarationDir": "dist/types",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"allowSyntheticDefaultImports": true,
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"allowImportingTsExtensions": true
|
|
12
|
+
},
|
|
13
|
+
"files": ["src/index.ts"]
|
|
14
|
+
}
|