Skip to content

Commit

Permalink
fix #1355: ignore tsconfig.json in node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jun 7, 2021
1 parent 5065800 commit d3ef487
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,12 @@

This approach should ensure maximum compatibility with all JavaScript environments that support ES5 and above. Note that this means minified files containing Unicode properties may be slightly larger than before.

* Ignore `tsconfig.json` files inside `node_modules` ([#1355](https://github.com/evanw/esbuild/issues/1355))

Package authors often publish their `tsconfig.json` files to npm because of npm's default-include publishing model and because these authors probably don't know about `.npmignore` files. People trying to use these packages with esbuild have historically complained that esbuild is respecting `tsconfig.json` in these cases. The assumption is that the package author published these files by accident.

With this release, esbuild will no longer respect `tsconfig.json` files when the source file is inside a `node_modules` folder. Note that `tsconfig.json` files inside `node_modules` are still parsed, and extending `tsconfig.json` files from inside a package is still supported.

## 0.12.6

* Improve template literal lowering transformation conformance ([#1327](https://github.com/evanw/esbuild/issues/1327))
Expand Down
25 changes: 25 additions & 0 deletions internal/bundler/bundler_tsconfig_test.go
Expand Up @@ -994,6 +994,31 @@ func TestTsconfigJsonNodeModulesImplicitFile(t *testing.T) {
})
}

func TestTsconfigJsonInsideNodeModules(t *testing.T) {
tsconfig_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/app/entry.tsx": `
import 'foo'
`,
"/Users/user/project/src/node_modules/foo/index.tsx": `
console.log(<div/>)
`,
"/Users/user/project/src/node_modules/foo/tsconfig.json": `
{
"compilerOptions": {
"jsxFactory": "TEST_FAILED"
}
}
`,
},
entryPaths: []string{"/Users/user/project/src/app/entry.tsx"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/Users/user/project/out.js",
},
})
}

func TestTsconfigWarningsInsideNodeModules(t *testing.T) {
tsconfig_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
6 changes: 6 additions & 0 deletions internal/bundler/snapshots/snapshots_tsconfig.txt
Expand Up @@ -279,6 +279,12 @@ console.log("works");
// Users/user/project/src/entry.jsx
console.log(/* @__PURE__ */ baseFactory("div", null), /* @__PURE__ */ baseFactory(derivedFragment, null));

================================================================================
TestTsconfigJsonInsideNodeModules
---------- /Users/user/project/out.js ----------
// Users/user/project/src/node_modules/foo/index.tsx
console.log(/* @__PURE__ */ React.createElement("div", null));

================================================================================
TestTsconfigJsonNodeModulesImplicitFile
---------- /Users/user/project/out.js ----------
Expand Down
54 changes: 38 additions & 16 deletions internal/resolver/resolver.go
Expand Up @@ -499,24 +499,46 @@ func (r resolverQuery) finalizeResolve(result *ResolveResult) {

// Copy various fields from the nearest enclosing "tsconfig.json" file if present
if path == &result.PathPair.Primary && dirInfo.enclosingTSConfigJSON != nil {
result.JSXFactory = dirInfo.enclosingTSConfigJSON.JSXFactory
result.JSXFragment = dirInfo.enclosingTSConfigJSON.JSXFragmentFactory
result.UseDefineForClassFieldsTS = dirInfo.enclosingTSConfigJSON.UseDefineForClassFields
result.PreserveUnusedImportsTS = dirInfo.enclosingTSConfigJSON.PreserveImportsNotUsedAsValues
result.TSTarget = dirInfo.enclosingTSConfigJSON.TSTarget

if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("This import is under the effect of %q",
dirInfo.enclosingTSConfigJSON.AbsPath))
if result.JSXFactory != nil {
r.debugLogs.addNote(fmt.Sprintf("\"jsxFactory\" is %q due to %q",
strings.Join(result.JSXFactory, "."),
dirInfo.enclosingTSConfigJSON.AbsPath))
// Except don't do this if we're inside a "node_modules" directory. Package
// authors often publish their "tsconfig.json" files to npm because of
// npm's default-include publishing model and because these authors
// probably don't know about ".npmignore" files.
//
// People trying to use these packages with esbuild have historically
// complained that esbuild is respecting "tsconfig.json" in these cases.
// The assumption is that the package author published these files by
// accident.
//
// Ignoring "tsconfig.json" files inside "node_modules" directories breaks
// the use case of publishing TypeScript code and having it be transpiled
// for you, but that's the uncommon case and likely doesn't work with
// many other tools anyway. So now these files are ignored.
if helpers.IsInsideNodeModules(result.PathPair.Primary.Text) {
if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("Ignoring %q because %q is inside \"node_modules\"",
dirInfo.enclosingTSConfigJSON.AbsPath,
result.PathPair.Primary.Text))
}
if result.JSXFragment != nil {
r.debugLogs.addNote(fmt.Sprintf("\"jsxFragment\" is %q due to %q",
strings.Join(result.JSXFragment, "."),
} else {
result.JSXFactory = dirInfo.enclosingTSConfigJSON.JSXFactory
result.JSXFragment = dirInfo.enclosingTSConfigJSON.JSXFragmentFactory
result.UseDefineForClassFieldsTS = dirInfo.enclosingTSConfigJSON.UseDefineForClassFields
result.PreserveUnusedImportsTS = dirInfo.enclosingTSConfigJSON.PreserveImportsNotUsedAsValues
result.TSTarget = dirInfo.enclosingTSConfigJSON.TSTarget

if r.debugLogs != nil {
r.debugLogs.addNote(fmt.Sprintf("This import is under the effect of %q",
dirInfo.enclosingTSConfigJSON.AbsPath))
if result.JSXFactory != nil {
r.debugLogs.addNote(fmt.Sprintf("\"jsxFactory\" is %q due to %q",
strings.Join(result.JSXFactory, "."),
dirInfo.enclosingTSConfigJSON.AbsPath))
}
if result.JSXFragment != nil {
r.debugLogs.addNote(fmt.Sprintf("\"jsxFragment\" is %q due to %q",
strings.Join(result.JSXFragment, "."),
dirInfo.enclosingTSConfigJSON.AbsPath))
}
}
}
}
Expand Down

0 comments on commit d3ef487

Please sign in to comment.