-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to normalize module identifiers.
- Loading branch information
Showing
4 changed files
with
91 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import resolve from "resolve"; | ||
import { distDir, eachFile, reparse, reprint } from './helpers'; | ||
|
||
eachFile(distDir, (file, relPath) => new Promise((resolve, reject) => { | ||
fs.readFile(file, "utf8", (error, source) => { | ||
if (error) return reject(error); | ||
const output = transform(source, file); | ||
if (source === output) { | ||
resolve(file); | ||
} else { | ||
console.log("Resolved module identifiers in " + relPath); | ||
fs.writeFile(file, output, "utf8", error => { | ||
error ? reject(error) : resolve(file); | ||
}); | ||
} | ||
}); | ||
})); | ||
|
||
import * as recast from "recast"; | ||
const n = recast.types.namedTypes; | ||
type Node = recast.types.namedTypes.Node; | ||
|
||
function transform(code: string, file: string) { | ||
const ast = reparse(code); | ||
|
||
recast.visit(ast, { | ||
visitImportDeclaration(path) { | ||
this.traverse(path); | ||
normalizeSourceString(file, path.node.source); | ||
}, | ||
|
||
visitImportExpression(path) { | ||
this.traverse(path); | ||
normalizeSourceString(file, path.node.source); | ||
}, | ||
|
||
visitExportAllDeclaration(path) { | ||
this.traverse(path); | ||
normalizeSourceString(file, path.node.source); | ||
}, | ||
|
||
visitExportNamedDeclaration(path) { | ||
this.traverse(path); | ||
normalizeSourceString(file, path.node.source); | ||
}, | ||
}); | ||
|
||
return reprint(ast); | ||
} | ||
|
||
function isRelative(id: string) { | ||
return id.startsWith("./") || id.startsWith("../"); | ||
} | ||
|
||
function normalizeSourceString(file: string, source?: Node | null) { | ||
if (source && n.StringLiteral.check(source) && isRelative(source.value)) { | ||
try { | ||
source.value = normalizeId(source.value, file); | ||
} catch (error) { | ||
console.error(`Failed to resolve ${source.value} in ${file}`); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
function normalizeId(id: string, file: string) { | ||
const basedir = path.dirname(file); | ||
const absPath = resolve.sync(id, { | ||
basedir, | ||
packageFilter(pkg) { | ||
if (pkg.module) { | ||
return { ...pkg, main: pkg.module }; | ||
} | ||
}, | ||
}); | ||
const relPath = path.relative(basedir, absPath); | ||
const relId = relPath.split(path.sep).join('/'); | ||
return isRelative(relId) ? relId : "./" + relId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters