Skip to content

Commit

Permalink
the previous change also fixes #1768
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Dec 14, 2022
1 parent f2f78ef commit cfc3367
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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:

Expand Down Expand Up @@ -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))
Expand Down
34 changes: 34 additions & 0 deletions internal/bundler/bundler_default_test.go
Expand Up @@ -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,
},
})
}
109 changes: 109 additions & 0 deletions internal/bundler/snapshots/snapshots_loader.txt
Expand Up @@ -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 ----------
Expand Down
11 changes: 7 additions & 4 deletions internal/js_parser/js_parser.go
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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")
}
}
}
Expand Down

0 comments on commit cfc3367

Please sign in to comment.