Skip to content

Commit 1ec2671

Browse files
committed
new message when "type" should be "types" (#395)
1 parent 988aab7 commit 1ec2671

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

internal/bundler/bundler_packagejson_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -2348,3 +2348,26 @@ NOTE: You can mark the path "foo" as external to exclude it from the bundle, whi
23482348
`,
23492349
})
23502350
}
2351+
2352+
func TestPackageJsonTypeShouldBeTypes(t *testing.T) {
2353+
packagejson_suite.expectBundled(t, bundled{
2354+
files: map[string]string{
2355+
"/Users/user/project/src/index.js": ``,
2356+
"/Users/user/project/package.json": `
2357+
{
2358+
"main": "./src/index.js",
2359+
"type": "./src/index.d.ts"
2360+
}
2361+
`,
2362+
},
2363+
entryPaths: []string{"/Users/user/project/src/index.js"},
2364+
options: config.Options{
2365+
Mode: config.ModeBundle,
2366+
AbsOutputFile: "/Users/user/project/out.js",
2367+
MainFields: []string{},
2368+
},
2369+
expectedScanLog: `Users/user/project/package.json: WARNING: "./src/index.d.ts" is not a valid value for the "type" field
2370+
Users/user/project/package.json: NOTE: TypeScript type declarations use the "types" field, not the "type" field:
2371+
`,
2372+
})
2373+
}

internal/bundler/snapshots/snapshots_packagejson.txt

+4
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,7 @@ var require_main = __commonJS({
697697
// Users/user/project/src/entry.js
698698
var import_demo_pkg = __toESM(require_main());
699699
console.log((0, import_demo_pkg.default)());
700+
701+
================================================================================
702+
TestPackageJsonTypeShouldBeTypes
703+
---------- /Users/user/project/out.js ----------

internal/resolver/package_json.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (r resolverQuery) parsePackageJSON(inputPath string) *packageJSON {
260260
}
261261

262262
// Read the "type" field
263-
if typeJSON, _, ok := getProperty(json, "type"); ok {
263+
if typeJSON, typeKeyLoc, ok := getProperty(json, "type"); ok {
264264
if typeValue, ok := getString(typeJSON); ok {
265265
switch typeValue {
266266
case "commonjs":
@@ -276,10 +276,24 @@ func (r resolverQuery) parsePackageJSON(inputPath string) *packageJSON {
276276
Range: jsonSource.RangeOfString(typeJSON.Loc),
277277
}
278278
default:
279-
r.log.AddWithNotes(logger.Warning, &tracker, jsonSource.RangeOfString(typeJSON.Loc),
279+
notes := []logger.MsgData{{Text: "The \"type\" field must be set to either \"commonjs\" or \"module\"."}}
280+
kind := logger.Warning
281+
282+
// If someone does something like "type": "./index.d.ts" then they
283+
// likely meant "types" instead of "type". Customize the message
284+
// for this and hide it if it's inside a published npm package.
285+
if strings.HasSuffix(typeValue, ".d.ts") {
286+
notes[0] = tracker.MsgData(jsonSource.RangeOfString(typeKeyLoc),
287+
"TypeScript type declarations use the \"types\" field, not the \"type\" field:")
288+
notes[0].Location.Suggestion = "\"types\""
289+
if helpers.IsInsideNodeModules(jsonSource.KeyPath.Text) {
290+
kind = logger.Debug
291+
}
292+
}
293+
294+
r.log.AddWithNotes(kind, &tracker, jsonSource.RangeOfString(typeJSON.Loc),
280295
fmt.Sprintf("%q is not a valid value for the \"type\" field", typeValue),
281-
[]logger.MsgData{{Text: "The \"type\" field must be set to either \"commonjs\" or \"module\"."}},
282-
)
296+
notes)
283297
}
284298
} else {
285299
r.log.Add(logger.Warning, &tracker, logger.Range{Loc: typeJSON.Loc},

0 commit comments

Comments
 (0)