Skip to content

Commit 220141e

Browse files
committed
import-is-undefined now defaults to warning
1 parent af0fe32 commit 220141e

File tree

5 files changed

+115
-22
lines changed

5 files changed

+115
-22
lines changed

internal/bundler_tests/bundler_css_test.go

+54-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ func TestCSSFromJSMissingStarImport(t *testing.T) {
161161
Mode: config.ModeBundle,
162162
AbsOutputDir: "/out",
163163
},
164-
debugLogs: true,
165-
expectedCompileLog: `entry.js: DEBUG: Import "missing" will always be undefined because there is no matching export in "a.css"
164+
expectedCompileLog: `entry.js: WARNING: Import "missing" will always be undefined because there is no matching export in "a.css"
166165
`,
167166
})
168167
}
@@ -194,6 +193,9 @@ func TestImportGlobalCSSFromJS(t *testing.T) {
194193
Mode: config.ModeBundle,
195194
AbsOutputDir: "/out",
196195
},
196+
expectedCompileLog: `a.js: WARNING: Import "a" will always be undefined because there is no matching export in "a.css"
197+
b.js: WARNING: Import "b" will always be undefined because there is no matching export in "b.css"
198+
`,
197199
})
198200
}
199201

@@ -1122,3 +1124,53 @@ func TestDeduplicateRules(t *testing.T) {
11221124
},
11231125
})
11241126
}
1127+
1128+
// This test makes sure JS files that import local CSS names using the
1129+
// wrong name (e.g. a typo) get a warning so that the problem is noticed.
1130+
func TestUndefinedImportWarningCSS(t *testing.T) {
1131+
css_suite.expectBundled(t, bundled{
1132+
files: map[string]string{
1133+
"/entry.js": `
1134+
import * as empty_js from './empty.js'
1135+
import * as empty_esm_js from './empty.esm.js'
1136+
import * as empty_json from './empty.json'
1137+
import * as empty_css from './empty.css'
1138+
import * as empty_global_css from './empty.global-css'
1139+
import * as empty_local_css from './empty.local-css'
1140+
console.log(
1141+
empty_js.foo,
1142+
empty_esm_js.foo,
1143+
empty_json.foo,
1144+
empty_css.foo,
1145+
empty_global_css.foo,
1146+
empty_local_css.foo,
1147+
)
1148+
`,
1149+
"/empty.js": ``,
1150+
"/empty.esm.js": `export {}`,
1151+
"/empty.json": `{}`,
1152+
"/empty.css": ``,
1153+
"/empty.global-css": ``,
1154+
"/empty.local-css": ``,
1155+
},
1156+
entryPaths: []string{"/entry.js"},
1157+
options: config.Options{
1158+
Mode: config.ModeBundle,
1159+
AbsOutputDir: "/out",
1160+
ExtensionToLoader: map[string]config.Loader{
1161+
".js": config.LoaderJS,
1162+
".json": config.LoaderJSON,
1163+
".css": config.LoaderCSS,
1164+
".global-css": config.LoaderGlobalCSS,
1165+
".local-css": config.LoaderLocalCSS,
1166+
},
1167+
},
1168+
expectedCompileLog: `entry.js: WARNING: Import "foo" will always be undefined because the file "empty.js" has no exports
1169+
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.esm.js"
1170+
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.json"
1171+
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.css"
1172+
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.global-css"
1173+
entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "empty.local-css"
1174+
`,
1175+
})
1176+
}

internal/bundler_tests/bundler_importstar_test.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,7 @@ func TestNamespaceImportMissingES6(t *testing.T) {
10651065
Mode: config.ModeBundle,
10661066
AbsOutputFile: "/out.js",
10671067
},
1068-
debugLogs: true,
1069-
expectedCompileLog: `entry.js: DEBUG: Import "foo" will always be undefined because there is no matching export in "foo.js"
1068+
expectedCompileLog: `entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "foo.js"
10701069
`,
10711070
})
10721071
}
@@ -1128,8 +1127,7 @@ func TestNamespaceImportUnusedMissingES6(t *testing.T) {
11281127
Mode: config.ModeBundle,
11291128
AbsOutputFile: "/out.js",
11301129
},
1131-
debugLogs: true,
1132-
expectedCompileLog: `entry.js: DEBUG: Import "foo" will always be undefined because there is no matching export in "foo.js"
1130+
expectedCompileLog: `entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "foo.js"
11331131
`,
11341132
})
11351133
}
@@ -1285,8 +1283,7 @@ func TestNamespaceImportReExportStarMissingES6(t *testing.T) {
12851283
Mode: config.ModeBundle,
12861284
AbsOutputFile: "/out.js",
12871285
},
1288-
debugLogs: true,
1289-
expectedCompileLog: `entry.js: DEBUG: Import "foo" will always be undefined because there is no matching export in "foo.js"
1286+
expectedCompileLog: `entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "foo.js"
12901287
`,
12911288
})
12921289
}
@@ -1310,8 +1307,7 @@ func TestNamespaceImportReExportStarUnusedMissingES6(t *testing.T) {
13101307
Mode: config.ModeBundle,
13111308
AbsOutputFile: "/out.js",
13121309
},
1313-
debugLogs: true,
1314-
expectedCompileLog: `entry.js: DEBUG: Import "foo" will always be undefined because there is no matching export in "foo.js"
1310+
expectedCompileLog: `entry.js: WARNING: Import "foo" will always be undefined because there is no matching export in "foo.js"
13151311
`,
13161312
})
13171313
}
@@ -1665,9 +1661,8 @@ func TestImportDefaultNamespaceComboIssue446(t *testing.T) {
16651661
}},
16661662
},
16671663
},
1668-
debugLogs: true,
1669-
expectedCompileLog: `internal-def.js: DEBUG: Import "def" will always be undefined because there is no matching export in "internal.js"
1670-
internal-ns-def.js: DEBUG: Import "def" will always be undefined because there is no matching export in "internal.js"
1664+
expectedCompileLog: `internal-def.js: WARNING: Import "def" will always be undefined because there is no matching export in "internal.js"
1665+
internal-ns-def.js: WARNING: Import "def" will always be undefined because there is no matching export in "internal.js"
16711666
`,
16721667
})
16731668
}
@@ -1693,14 +1688,13 @@ func TestImportDefaultNamespaceComboNoDefault(t *testing.T) {
16931688
Mode: config.ModeBundle,
16941689
AbsOutputDir: "/out",
16951690
},
1696-
debugLogs: true,
16971691
expectedCompileLog: `entry-default-ns-prop.js: ERROR: No matching export in "foo.js" for import "default"
1698-
entry-default-ns-prop.js: DEBUG: Import "default" will always be undefined because there is no matching export in "foo.js"
1692+
entry-default-ns-prop.js: WARNING: Import "default" will always be undefined because there is no matching export in "foo.js"
16991693
entry-default-ns.js: ERROR: No matching export in "foo.js" for import "default"
17001694
entry-default-prop.js: ERROR: No matching export in "foo.js" for import "default"
1701-
entry-default-prop.js: DEBUG: Import "default" will always be undefined because there is no matching export in "foo.js"
1695+
entry-default-prop.js: WARNING: Import "default" will always be undefined because there is no matching export in "foo.js"
17021696
entry-default.js: ERROR: No matching export in "foo.js" for import "default"
1703-
entry-prop.js: DEBUG: Import "default" will always be undefined because there is no matching export in "foo.js"
1697+
entry-prop.js: WARNING: Import "default" will always be undefined because there is no matching export in "foo.js"
17041698
`,
17051699
})
17061700
}
@@ -1746,8 +1740,7 @@ func TestImportNamespaceUndefinedPropertyEmptyFile(t *testing.T) {
17461740
Mode: config.ModeBundle,
17471741
AbsOutputDir: "/out",
17481742
},
1749-
debugLogs: true,
1750-
expectedCompileLog: `entry-default.js: DEBUG: Import "default" will always be undefined because there is no matching export in "empty.mjs"
1743+
expectedCompileLog: `entry-default.js: WARNING: Import "default" will always be undefined because there is no matching export in "empty.mjs"
17511744
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "empty.js" has no exports
17521745
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "empty.mjs" has no exports
17531746
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "empty.cjs" has no exports
@@ -1797,8 +1790,7 @@ func TestImportNamespaceUndefinedPropertySideEffectFreeFile(t *testing.T) {
17971790
Mode: config.ModeBundle,
17981791
AbsOutputDir: "/out",
17991792
},
1800-
debugLogs: true,
1801-
expectedCompileLog: `entry-default.js: DEBUG: Import "default" will always be undefined because there is no matching export in "foo/no-side-effects.mjs"
1793+
expectedCompileLog: `entry-default.js: WARNING: Import "default" will always be undefined because there is no matching export in "foo/no-side-effects.mjs"
18021794
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "foo/no-side-effects.js" has no exports
18031795
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "foo/no-side-effects.mjs" has no exports
18041796
entry-nope.js: WARNING: Import "nope" will always be undefined because the file "foo/no-side-effects.cjs" has no exports

internal/bundler_tests/snapshots/snapshots_css.txt

+25
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,28 @@ TestTextImportURLInCSSText
12401240
a {
12411241
background: url(data:text/plain;base64,VGhpcyBpcyBzb21lIHRleHQu);
12421242
}
1243+
1244+
================================================================================
1245+
TestUndefinedImportWarningCSS
1246+
---------- /out/entry.js ----------
1247+
// empty.js
1248+
var require_empty = __commonJS({
1249+
"empty.js"() {
1250+
}
1251+
});
1252+
1253+
// entry.js
1254+
var empty_js = __toESM(require_empty());
1255+
console.log(
1256+
void 0,
1257+
void 0,
1258+
void 0,
1259+
void 0,
1260+
void 0,
1261+
void 0
1262+
);
1263+
1264+
---------- /out/entry.css ----------
1265+
/* empty.css */
1266+
/* empty.global-css */
1267+
/* empty.local-css */

internal/linker/linker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ loop:
25102510
// time, so we emit a debug message and rewrite the value to the literal
25112511
// "undefined" instead of emitting an error.
25122512
symbol.ImportItemStatus = ast.ImportItemMissing
2513-
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, logger.Debug, trackerFile.LineColumnTracker(), r, fmt.Sprintf(
2513+
c.log.AddID(logger.MsgID_Bundler_ImportIsUndefined, logger.Warning, trackerFile.LineColumnTracker(), r, fmt.Sprintf(
25142514
"Import %q will always be undefined because there is no matching export in %q",
25152515
namedImport.Alias, c.graph.Files[nextTracker.sourceIndex].InputFile.Source.PrettyPath))
25162516
} else {

scripts/end-to-end-tests.js

+24
Original file line numberDiff line numberDiff line change
@@ -2229,13 +2229,29 @@ tests.push(
22292229
if (ns.default !== void 0) throw 'fail'
22302230
`,
22312231
'node_modules/pkg/index.mjs': ``,
2232+
}, {
2233+
expectedStderr: `▲ [WARNING] Import "default" will always be undefined because there is no matching export in "node_modules/pkg/index.mjs" [import-is-undefined]
2234+
2235+
in.js:3:13:
2236+
3 │ if (ns.default !== void 0) throw 'fail'
2237+
╵ ~~~~~~~
2238+
2239+
`,
22322240
}),
22332241
test(['in.js', '--outfile=node.js', '--bundle'], {
22342242
'in.js': `
22352243
import * as ns from 'pkg/index.mts'
22362244
if (ns.default !== void 0) throw 'fail'
22372245
`,
22382246
'node_modules/pkg/index.mts': ``,
2247+
}, {
2248+
expectedStderr: `▲ [WARNING] Import "default" will always be undefined because there is no matching export in "node_modules/pkg/index.mts" [import-is-undefined]
2249+
2250+
in.js:3:13:
2251+
3 │ if (ns.default !== void 0) throw 'fail'
2252+
╵ ~~~~~~~
2253+
2254+
`,
22392255
}),
22402256
test(['in.js', '--outfile=node.js', '--bundle'], {
22412257
'in.js': `
@@ -2256,6 +2272,14 @@ tests.push(
22562272
"type": "module"
22572273
}`,
22582274
'node_modules/pkg/index.js': ``,
2275+
}, {
2276+
expectedStderr: `▲ [WARNING] Import "default" will always be undefined because there is no matching export in "node_modules/pkg/index.js" [import-is-undefined]
2277+
2278+
in.js:3:13:
2279+
3 │ if (ns.default !== void 0) throw 'fail'
2280+
╵ ~~~~~~~
2281+
2282+
`,
22592283
}),
22602284
test(['in.js', '--outfile=node.js', '--bundle', '--external:pkg'], {
22612285
'in.js': `

0 commit comments

Comments
 (0)