From 7065824ab74141b733b1c77fb08b84e2432db163 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Mon, 7 Dec 2020 21:01:38 -0800 Subject: [PATCH] set stdin importer in on-resolve plugins (#527) --- CHANGELOG.md | 4 ++ internal/bundler/bundler.go | 19 ++++++--- scripts/plugin-tests.js | 83 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8dd5cded2..be764606434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ Previously you could only use the transform API in the browser, not the build API. You can now use the build API in the browser too. There is currently no in-browser file system so the build API will not do anything by default. Using this API requires you to use plugins to provide your own file system. Instructions for running esbuild in the browser can be found here: https://esbuild.github.io/api/#running-in-the-browser. +* Set the importer to `sourcefile` in on-resolve plugins for stdin + + When the stdin feature is used with on-resolve plugins, the importer for any import paths in stdin is currently always set to ``. The `sourcefile` option provides a way to set the file name of stdin but it wasn't carried through to on-resolve plugins due to an oversight. This release changes this behavior so now `sourcefile` is used instead of `` if present. In addition, if the stdin resolve directory is also specified the importer will be placed in the `file` namespace similar to a normal file. + ## 0.8.20 * Fix an edge case with class body initialization diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index 2d5ca4759ef..451500e5cb1 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -149,9 +149,6 @@ func parseFile(args parseArgs) { if stdin := args.options.Stdin; stdin != nil { // Special-case stdin source.Contents = stdin.Contents - if stdin.SourceFile != "" { - source.PrettyPath = stdin.SourceFile - } loader = stdin.Loader if loader == config.LoaderNone { loader = config.LoaderJS @@ -964,9 +961,19 @@ func (s *scanner) addEntryPoints(entryPoints []string) []uint32 { entryPointIndices := make([]uint32, 0, len(entryPoints)+1) // Treat stdin as an extra entry point - if s.options.Stdin != nil { - resolveResult := resolver.ResolveResult{PathPair: resolver.PathPair{Primary: logger.Path{Text: ""}}} - sourceIndex := s.maybeParseFile(resolveResult, "", nil, logger.Range{}, inputKindStdin, nil) + if stdin := s.options.Stdin; stdin != nil { + stdinPath := logger.Path{Text: ""} + if stdin.SourceFile != "" { + if stdin.AbsResolveDir == "" { + stdinPath = logger.Path{Text: stdin.SourceFile} + } else if s.fs.IsAbs(stdin.SourceFile) { + stdinPath = logger.Path{Text: stdin.SourceFile, Namespace: "file"} + } else { + stdinPath = logger.Path{Text: s.fs.Join(stdin.AbsResolveDir, stdin.SourceFile), Namespace: "file"} + } + } + resolveResult := resolver.ResolveResult{PathPair: resolver.PathPair{Primary: stdinPath}} + sourceIndex := s.maybeParseFile(resolveResult, s.res.PrettyPath(stdinPath), nil, logger.Range{}, inputKindStdin, nil) entryPointIndices = append(entryPointIndices, sourceIndex) } diff --git a/scripts/plugin-tests.js b/scripts/plugin-tests.js index 9407c216242..a8f6319d976 100644 --- a/scripts/plugin-tests.js +++ b/scripts/plugin-tests.js @@ -580,6 +580,89 @@ let pluginTests = { assert.strictEqual(result.outputFiles[1].text, `// virtual-ns:input 2\nconsole.log("input 2");\n`) }, + async stdinImporter({ esbuild, testDir }) { + const output = path.join(testDir, 'out.js') + await esbuild.build({ + stdin: { + contents: `import x from "plugin"; export default x`, + sourcefile: 'stdin-sourcefile', + }, + bundle: true, outfile: output, format: 'cjs', plugins: [{ + name: 'name', + setup(build) { + build.onResolve({ filter: /^plugin$/ }, args => { + assert.strictEqual(args.namespace, '') + assert.strictEqual(args.importer, 'stdin-sourcefile') + assert.strictEqual(args.resolveDir, '') + assert.strictEqual(args.path, 'plugin') + return { path: args.path, namespace: 'worked' } + }) + build.onLoad({ filter: /.*/, namespace: 'worked' }, () => { + return { contents: `export default 123` } + }) + }, + }], + }) + const result = require(output) + assert.strictEqual(result.default, 123) + }, + + async stdinImporterResolveDir({ esbuild, testDir }) { + const output = path.join(testDir, 'out.js') + await esbuild.build({ + stdin: { + contents: `import x from "plugin"; export default x`, + sourcefile: 'stdin-sourcefile', + resolveDir: testDir, + }, + bundle: true, outfile: output, format: 'cjs', plugins: [{ + name: 'name', + setup(build) { + build.onResolve({ filter: /^plugin$/ }, args => { + assert.strictEqual(args.namespace, 'file') + assert.strictEqual(args.importer, path.join(testDir, 'stdin-sourcefile')) + assert.strictEqual(args.resolveDir, testDir) + assert.strictEqual(args.path, 'plugin') + return { path: args.path, namespace: 'worked' } + }) + build.onLoad({ filter: /.*/, namespace: 'worked' }, () => { + return { contents: `export default 123` } + }) + }, + }], + }) + const result = require(output) + assert.strictEqual(result.default, 123) + }, + + async stdinAbsoluteImporterResolveDir({ esbuild, testDir }) { + const output = path.join(testDir, 'out.js') + await esbuild.build({ + stdin: { + contents: `import x from "plugin"; export default x`, + sourcefile: path.join(testDir, 'stdin-sourcefile'), + resolveDir: testDir, + }, + bundle: true, outfile: output, format: 'cjs', plugins: [{ + name: 'name', + setup(build) { + build.onResolve({ filter: /^plugin$/ }, args => { + assert.strictEqual(args.namespace, 'file') + assert.strictEqual(args.importer, path.join(testDir, 'stdin-sourcefile')) + assert.strictEqual(args.resolveDir, testDir) + assert.strictEqual(args.path, 'plugin') + return { path: args.path, namespace: 'worked' } + }) + build.onLoad({ filter: /.*/, namespace: 'worked' }, () => { + return { contents: `export default 123` } + }) + }, + }], + }) + const result = require(output) + assert.strictEqual(result.default, 123) + }, + async stdinRelative({ esbuild, testDir }) { const output = path.join(testDir, 'out.js') await esbuild.build({