Skip to content

Commit

Permalink
import-is-undefined is debug in node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 18, 2023
1 parent 220141e commit ddfdfb8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
52 changes: 52 additions & 0 deletions internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,16 @@ func TestUndefinedImportWarningCSS(t *testing.T) {
import * as empty_css from './empty.css'
import * as empty_global_css from './empty.global-css'
import * as empty_local_css from './empty.local-css'
import * as pkg_empty_js from 'pkg/empty.js'
import * as pkg_empty_esm_js from 'pkg/empty.esm.js'
import * as pkg_empty_json from 'pkg/empty.json'
import * as pkg_empty_css from 'pkg/empty.css'
import * as pkg_empty_global_css from 'pkg/empty.global-css'
import * as pkg_empty_local_css from 'pkg/empty.local-css'
import 'pkg'
console.log(
empty_js.foo,
empty_esm_js.foo,
Expand All @@ -1145,13 +1155,49 @@ func TestUndefinedImportWarningCSS(t *testing.T) {
empty_global_css.foo,
empty_local_css.foo,
)
console.log(
pkg_empty_js.foo,
pkg_empty_esm_js.foo,
pkg_empty_json.foo,
pkg_empty_css.foo,
pkg_empty_global_css.foo,
pkg_empty_local_css.foo,
)
`,

"/empty.js": ``,
"/empty.esm.js": `export {}`,
"/empty.json": `{}`,
"/empty.css": ``,
"/empty.global-css": ``,
"/empty.local-css": ``,

"/node_modules/pkg/empty.js": ``,
"/node_modules/pkg/empty.esm.js": `export {}`,
"/node_modules/pkg/empty.json": `{}`,
"/node_modules/pkg/empty.css": ``,
"/node_modules/pkg/empty.global-css": ``,
"/node_modules/pkg/empty.local-css": ``,

// Files inside of "node_modules" should not generate a warning
"/node_modules/pkg/index.js": `
import * as empty_js from './empty.js'
import * as empty_esm_js from './empty.esm.js'
import * as empty_json from './empty.json'
import * as empty_css from './empty.css'
import * as empty_global_css from './empty.global-css'
import * as empty_local_css from './empty.local-css'
console.log(
empty_js.foo,
empty_esm_js.foo,
empty_json.foo,
empty_css.foo,
empty_global_css.foo,
empty_local_css.foo,
)
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Expand All @@ -1171,6 +1217,12 @@ entry.js: WARNING: Import "foo" will always be undefined because there is no mat
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.css"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.global-css"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.local-css"
entry.js: WARNING: Import "foo" will always be undefined because the file "node_modules/pkg/empty.js" has no exports
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "node_modules/pkg/empty.esm.js"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "node_modules/pkg/empty.json"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "node_modules/pkg/empty.css"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "node_modules/pkg/empty.global-css"
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "node_modules/pkg/empty.local-css"
`,
})
}
33 changes: 32 additions & 1 deletion internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,36 @@ var require_empty = __commonJS({
}
});

// node_modules/pkg/empty.js
var require_empty2 = __commonJS({
"node_modules/pkg/empty.js"() {
}
});

// entry.js
var empty_js2 = __toESM(require_empty());
var pkg_empty_js = __toESM(require_empty2());

// node_modules/pkg/index.js
var empty_js = __toESM(require_empty2());
console.log(
void 0,
void 0,
void 0,
void 0,
void 0,
void 0
);

// entry.js
var empty_js = __toESM(require_empty());
console.log(
void 0,
void 0,
void 0,
void 0,
void 0,
void 0
);
console.log(
void 0,
void 0,
Expand All @@ -1265,3 +1293,6 @@ console.log(
/* empty.css */
/* empty.global-css */
/* empty.local-css */
/* node_modules/pkg/empty.css */
/* node_modules/pkg/empty.global-css */
/* node_modules/pkg/empty.local-css */
12 changes: 10 additions & 2 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,11 @@ loop:
if status == importCommonJSWithoutExports {
symbol := c.graph.Symbols.Get(tracker.importRef)
symbol.ImportItemStatus = ast.ImportItemMissing
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, logger.Warning,
kind := logger.Warning
if helpers.IsInsideNodeModules(trackerFile.InputFile.Source.KeyPath.Text) {
kind = logger.Debug
}
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, kind,
trackerFile.LineColumnTracker(),
js_lexer.RangeOfIdentifier(trackerFile.InputFile.Source, namedImport.AliasLoc),
fmt.Sprintf("Import %q will always be undefined because the file %q has no exports",
Expand Down Expand Up @@ -2510,7 +2514,11 @@ loop:
// time, so we emit a debug message and rewrite the value to the literal
// "undefined" instead of emitting an error.
symbol.ImportItemStatus = ast.ImportItemMissing
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, logger.Warning, trackerFile.LineColumnTracker(), r, fmt.Sprintf(
kind := logger.Warning
if helpers.IsInsideNodeModules(trackerFile.InputFile.Source.KeyPath.Text) {
kind = logger.Debug
}
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, kind, trackerFile.LineColumnTracker(), r, fmt.Sprintf(
"Import %q will always be undefined because there is no matching export in %q",
namedImport.Alias, c.graph.Files[nextTracker.sourceIndex].InputFile.Source.PrettyPath))
} else {
Expand Down

0 comments on commit ddfdfb8

Please sign in to comment.