Git Product home page Git Product logo

babel-plugin-tsconfig-paths's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pmunin agilgur5

babel-plugin-tsconfig-paths's Issues

Plugin fails to read tsconfig.json with comments

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?

Not handling file + folder of the same name

Consider this modules structure:

image

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:

let checkImport = transformedImport
try {
const stat = fs.statSync(transformedImport)
if (stat && stat.isDirectory()) {
checkImport = path.join(transformedImport, 'index')
}

This logic is incorrect. Instead, checkImport should be an array of either 1 or 2 elements (always include transformedImport, and possibly transformedImport/index).

Original extension always being added - not working with typescript transpile

Steps to reproduce

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

Expected result

The script runs normally.

Actual result

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:

const checkExtensions = [''].concat(extensions)
const fileVariations = checkExtensions.map((ext) => checkImport + ext)
const realFile = fileVariations.find((file) => fs.existsSync(file))
if (realFile) {
return (
relative
? getRelativePath(sourceFile, realFile)
: realFile

If I change it like this:

-       ? getRelativePath(sourceFile, realFile) 
+       ? getRelativePath(sourceFile, checkImport) 
-       : realFile 
+       : checkImport 

that fixes the problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.