xanix 1.0.3 → 1.0.4

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.
@@ -1,6 +1,5 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import ts from "typescript";
4
3
  import alias from "@rollup/plugin-alias";
5
4
  export default function xanixTsconfigAlias() {
6
5
  const root = process.cwd();
@@ -10,22 +9,23 @@ export default function xanixTsconfigAlias() {
10
9
  name: "xanix-tsconfig-alias",
11
10
  };
12
11
  }
13
- const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
14
- if (configFile.error) {
15
- const message = ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n");
16
- throw new Error(`[xanix] Failed to read tsconfig.json:\n${message}`);
12
+ let config;
13
+ try {
14
+ const content = fs.readFileSync(tsconfigPath, "utf8");
15
+ config = parseJsonc(content);
16
+ }
17
+ catch (error) {
18
+ throw new Error(`[xanix] Failed to read tsconfig.json:\n${error.message}`);
17
19
  }
18
- const config = configFile.config;
19
20
  const compilerOptions = config.compilerOptions ?? {};
20
21
  const baseUrl = compilerOptions.baseUrl ?? ".";
21
22
  const basePath = path.resolve(path.dirname(tsconfigPath), baseUrl);
22
23
  const paths = compilerOptions.paths ?? {};
23
24
  const entries = Object.entries(paths)
24
25
  .map(([find, replacements]) => {
25
- const replacement = replacements[0];
26
- if (!replacement) {
26
+ const replacement = replacements?.[0];
27
+ if (!replacement)
27
28
  return null;
28
- }
29
29
  if (find.includes("*")) {
30
30
  const findPrefix = find.replace(/\/?\*$/, "");
31
31
  const replacementPrefix = replacement.replace(/\/?\*$/, "");
@@ -39,7 +39,7 @@ export default function xanixTsconfigAlias() {
39
39
  replacement: path.resolve(basePath, replacement),
40
40
  };
41
41
  })
42
- .filter((entry) => entry !== null);
42
+ .filter(Boolean);
43
43
  return alias({
44
44
  entries,
45
45
  });
@@ -47,3 +47,13 @@ export default function xanixTsconfigAlias() {
47
47
  function escapeRegExp(value) {
48
48
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
49
49
  }
50
+ function parseJsonc(content) {
51
+ const withoutComments = content
52
+ // Remove /* block comments */
53
+ .replace(/\/\*[\s\S]*?\*\//g, "")
54
+ // Remove // comments
55
+ .replace(/\/\/.*$/gm, "");
56
+ // Remove trailing commas
57
+ const withoutTrailingCommas = withoutComments.replace(/,\s*([}\]])/g, "$1");
58
+ return JSON.parse(withoutTrailingCommas);
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xanix",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",