@gjsify/esbuild-plugin-alias 0.0.3 → 0.1.0

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 CHANGED
@@ -1 +1,33 @@
1
1
  # @gjsify/esbuild-plugin-alias
2
+
3
+ esbuild plugin for module aliasing. Used internally by @gjsify/esbuild-plugin-gjsify.
4
+
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/esbuild-plugin-alias
11
+ # or
12
+ yarn add @gjsify/esbuild-plugin-alias
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { build } from 'esbuild';
19
+ import { aliasPlugin } from '@gjsify/esbuild-plugin-alias';
20
+
21
+ await build({
22
+ entryPoints: ['src/index.ts'],
23
+ bundle: true,
24
+ outfile: 'dist/index.js',
25
+ plugins: [aliasPlugin({
26
+ 'original-module': 'replacement-module',
27
+ })],
28
+ });
29
+ ```
30
+
31
+ ## License
32
+
33
+ MIT
@@ -13,37 +13,40 @@ var aliasPlugin = (aliasObj) => {
13
13
  build.onResolve({ filter: re }, async (args) => {
14
14
  let resolvedAliasPath = aliasObj[args.path];
15
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);
16
+ if (!resolvedAliasPath) {
17
+ return null;
18
+ }
19
+ if (resolvedAliasPath.startsWith("http://")) {
20
+ namespace = "http";
21
+ resolvedAliasPath = resolvedAliasPath.slice(5);
22
+ } else if (resolvedAliasPath.startsWith("https://")) {
23
+ namespace = "https";
24
+ resolvedAliasPath = resolvedAliasPath.slice(6);
25
+ } else {
26
+ const resolvedAlias = await build.resolve(resolvedAliasPath, {
27
+ importer: args.importer,
28
+ kind: args.kind,
29
+ namespace,
30
+ resolveDir: args.resolveDir,
31
+ pluginData: args.pluginData
32
+ });
33
+ if (resolvedAlias.errors.length > 0) {
34
+ console.error(resolvedAlias.errors);
35
+ return resolvedAlias;
36
+ } else if (resolvedAlias.external) {
37
+ return { path: resolvedAlias.path, external: true };
23
38
  } 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);
39
+ resolvedAliasPath = resolvedAlias.path;
40
+ namespace = resolvedAlias.namespace;
40
41
  }
41
- return {
42
- path: resolvedAliasPath,
43
- namespace
44
- };
45
42
  }
46
- return null;
43
+ if (existsSync(resolvedAliasPath)) {
44
+ resolvedAliasPath = await realpath(resolvedAliasPath);
45
+ }
46
+ return {
47
+ path: resolvedAliasPath,
48
+ namespace
49
+ };
47
50
  });
48
51
  }
49
52
  };
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
- 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 = {
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-alias",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
- "main": "dist/cjs/index.cjs",
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
- "print:name": "echo '@gjsify/esbuild-plugin-alias'",
12
- "build": "yarn print:name && yarn build:js && yarn build:types",
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.18.3",
30
- "typescript": "^5.1.3"
34
+ "esbuild": "^0.27.4",
35
+ "typescript": "^6.0.2"
31
36
  }
32
37
  }
package/src/plugin.ts CHANGED
@@ -23,44 +23,44 @@ export const aliasPlugin = (aliasObj: Record<string, string>) => {
23
23
 
24
24
  let namespace = args.namespace;
25
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
- }));
26
+ if (!resolvedAliasPath) {
27
+ return null;
28
+ }
42
29
 
43
- if (resolvedAlias.errors) {
44
- return resolvedAlias;
45
- } else {
46
- resolvedAliasPath = resolvedAlias.path;
47
- namespace = resolvedAlias.namespace;
48
- }
49
- }
30
+ if (resolvedAliasPath.startsWith('http://')) {
31
+ namespace = 'http';
32
+ resolvedAliasPath = resolvedAliasPath.slice(5)
33
+ } else if (resolvedAliasPath.startsWith('https://')) {
34
+ namespace = 'https';
35
+ resolvedAliasPath = resolvedAliasPath.slice(6)
36
+ } else {
37
+ const resolvedAlias = (await build.resolve(resolvedAliasPath, {
38
+ importer: args.importer,
39
+ kind: args.kind,
40
+ namespace: namespace,
41
+ resolveDir: args.resolveDir,
42
+ pluginData: args.pluginData,
43
+ }));
50
44
 
51
- if (existsSync(resolvedAliasPath)) {
52
- resolvedAliasPath = await realpath(resolvedAliasPath);
45
+ if (resolvedAlias.errors.length > 0) {
46
+ console.error(resolvedAlias.errors);
47
+ return resolvedAlias;
48
+ } else if (resolvedAlias.external) {
49
+ return { path: resolvedAlias.path, external: true };
50
+ } else {
51
+ resolvedAliasPath = resolvedAlias.path;
52
+ namespace = resolvedAlias.namespace;
53
53
  }
54
+ }
54
55
 
55
- // console.debug(`resolvedAliasPath: ${args.path} -> ${resolvedAliasPath}`);
56
-
57
- return {
58
- path: resolvedAliasPath,
59
- namespace: namespace,
60
- }
56
+ if (existsSync(resolvedAliasPath)) {
57
+ resolvedAliasPath = await realpath(resolvedAliasPath);
61
58
  }
62
59
 
63
- return null;
60
+ return {
61
+ path: resolvedAliasPath,
62
+ namespace: namespace,
63
+ };
64
64
  });
65
65
  },
66
66
  };
package/tsconfig.json CHANGED
@@ -2,13 +2,19 @@
2
2
  "compilerOptions": {
3
3
  "module": "ESNext",
4
4
  "target": "ESNext",
5
+ "rootDir": "src",
5
6
  "outDir": "dist",
6
- "types": ["node"],
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
+ "emitDeclarationOnly": true,
15
+ "strict": false
12
16
  },
13
- "files": ["src/index.ts"]
14
- }
17
+ "files": [
18
+ "src/index.ts"
19
+ ]
20
+ }
@@ -1,80 +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
- 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
- });