Skip to content

Commit 2294579

Browse files
committed
fix: Fix module resolution issue
1 parent ea9fd6e commit 2294579

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

Diff for: packages/ts-doc/src/models/DocFile.js

+32-35
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,73 @@
1-
const {context} = require("../context");
2-
const path = require("path");
3-
const fs = require("fs");
4-
const normalizePath = require("normalize-path");
5-
const mapExported = new Map();
1+
const {context} = require('../context')
2+
const path = require('path')
3+
const fs = require('fs')
4+
const normalizePath = require('normalize-path')
5+
const readPkgUp = require('read-pkg-up')
6+
const mapExported = new Map()
67

78
class DocFile {
89
/**
910
*
1011
* @param file
1112
*/
1213
constructor(file) {
13-
this.file = normalizePath(file);
14-
this.symbols = new Map();
15-
this.contents = fs.readFileSync(file).toString();
14+
this.file = normalizePath(file)
15+
this.symbols = new Map()
16+
this.contents = fs.readFileSync(file).toString()
1617
}
1718

1819
get path() {
19-
return normalizePath((this.file || "").replace(".d.ts", ".ts"));
20+
return normalizePath((this.file || '').replace('.d.ts', '.ts'))
2021
}
2122

2223
get srcPath() {
23-
return normalizePath(context.srcResolver(this.path));
24+
return normalizePath(context.srcResolver(this.path))
2425
}
2526

2627
get relativePackagePath() {
27-
const p = normalizePath(path.join(context.rootDir, context.packagesDir));
28+
const p = normalizePath(path.join(context.rootDir, context.packagesDir))
2829

29-
return normalizePath(this.srcPath.replace(p, ""));
30+
return normalizePath(this.srcPath.replace(p, ''))
3031
}
3132

3233
get relativePath() {
33-
return normalizePath(this.srcPath.replace(`${context.rootDir}/`, ""));
34+
return normalizePath(this.srcPath.replace(`${context.rootDir}/`, ''))
3435
}
3536

3637
get module() {
37-
const [pkgName, subPkgName] = this.relativePackagePath.replace(/^\//, "").split("/");
38-
const pkgSettings = context.modules[pkgName];
39-
let moduleName = pkgName;
38+
const [packagePath] = this.relativePackagePath.split('/src')
39+
const packageDir = context.rootDir + '/' + context.packagesDir + '/' + packagePath
4040

41-
if (typeof pkgSettings === "object" && context.modules[pkgName][subPkgName]) {
42-
moduleName = pkgName + "/" + subPkgName;
43-
}
41+
const {packageJson: {name}} = readPkgUp.sync({cwd: packageDir})
4442

4543
return {
46-
modulePath: path.join(context.rootDir, context.packagesDir, pkgName),
47-
moduleName: `${context.scope}/${moduleName}`,
48-
importFrom: `${context.scope}/${pkgName}`,
49-
pkgName,
50-
subPkgName
51-
};
44+
modulePath: packageDir,
45+
moduleName: name,
46+
importFrom: name,
47+
pkgName: name.replace(context.scope, '')
48+
}
5249
}
5350

5451
requireModule() {
55-
const {modulePath} = this.module;
56-
let file = path.join(modulePath, "index.js");
52+
const {modulePath} = this.module
53+
let file = path.join(modulePath, 'index.js')
5754

58-
if (fs.existsSync(path.join(modulePath, "package.json"))) {
59-
const pkg = require(path.join(modulePath, "package.json"));
60-
file = path.join(modulePath, pkg.main);
55+
if (fs.existsSync(path.join(modulePath, 'package.json'))) {
56+
const pkg = require(path.join(modulePath, 'package.json'))
57+
file = path.join(modulePath, pkg.main)
6158
}
6259

6360
if (mapExported.has(file)) {
64-
return mapExported.get(file);
61+
return mapExported.get(file)
6562
}
6663

6764
if (fs.existsSync(file)) {
68-
mapExported.set(file, require(file));
69-
return mapExported.get(file);
65+
mapExported.set(file, require(file))
66+
return mapExported.get(file)
7067
}
7168

72-
return undefined;
69+
return undefined
7370
}
7471
}
7572

76-
module.exports.DocFile = DocFile;
73+
module.exports.DocFile = DocFile

Diff for: packages/ts-doc/src/scan/scan.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const globby = require("globby");
44
const path = require("path");
55
const chalk = require("chalk");
6-
const logger = require("fancy-log");
76
const {render} = require("../render/render");
87
const {context} = require("../context");
98
const {trim} = require("../utils/trim");

0 commit comments

Comments
 (0)