js-brecht / babel-plugin-tsconfig-paths Goto Github PK
View Code? Open in Web Editor NEWResolve imports/requires based on path aliases in tsconfig.json
License: ISC License
Resolve imports/requires based on path aliases in tsconfig.json
License: ISC License
While it is not standard practice to have comments in json files, the typescript website and many project boilerplates include comments in their tsconfig.json. These comments are usually there to describe what the tsconfig.json properties do, and the parsers for tsc and other tools seem to support them.
Is this something this plugin can look into?
Consider this modules structure:
and this import:
import xxx from "~/cleaners/model_id"
Your babel plugin fails to process this import.
This is because if it sees a directory at the import location, it explicitly appends /index
to the import name:
babel-plugin-tsconfig-paths/lib/resolve.js
Lines 71 to 76 in a534e4d
This logic is incorrect. Instead, checkImport
should be an array of either 1 or 2 elements (always include transformedImport
, and possibly transformedImport/index
).
Using null coalescence (??) is probably what is desired here:
babel-plugin-tsconfig-paths/lib/opts.js
Line 84 in bccfa99
Create src/foo.ts
:
import baz from "~/bar/baz"
create src/bar/baz.ts
:
console.log("I am baz")
create tsconfig.json
:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": [
"src/*"
],
},
}
}
create .babelrc
:
{
"presets": [
[
"@babel/env",
{
"targets": {
"node": true
}
}
]
],
"plugins": [
"@babel/transform-typescript",
"tsconfig-paths"
]
}
and compile the project with:
babel src --extensions '.js,.ts' --out-dir build
then start it with:
node build/foo
The script runs normally.
The script crashes:
Error: Cannot find module './bar/baz.ts'
If you check build/foo.js
, you will see:
var _baz = _interopRequireDefault(require("./bar/baz.ts"));
You see the problem? The plugin keeps the original source file extension, which is replaced on babel compile.
The problem is in this code:
babel-plugin-tsconfig-paths/lib/resolve.js
Lines 83 to 91 in a534e4d
If I change it like this:
- ? getRelativePath(sourceFile, realFile)
+ ? getRelativePath(sourceFile, checkImport)
- : realFile
+ : checkImport
that fixes the problem.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.