@gjsify/esbuild-plugin-alias 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 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
@@ -33,6 +33,8 @@ var aliasPlugin = (aliasObj) => {
33
33
  if (resolvedAlias.errors.length > 0) {
34
34
  console.error(resolvedAlias.errors);
35
35
  return resolvedAlias;
36
+ } else if (resolvedAlias.external) {
37
+ return { path: resolvedAlias.path, external: true };
36
38
  } else {
37
39
  resolvedAliasPath = resolvedAlias.path;
38
40
  namespace = resolvedAlias.namespace;
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.4",
3
+ "version": "0.1.1",
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.19.10",
30
- "typescript": "^5.3.3"
34
+ "esbuild": "^0.27.4",
35
+ "typescript": "^6.0.2"
31
36
  }
32
37
  }
package/src/plugin.ts CHANGED
@@ -45,6 +45,8 @@ export const aliasPlugin = (aliasObj: Record<string, string>) => {
45
45
  if (resolvedAlias.errors.length > 0) {
46
46
  console.error(resolvedAlias.errors);
47
47
  return resolvedAlias;
48
+ } else if (resolvedAlias.external) {
49
+ return { path: resolvedAlias.path, external: true };
48
50
  } else {
49
51
  resolvedAliasPath = resolvedAlias.path;
50
52
  namespace = resolvedAlias.namespace;
package/tsconfig.json CHANGED
@@ -2,14 +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
13
  "allowImportingTsExtensions": true,
12
14
  "emitDeclarationOnly": true,
15
+ "strict": false
13
16
  },
14
- "files": ["src/index.ts"]
15
- }
17
+ "files": [
18
+ "src/index.ts"
19
+ ]
20
+ }
@@ -1,81 +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
- return null;
43
- }
44
- if (resolvedAliasPath.startsWith("http://")) {
45
- namespace = "http";
46
- resolvedAliasPath = resolvedAliasPath.slice(5);
47
- } else if (resolvedAliasPath.startsWith("https://")) {
48
- namespace = "https";
49
- resolvedAliasPath = resolvedAliasPath.slice(6);
50
- } else {
51
- const resolvedAlias = await build.resolve(resolvedAliasPath, {
52
- importer: args.importer,
53
- kind: args.kind,
54
- namespace,
55
- resolveDir: args.resolveDir,
56
- pluginData: args.pluginData
57
- });
58
- if (resolvedAlias.errors.length > 0) {
59
- console.error(resolvedAlias.errors);
60
- return resolvedAlias;
61
- } else {
62
- resolvedAliasPath = resolvedAlias.path;
63
- namespace = resolvedAlias.namespace;
64
- }
65
- }
66
- if ((0, import_fs.existsSync)(resolvedAliasPath)) {
67
- resolvedAliasPath = await (0, import_promises.realpath)(resolvedAliasPath);
68
- }
69
- return {
70
- path: resolvedAliasPath,
71
- namespace
72
- };
73
- });
74
- }
75
- };
76
- return plugin;
77
- };
78
- // Annotate the CommonJS export names for ESM import in node:
79
- 0 && (module.exports = {
80
- aliasPlugin
81
- });