Skip to content

Commit 0b0952d

Browse files
author
Martynas Žilinskas
committed
Another generating require relative path fix.
1 parent d96d84d commit 0b0952d

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

example/dist/hello.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/* Generated by main-file-generator */
2-
module.exports.default = require('build.js').Default;
2+
module.exports.default = require('./build.js').Default;

example/dist/world.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/* Generated by main-file-generator */
2-
module.exports.default = require('build.js').World;
2+
module.exports.default = require('./build.js').World;

src/generator.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ export default class Generator {
4343

4444
// Get `require` relative path.
4545
let requireFileFullPath = path.join(process.cwd(), this.config.proxyjs.requireFile);
46-
let requireFileRelativePath = path.relative(path.dirname(fullFilePath), requireFileFullPath);
47-
if(path.sep !== "/") {
48-
requireFileRelativePath = requireFileRelativePath.split(path.sep).join('/');
49-
}
50-
46+
let requireFileRelativePath = this.getRelativePath(fullFilePath, requireFileFullPath);
47+
5148
console.log(fullFilePath);
5249
//Write to file
5350
let stream = fs.createWriteStream(fullFilePath, { 'flags': 'w' });
@@ -73,4 +70,17 @@ export default class Generator {
7370
let parts = string.split('/');
7471
return (parts.length > 0) ? parts[parts.length - 1] : '';
7572
}
73+
74+
private getRelativePath(fromDir: string, toFilePath: string) {
75+
let filePath = path.relative(path.dirname(fromDir), toFilePath);
76+
if (path.sep !== "/") {
77+
filePath = filePath.split(path.sep).join('/');
78+
}
79+
80+
if (filePath.indexOf('/') === -1) {
81+
filePath = './' + filePath;
82+
}
83+
84+
return filePath;
85+
}
7686
}

0 commit comments

Comments
 (0)