@adamhl8/configs 0.17.0 → 0.17.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.
@@ -8,6 +8,7 @@ import * as hookable0 from "hookable";
8
8
  //#region src/configs/tsdown.d.ts
9
9
  declare const tsdownConfig: MergeConfigFn<{
10
10
  entry?: rolldown0.InputOption;
11
+ exports?: boolean | tsdown0.ExportsOptions;
11
12
  external?: rolldown0.ExternalOption;
12
13
  noExternal?: (string | RegExp | (string | RegExp)[]) | ((id: string, importer: string | undefined) => boolean | null | undefined | void);
13
14
  skipNodeModulesBundle?: boolean;
@@ -58,7 +59,6 @@ declare const tsdownConfig: MergeConfigFn<{
58
59
  attw?: boolean | tsdown0.AttwOptions;
59
60
  report?: boolean | tsdown0.ReportOptions;
60
61
  globImport?: boolean;
61
- exports?: boolean | tsdown0.ExportsOptions;
62
62
  publicDir?: tsdown0.CopyOptions | tsdown0.CopyOptionsFn;
63
63
  copy?: tsdown0.CopyOptions | tsdown0.CopyOptionsFn;
64
64
  hooks?: Partial<tsdown0.TsdownHooks> | ((hooks: hookable0.Hookable<tsdown0.TsdownHooks>) => void | Promise<void>);
@@ -1 +1 @@
1
- {"version":3,"file":"tsdown.d.ts","names":[],"sources":["../../src/configs/tsdown.ts"],"sourcesContent":[],"mappings":";;;;;;;;cAyCa;UAAyF,SAAA,CAAA;;;;;;;EAAzF,GAAA,CAAA,QAAA,CAAA,MAAyF,EAAA,GAAA,CAAA;EAAA,MAAA,CAAA,QAAA,CAAA,MAAA,EAAA,MAAA,CAAA;OAAA,CAAA,EAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAA7E,SAAA,MAAA,EAAA,KAAA;EACZ,SAAA,QAAA,EAAwF,SAAA;EAAA,SAAA,MAAA,EAAA,UAAA;WAAA,SAAzE,EAAA,IAAA;WAAA,IAAA,EAAA,KAAA;WAAA,GAAA,EAAA;IAAA,SAAA,UAAA,EAAA,IAAA;;;;;;;;;;cAAf,iBAAe,sBAAA,KAAyE,OAAA,CAAzE,OAAA"}
1
+ {"version":3,"file":"tsdown.d.ts","names":[],"sources":["../../src/configs/tsdown.ts"],"sourcesContent":[],"mappings":";;;;;;;;cAyCa;UAAyF,SAAA,CAAA;;;;;;;EAAzF,MAAA,CAAA,EAAA,MAAyF,GAAA,MAAA,EAAA,GAAA,KAAA;EAAA,GAAA,CAAA,QAAA,CAAA,MAAA,EAAA,GAAA,CAAA;QAAA,CAAA,QAAA,CAAA,MAAA,EAAA,MAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAA7E,SAAA,MAAA,EAAA,KAAA;EACZ,SAAA,QAAA,EAAwF,SAAA;EAAA,SAAA,MAAA,EAAA,UAAA;WAAA,SAAzE,EAAA,IAAA;WAAA,IAAA,EAAA,KAAA;WAAA,GAAA,EAAA;IAAA,SAAA,UAAA,EAAA,IAAA;;;;;;;;;;cAAf,iBAAe,sBAAA,KAAyE,OAAA,CAAzE,OAAA"}
@@ -38,7 +38,7 @@ async function checkExports(filePaths) {
38
38
  visit(sourceFile);
39
39
  if (exportDeclarations.length === 0) return;
40
40
  const exportDeclarationText = exportDeclarations.map((declaration) => declaration.getText()).join("\n");
41
- EXPORT_ERRORS.push(`${pc.redBright("✗")} ${filePath}\n${pc.dim(exportDeclarationText)}\n`);
41
+ EXPORT_ERRORS.push(`${pc.redBright("✗")} ${pc.blue(filePath)}\n${pc.dim(exportDeclarationText)}\n`);
42
42
  });
43
43
  return Promise.all(filePromises);
44
44
  });
@@ -53,34 +53,61 @@ function getPathsMap() {
53
53
  for (let [alias, [aliasDir]] of Object.entries(tsconfig.config.compilerOptions?.paths ?? {})) {
54
54
  if (!alias.endsWith("/*")) continue;
55
55
  if (!aliasDir?.endsWith("/*")) continue;
56
- aliasDir = aliasDir.slice(0, -2);
57
56
  alias = alias.slice(0, -2);
57
+ aliasDir = aliasDir.slice(0, -2);
58
58
  pathsMap[alias] = aliasDir;
59
59
  }
60
60
  return pathsMap;
61
61
  }
62
- function changeExtension(parsedPath, newExtension) {
62
+ function resolveImportPath(importPath, filePath, pathsMap) {
63
+ const fileDir = path.dirname(filePath);
64
+ for (const [alias, aliasDir] of Object.entries(pathsMap)) {
65
+ if (!importPath.startsWith(alias)) continue;
66
+ const relativePath = importPath.slice(alias.length + 1);
67
+ return path.resolve(aliasDir, relativePath);
68
+ }
69
+ return path.resolve(fileDir, importPath);
70
+ }
71
+ function changeExtension(importParts, newExtension) {
63
72
  return {
64
- ...parsedPath,
73
+ ...importParts,
65
74
  ext: newExtension,
66
75
  base: ""
67
76
  };
68
77
  }
69
- function transformExtension(filePath) {
70
- const pathParts = path.parse(filePath);
71
- if (!(pathParts.ext === ".js" || pathParts.ext === "")) return filePath;
72
- return path.format(changeExtension(pathParts, ".ts"));
78
+ function getNewExtensionPathParts(importParts) {
79
+ const tsExtensionLookups = [
80
+ "",
81
+ ".js",
82
+ ".jsx",
83
+ ".ts",
84
+ ".tsx"
85
+ ];
86
+ const allowedExtensions = [
87
+ ".ts",
88
+ ".tsx",
89
+ ".d.ts"
90
+ ];
91
+ if (!tsExtensionLookups.includes(importParts.ext)) return {
92
+ ext: importParts.ext,
93
+ base: importParts.base
94
+ };
95
+ if (tsExtensionLookups.includes(importParts.ext)) for (const allowedExtension of allowedExtensions) {
96
+ const targetPathParts = changeExtension(importParts, allowedExtension);
97
+ const targetPath = path.format(targetPathParts);
98
+ if (fss.existsSync(targetPath)) return {
99
+ ext: targetPathParts.ext,
100
+ base: targetPathParts.base
101
+ };
102
+ }
73
103
  }
74
- function transformRelativeImport(filePath, importPath, pathsMap) {
75
- const currentDir = path.dirname(filePath);
76
- const absoluteImportPath = path.resolve(currentDir, importPath);
77
- if (!fss.existsSync(absoluteImportPath)) return err(`skipped transform of '${importPath}': target file does not exist`, void 0);
104
+ function getAliasPathParts(importParts, pathsMap) {
105
+ const importPath = path.format(importParts);
78
106
  for (const [alias, aliasDir] of Object.entries(pathsMap)) {
79
- const relativeToAliasDir = path.relative(aliasDir, absoluteImportPath);
107
+ const relativeToAliasDir = path.relative(aliasDir, importPath);
80
108
  if (relativeToAliasDir.startsWith("..")) continue;
81
- return `${alias}/${relativeToAliasDir}`;
109
+ return { dir: path.parse(`${alias}/${relativeToAliasDir}`).dir };
82
110
  }
83
- return err(`could not find alias for '${importPath}'`, void 0);
84
111
  }
85
112
  async function fixImports(filePaths, { write, importIgnoreStrings, skipAlias }) {
86
113
  const IMPORT_ERRORS = [];
@@ -97,23 +124,34 @@ async function fixImports(filePaths, { write, importIgnoreStrings, skipAlias })
97
124
  const isAliasImport = Object.keys(pathsMap).some((alias) => importPath.startsWith(alias));
98
125
  if (!(isRelativeImport || isAliasImport)) return match;
99
126
  if (importIgnoreStrings.some((ignoreString) => importPath.includes(ignoreString))) return match;
100
- let newImportPath = transformExtension(importPath);
127
+ let newImportParts = path.parse(importPath);
128
+ const resolvedImportPath = resolveImportPath(importPath, filePath, pathsMap);
129
+ const resolvedImportParts = path.parse(resolvedImportPath);
130
+ const transformExtensionResult = getNewExtensionPathParts(resolvedImportParts);
131
+ if (transformExtensionResult) newImportParts = {
132
+ ...newImportParts,
133
+ ...transformExtensionResult
134
+ };
135
+ else importErrorsForFile.push(`skipped extension transform of '${importPath}': target file not found`);
101
136
  if (isRelativeImport && !skipAlias) {
102
- const transformedRelativeImport = transformRelativeImport(filePath, newImportPath, pathsMap);
103
- if (isErr(transformedRelativeImport)) importErrorsForFile.push(transformedRelativeImport.messageChain);
104
- else newImportPath = transformedRelativeImport;
137
+ const transformToAliasImportResult = getAliasPathParts(resolvedImportParts, pathsMap);
138
+ if (transformToAliasImportResult) newImportParts = {
139
+ ...newImportParts,
140
+ ...transformToAliasImportResult
141
+ };
142
+ else importErrorsForFile.push(`skipped transforming relative import path '${importPath}': could not find appropriate alias`);
105
143
  }
144
+ const newImportPath = path.format(newImportParts);
106
145
  if (newImportPath === importPath) return match;
107
146
  const { ext: originalExt } = path.parse(importPath);
108
- const newPathParts = path.parse(newImportPath);
109
- const { ext: newExt } = newPathParts;
147
+ const { ext: newExt } = newImportParts;
110
148
  let newImportPathString = newImportPath;
111
- if (newExt !== originalExt) newImportPathString = `${path.format(changeExtension(newPathParts, ""))}${pc.greenBright(newExt)}`;
149
+ if (newExt !== originalExt) newImportPathString = `${path.format(changeExtension(newImportParts, ""))}${pc.greenBright(newExt)}`;
112
150
  transformedImportsForFile.push(`'${importPath}' -> '${newImportPathString}'`);
113
151
  return match.replace(importPath, newImportPath);
114
152
  });
115
- if (importErrorsForFile.length > 0) IMPORT_ERRORS.push(`${pc.redBright("✗")} ${filePath}\n${importErrorsForFile.join("\n")}\n`);
116
- if (transformedImportsForFile.length > 0) TRANSFORMED_IMPORTS.push(`${pc.greenBright("✓")} ${filePath}\n${transformedImportsForFile.join("\n")}\n`);
153
+ if (importErrorsForFile.length > 0) IMPORT_ERRORS.push(`${pc.redBright("✗")} ${pc.blue(filePath)}\n${importErrorsForFile.join("\n")}\n`);
154
+ if (transformedImportsForFile.length > 0) TRANSFORMED_IMPORTS.push(`${pc.greenBright("✓")} ${pc.blue(filePath)}\n${transformedImportsForFile.join("\n")}\n`);
117
155
  if (transformedContent === content) return;
118
156
  if (write) await fs.writeFile(filePath, transformedContent);
119
157
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamhl8/configs",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",