Skip to content

Commit

Permalink
don't treat unknown extensions as text files
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 17, 2020
1 parent 236194e commit bab150d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 14 deletions.
17 changes: 12 additions & 5 deletions src/esbuild/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ type parseResult struct {
ok bool
}

func parseFile(log logging.Log, source logging.Source, options parser.ParseOptions, results chan parseResult) {
func parseFile(
log logging.Log, source logging.Source, importSource logging.Source,
pathRange ast.Range, options parser.ParseOptions, results chan parseResult,
) {
path := source.AbsolutePath

switch {
Expand All @@ -61,10 +64,14 @@ func parseFile(log logging.Log, source logging.Source, options parser.ParseOptio
ast := parser.ModuleExportsAST(source, expr)
results <- parseResult{source.Index, ast, ok}

default:
case strings.HasSuffix(path, ".txt"):
expr := ast.Expr{ast.Loc{0}, &ast.EString{lexer.StringToUTF16(source.Contents)}}
ast := parser.ModuleExportsAST(source, expr)
results <- parseResult{source.Index, ast, true}

default:
log.AddRangeError(importSource, pathRange, fmt.Sprintf("File extension not supported: %s", path))
results <- parseResult{}
}
}

Expand All @@ -75,7 +82,7 @@ func ScanBundle(log logging.Log, fs fs.FS, res resolver.Resolver, entryPaths []s
results := make(chan parseResult)
remaining := 0

maybeParseFile := func(path string, source logging.Source, pathRange ast.Range, isDisabled bool) (uint32, bool) {
maybeParseFile := func(path string, importSource logging.Source, pathRange ast.Range, isDisabled bool) (uint32, bool) {
sourceIndex, ok := visited[path]
if !ok {
sourceIndex = uint32(len(sources))
Expand All @@ -86,7 +93,7 @@ func ScanBundle(log logging.Log, fs fs.FS, res resolver.Resolver, entryPaths []s
if !isDisabled {
contents, ok = res.Read(path)
if !ok {
log.AddRangeError(source, pathRange, fmt.Sprintf("Could not read from file: %s", path))
log.AddRangeError(importSource, pathRange, fmt.Sprintf("Could not read from file: %s", path))
return 0, false
}
}
Expand All @@ -100,7 +107,7 @@ func ScanBundle(log logging.Log, fs fs.FS, res resolver.Resolver, entryPaths []s
sources = append(sources, source)
files = append(files, file{})
remaining++
go parseFile(log, source, options, results)
go parseFile(log, source, importSource, pathRange, options, results)
}
return sourceIndex, true
}
Expand Down
122 changes: 113 additions & 9 deletions src/esbuild/bundler/bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ func assertLog(t *testing.T, msgs []logging.Msg, expected string) {
}

type bundled struct {
files map[string]string
entryPaths []string
expected map[string]string
expectedLog string
parseOptions parser.ParseOptions
bundleOptions BundleOptions
files map[string]string
entryPaths []string
expected map[string]string
expectedScanLog string
expectedCompileLog string
parseOptions parser.ParseOptions
bundleOptions BundleOptions
}

func expectBundled(t *testing.T, args bundled) {
Expand All @@ -39,15 +40,21 @@ func expectBundled(t *testing.T, args bundled) {

log, join := logging.NewDeferLog()
bundle := ScanBundle(log, fs, resolver, args.entryPaths, args.parseOptions)
assertLog(t, join(), "")
msgs := join()
assertLog(t, msgs, args.expectedScanLog)

// Stop now if there were any errors during the scan
if len(msgs) > 0 {
return
}

log, join = logging.NewDeferLog()
args.bundleOptions.omitLoaderForTests = true
if args.bundleOptions.AbsOutputFile != "" {
args.bundleOptions.AbsOutputDir = path.Dir(args.bundleOptions.AbsOutputFile)
}
results := bundle.Compile(log, args.bundleOptions)
assertLog(t, join(), args.expectedLog)
assertLog(t, join(), args.expectedCompileLog)

assertEqual(t, len(results), len(args.expected))
for _, result := range results {
Expand Down Expand Up @@ -1094,7 +1101,7 @@ func TestPackageImportMissingES6(t *testing.T) {
Bundle: true,
AbsOutputFile: "/out.js",
},
expectedLog: `/entry.js: error: No matching export for import "default"
expectedCompileLog: `/entry.js: error: No matching export for import "default"
/entry.js: error: No matching export for import "y"
`,
expected: map[string]string{
Expand Down Expand Up @@ -1186,6 +1193,103 @@ func TestDotImport(t *testing.T) {
})
}

func TestRequireJson(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test.json'))
`,
"/test.json": `
{
"a": true,
"b": 123,
"c": [null]
}
`,
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
Bundle: true,
AbsOutputFile: "/out.js",
},
expected: map[string]string{
"/out.js": `loader({
1(require, exports, module) {
// /test.json
module.exports = {
a: true,
b: 123,
c: [null]
};
},
0(require) {
// /entry.js
console.log(require(1 /* ./test.json */));
}
}, 0);
`,
},
})
}

func TestRequireTxt(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test.txt'))
`,
"/test.txt": `This is a test.`,
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
Bundle: true,
AbsOutputFile: "/out.js",
},
expected: map[string]string{
"/out.js": `loader({
1(require, exports, module) {
// /test.txt
module.exports = "This is a test.";
},
0(require) {
// /entry.js
console.log(require(1 /* ./test.txt */));
}
}, 0);
`,
},
})
}

func TestRequireBadExtension(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
console.log(require('./test'))
`,
"/test": `This is a test.`,
},
entryPaths: []string{"/entry.js"},
parseOptions: parser.ParseOptions{
IsBundling: true,
},
bundleOptions: BundleOptions{
Bundle: true,
AbsOutputFile: "/out.js",
},
expectedScanLog: `/entry.js: error: File extension not supported: /test
`,
})
}

func TestSourceMap(t *testing.T) {
expectBundled(t, bundled{
files: map[string]string{
Expand Down

0 comments on commit bab150d

Please sign in to comment.