From cfc336705993168968c0679382baeafebca9df73 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Tue, 13 Dec 2022 23:52:50 -0500 Subject: [PATCH] the previous change also fixes #1768 --- CHANGELOG.md | 4 +- internal/bundler/bundler_default_test.go | 34 ++++++ .../bundler/snapshots/snapshots_loader.txt | 109 ++++++++++++++++++ internal/js_parser/js_parser.go | 11 +- 4 files changed, 153 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f48531fc89c..17b4d9c2977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ * Leading and trailing `.` such as `0.` and `.0` * Numbers with a space after the `-` such as `- 1` -* Add external imports to metafile ([#905](https://github.com/evanw/esbuild/issues/905), [#1933](https://github.com/evanw/esbuild/issues/1933), [#1939](https://github.com/evanw/esbuild/issues/1939)) +* Add external imports to metafile ([#905](https://github.com/evanw/esbuild/issues/905), [#1768](https://github.com/evanw/esbuild/issues/1768), [#1933](https://github.com/evanw/esbuild/issues/1933), [#1939](https://github.com/evanw/esbuild/issues/1939)) External imports now appear in `imports` arrays in the metafile (which is present when bundling with `metafile: true`) next to normal imports, but additionally have `external: true` to set them apart. This applies both to files in the `inputs` section and the `outputs` section. Here's an example: @@ -89,6 +89,8 @@ } ``` + One additional useful consequence of this is that the `imports` array is now populated when bundling is disabled. So you can now use esbuild with bundling disabled to inspect a file's imports. + ## 0.16.5 * Make it easy to exclude all packages from a bundle ([#1958](https://github.com/evanw/esbuild/issues/1958), [#1975](https://github.com/evanw/esbuild/issues/1975), [#2164](https://github.com/evanw/esbuild/issues/2164), [#2246](https://github.com/evanw/esbuild/issues/2246), [#2542](https://github.com/evanw/esbuild/issues/2542)) diff --git a/internal/bundler/bundler_default_test.go b/internal/bundler/bundler_default_test.go index 1e14780b022..1d1ce4b7afe 100644 --- a/internal/bundler/bundler_default_test.go +++ b/internal/bundler/bundler_default_test.go @@ -7177,3 +7177,37 @@ func TestMetafileVariousCases(t *testing.T) { }, }) } + +func TestMetafileNoBundle(t *testing.T) { + loader_suite.expectBundled(t, bundled{ + files: map[string]string{ + "/project/entry.js": ` + import a from 'pkg' + import b from './file' + console.log( + a, + b, + require('pkg2'), + require('./file2'), + import('./dynamic'), + ) + export let exported + `, + "/project/entry.css": ` + @import "pkg"; + @import "./file"; + a { background: url(pkg2) } + a { background: url(./file2) } + `, + }, + entryPaths: []string{ + "/project/entry.js", + "/project/entry.css", + }, + options: config.Options{ + Mode: config.ModeConvertFormat, + AbsOutputDir: "/out", + NeedsMetafile: true, + }, + }) +} diff --git a/internal/bundler/snapshots/snapshots_loader.txt b/internal/bundler/snapshots/snapshots_loader.txt index e48312953e5..7b2af5e90ff 100644 --- a/internal/bundler/snapshots/snapshots_loader.txt +++ b/internal/bundler/snapshots/snapshots_loader.txt @@ -978,6 +978,115 @@ x.a, x?.a, x[y ? "a" : z], x?.[y ? "a" : z], x[y ? z : "a"], x?.[y ? z : "a"], x var { a: x } = y, { ["a"]: x } = y, { [(z, "a")]: x } = y; "a" in x, (y ? "a" : z) in x, (y ? z : "a") in x, y, "a" in x; +================================================================================ +TestMetafileNoBundle +---------- /out/entry.js ---------- +import a from "pkg"; +import b from "./file"; +console.log( + a, + b, + require("pkg2"), + require("./file2"), + import("./dynamic") +); +let exported; + +---------- /out/entry.css ---------- +@import "pkg"; +@import "./file"; +a { + background: url(pkg2); +} +a { + background: url(./file2); +} +---------- metafile.json ---------- +{ + "inputs": { + "project/entry.js": { + "bytes": 191, + "imports": [] + }, + "project/entry.css": { + "bytes": 112, + "imports": [] + } + }, + "outputs": { + "out/entry.js": { + "imports": [ + { + "path": "pkg", + "kind": "import-statement", + "external": true + }, + { + "path": "./file", + "kind": "import-statement", + "external": true + }, + { + "path": "pkg2", + "kind": "require-call", + "external": true + }, + { + "path": "./file2", + "kind": "require-call", + "external": true + }, + { + "path": "./dynamic", + "kind": "dynamic-import", + "external": true + } + ], + "exports": [ + "exported" + ], + "entryPoint": "project/entry.js", + "inputs": { + "project/entry.js": { + "bytesInOutput": 148 + } + }, + "bytes": 148 + }, + "out/entry.css": { + "imports": [ + { + "path": "pkg", + "kind": "import-rule", + "external": true + }, + { + "path": "./file", + "kind": "import-rule", + "external": true + }, + { + "path": "pkg2", + "kind": "url-token", + "external": true + }, + { + "path": "./file2", + "kind": "url-token", + "external": true + } + ], + "entryPoint": "project/entry.css", + "inputs": { + "project/entry.css": { + "bytesInOutput": 65 + } + }, + "bytes": 98 + } + } +} + ================================================================================ TestMetafileVariousCases ---------- /out/file-NVISQQTV.file ---------- diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index e697b466129..fee131cdfc6 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -13900,7 +13900,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO // error from the unbundled require() call failing. omitWarnings := p.fnOrArrowDataVisit.tryBodyCount != 0 - if p.options.mode == config.ModeBundle { + if p.options.mode != config.ModePassThrough { // There must be one argument if len(e.Args) == 1 { p.ignoreUsage(p.requireRef) @@ -13922,6 +13922,12 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO } p.importRecordsForCurrentPart = append(p.importRecordsForCurrentPart, importRecordIndex) + // Currently "require" is not converted into "import" for ESM + if p.options.mode != config.ModeBundle && p.options.outputFormat == config.FormatESModule && !omitWarnings { + r := js_lexer.RangeOfIdentifier(p.source, e.Target.Loc) + p.log.AddID(logger.MsgID_JS_UnsupportedRequireCall, logger.Warning, &p.tracker, r, "Converting \"require\" to \"esm\" is currently not supported") + } + // Create a new expression to represent the operation return js_ast.Expr{Loc: expr.Loc, Data: &js_ast.ERequireString{ ImportRecordIndex: importRecordIndex, @@ -13951,9 +13957,6 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO Target: p.valueToSubstituteForRequire(e.Target.Loc), Args: e.Args, }}, exprOut{} - } else if p.options.outputFormat == config.FormatESModule && !omitWarnings { - r := js_lexer.RangeOfIdentifier(p.source, e.Target.Loc) - p.log.AddID(logger.MsgID_JS_UnsupportedRequireCall, logger.Warning, &p.tracker, r, "Converting \"require\" to \"esm\" is currently not supported") } } }