@gjsify/esbuild-plugin-alias 0.0.3
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 +1 -0
- package/dist/cjs/index.cjs +80 -0
- package/dist/esm/index.mjs +54 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/plugin.d.ts +3 -0
- package/esbuild.mjs +49 -0
- package/package.json +32 -0
- package/src/index.ts +1 -0
- package/src/plugin.ts +71 -0
- package/tsconfig.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @gjsify/esbuild-plugin-alias
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
aliasPlugin: () => aliasPlugin
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(src_exports);
|
|
25
|
+
|
|
26
|
+
// src/plugin.ts
|
|
27
|
+
var import_fs = require("fs");
|
|
28
|
+
var import_promises = require("fs/promises");
|
|
29
|
+
function escapeRegExp(str) {
|
|
30
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
31
|
+
}
|
|
32
|
+
var aliasPlugin = (aliasObj) => {
|
|
33
|
+
const aliases = Object.keys(aliasObj);
|
|
34
|
+
const re = new RegExp(`^(${aliases.map((x) => escapeRegExp(x)).join("|")})$`);
|
|
35
|
+
const plugin = {
|
|
36
|
+
name: "alias",
|
|
37
|
+
setup(build) {
|
|
38
|
+
build.onResolve({ filter: re }, async (args) => {
|
|
39
|
+
let resolvedAliasPath = aliasObj[args.path];
|
|
40
|
+
let namespace = args.namespace;
|
|
41
|
+
if (resolvedAliasPath) {
|
|
42
|
+
if (resolvedAliasPath.startsWith("http://")) {
|
|
43
|
+
namespace = "http";
|
|
44
|
+
resolvedAliasPath = resolvedAliasPath.slice(5);
|
|
45
|
+
} else if (resolvedAliasPath.startsWith("https://")) {
|
|
46
|
+
namespace = "https";
|
|
47
|
+
resolvedAliasPath = resolvedAliasPath.slice(6);
|
|
48
|
+
} else {
|
|
49
|
+
const resolvedAlias = await build.resolve(resolvedAliasPath, {
|
|
50
|
+
importer: args.importer,
|
|
51
|
+
kind: args.kind,
|
|
52
|
+
namespace,
|
|
53
|
+
resolveDir: args.resolveDir,
|
|
54
|
+
pluginData: args.pluginData
|
|
55
|
+
});
|
|
56
|
+
if (resolvedAlias.errors) {
|
|
57
|
+
return resolvedAlias;
|
|
58
|
+
} else {
|
|
59
|
+
resolvedAliasPath = resolvedAlias.path;
|
|
60
|
+
namespace = resolvedAlias.namespace;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if ((0, import_fs.existsSync)(resolvedAliasPath)) {
|
|
64
|
+
resolvedAliasPath = await (0, import_promises.realpath)(resolvedAliasPath);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
path: resolvedAliasPath,
|
|
68
|
+
namespace
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
return plugin;
|
|
76
|
+
};
|
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
78
|
+
0 && (module.exports = {
|
|
79
|
+
aliasPlugin
|
|
80
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
import { realpath } from "fs/promises";
|
|
4
|
+
function escapeRegExp(str) {
|
|
5
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
6
|
+
}
|
|
7
|
+
var aliasPlugin = (aliasObj) => {
|
|
8
|
+
const aliases = Object.keys(aliasObj);
|
|
9
|
+
const re = new RegExp(`^(${aliases.map((x) => escapeRegExp(x)).join("|")})$`);
|
|
10
|
+
const plugin = {
|
|
11
|
+
name: "alias",
|
|
12
|
+
setup(build) {
|
|
13
|
+
build.onResolve({ filter: re }, async (args) => {
|
|
14
|
+
let resolvedAliasPath = aliasObj[args.path];
|
|
15
|
+
let namespace = args.namespace;
|
|
16
|
+
if (resolvedAliasPath) {
|
|
17
|
+
if (resolvedAliasPath.startsWith("http://")) {
|
|
18
|
+
namespace = "http";
|
|
19
|
+
resolvedAliasPath = resolvedAliasPath.slice(5);
|
|
20
|
+
} else if (resolvedAliasPath.startsWith("https://")) {
|
|
21
|
+
namespace = "https";
|
|
22
|
+
resolvedAliasPath = resolvedAliasPath.slice(6);
|
|
23
|
+
} else {
|
|
24
|
+
const resolvedAlias = await build.resolve(resolvedAliasPath, {
|
|
25
|
+
importer: args.importer,
|
|
26
|
+
kind: args.kind,
|
|
27
|
+
namespace,
|
|
28
|
+
resolveDir: args.resolveDir,
|
|
29
|
+
pluginData: args.pluginData
|
|
30
|
+
});
|
|
31
|
+
if (resolvedAlias.errors) {
|
|
32
|
+
return resolvedAlias;
|
|
33
|
+
} else {
|
|
34
|
+
resolvedAliasPath = resolvedAlias.path;
|
|
35
|
+
namespace = resolvedAlias.namespace;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (existsSync(resolvedAliasPath)) {
|
|
39
|
+
resolvedAliasPath = await realpath(resolvedAliasPath);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
path: resolvedAliasPath,
|
|
43
|
+
namespace
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return plugin;
|
|
51
|
+
};
|
|
52
|
+
export {
|
|
53
|
+
aliasPlugin
|
|
54
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './plugin.js';
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/esbuild-plugin-alias",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "",
|
|
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-alias'",
|
|
12
|
+
"build": "yarn print:name && yarn build:js && yarn build:types",
|
|
13
|
+
"build:js": "node esbuild.mjs",
|
|
14
|
+
"build:types": "tsc --emitDeclarationOnly"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/gjsify/gjsify.git"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/gjsify/gjsify/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/gjsify/gjsify/tree/main/packages/infra/esbuild-plugin-alias#readme",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"esbuild",
|
|
26
|
+
"plugin"
|
|
27
|
+
],
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"esbuild": "^0.18.3",
|
|
30
|
+
"typescript": "^5.1.3"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './plugin.js';
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync } from "fs";
|
|
2
|
+
import { realpath } from "fs/promises";
|
|
3
|
+
|
|
4
|
+
import type { Plugin } from "esbuild";
|
|
5
|
+
|
|
6
|
+
function escapeRegExp(str: string) {
|
|
7
|
+
// $& means the whole matched string
|
|
8
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export const aliasPlugin = (aliasObj: Record<string, string>) => {
|
|
13
|
+
const aliases = Object.keys(aliasObj);
|
|
14
|
+
const re = new RegExp(`^(${aliases.map(x => escapeRegExp(x)).join('|')})$`);
|
|
15
|
+
|
|
16
|
+
const plugin: Plugin = {
|
|
17
|
+
name: 'alias',
|
|
18
|
+
setup(build) {
|
|
19
|
+
// we do not register 'file' namespace here, because the root file won't be processed
|
|
20
|
+
// https://github.com/evanw/esbuild/issues/791
|
|
21
|
+
build.onResolve({ filter: re }, async (args) => {
|
|
22
|
+
let resolvedAliasPath = aliasObj[args.path];
|
|
23
|
+
|
|
24
|
+
let namespace = args.namespace;
|
|
25
|
+
|
|
26
|
+
if (resolvedAliasPath) {
|
|
27
|
+
|
|
28
|
+
if (resolvedAliasPath.startsWith('http://')) {
|
|
29
|
+
namespace = 'http';
|
|
30
|
+
resolvedAliasPath = resolvedAliasPath.slice(5)
|
|
31
|
+
} else if (resolvedAliasPath.startsWith('https://')) {
|
|
32
|
+
namespace = 'https';
|
|
33
|
+
resolvedAliasPath = resolvedAliasPath.slice(6)
|
|
34
|
+
} else {
|
|
35
|
+
const resolvedAlias = (await build.resolve(resolvedAliasPath, {
|
|
36
|
+
importer: args.importer,
|
|
37
|
+
kind: args.kind,
|
|
38
|
+
namespace: namespace,
|
|
39
|
+
resolveDir: args.resolveDir,
|
|
40
|
+
pluginData: args.pluginData,
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
if (resolvedAlias.errors) {
|
|
44
|
+
return resolvedAlias;
|
|
45
|
+
} else {
|
|
46
|
+
resolvedAliasPath = resolvedAlias.path;
|
|
47
|
+
namespace = resolvedAlias.namespace;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (existsSync(resolvedAliasPath)) {
|
|
52
|
+
resolvedAliasPath = await realpath(resolvedAliasPath);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// console.debug(`resolvedAliasPath: ${args.path} -> ${resolvedAliasPath}`);
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
path: resolvedAliasPath,
|
|
59
|
+
namespace: namespace,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return null;
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return plugin;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export default aliasPlugin;
|
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
|
+
}
|