Skip to content

Commit

Permalink
fix #109: allow "--format" without "--bundle"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 6, 2020
1 parent 48991b3 commit b23bee3
Show file tree
Hide file tree
Showing 24 changed files with 951 additions and 422 deletions.
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@

## Unreleased

* Allow `--format` when bundling is disabled ([#109](https://github.com/evanw/esbuild/issues/109))

This change means esbuild can be used to convert ES6 import and export syntax to CommonJS syntax. The following code:

```js
import foo from 'foo'
export const bar = foo
```

will be transformed into the following code with `--format=cjs` (the code for `__export` and `__toModule` was omitted for brevity):

```js
__export(exports, {
bar: () => bar
});
const foo = __toModule(require("foo"));
const bar = foo.default;
```

This also applies to non-JavaScript loaders too. The following JSON:

```json
{"foo": true, "bar": false}
```

is normally converted to the following code with `--loader=json`:

```js
module.exports = {foo: true, bar: false};
```

but will be transformed into the following code instead with `--loader=json --format=esm`:

```js
var foo = true;
var bar = false;
var stdin_default = {foo, bar};
export {
bar,
stdin_default as default,
foo
};
```

Note that converting CommonJS `require()` calls to ES6 imports is not currently supported. Code containing a reference to `require` in these situations will generate a warning.

* Remove trailing `()` from `new` when minifying

Now `new Foo()` will be printed as `new Foo` when minifying (as long as it's safe to do so), resulting in slightly shorter minified code.
Expand Down
8 changes: 4 additions & 4 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func parseFile(args parseArgs) {

// Run the resolver on the parse thread so it's not run on the main thread.
// That way the main thread isn't blocked if the resolver takes a while.
if args.options.IsBundling {
if args.options.Mode == config.ModeBundle {
result.resolveResults = make([]*resolver.ResolveResult, len(result.file.ast.ImportRecords))

if len(result.file.ast.ImportRecords) > 0 {
Expand Down Expand Up @@ -536,7 +536,7 @@ func ScanBundle(log logging.Log, fs fs.FS, res resolver.Resolver, entryPaths []s
}

// Don't try to resolve paths if we're not bundling
if options.IsBundling {
if options.Mode == config.ModeBundle {
for _, part := range result.file.ast.Parts {
for _, importRecordIndex := range part.ImportRecordIndices {
record := &result.file.ast.ImportRecords[importRecordIndex]
Expand Down Expand Up @@ -647,7 +647,7 @@ func (b *Bundle) Compile(log logging.Log, options config.Options) []OutputFile {
}

// The format can't be "preserve" while bundling
if options.IsBundling && options.OutputFormat == config.FormatPreserve {
if options.Mode == config.ModeBundle && options.OutputFormat == config.FormatPreserve {
options.OutputFormat = config.FormatESModule
}

Expand Down Expand Up @@ -928,7 +928,7 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logging

// Always do tree shaking for the runtime because we never want to
// include unnecessary runtime code
IsBundling: true,
Mode: config.ModeBundle,
})
if log.HasErrors() {
panic("Internal error: failed to parse runtime")
Expand Down
52 changes: 26 additions & 26 deletions internal/bundler/bundler_dce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPackageJsonSideEffectsFalseKeepNamedImportES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -54,7 +54,7 @@ func TestPackageJsonSideEffectsFalseKeepNamedImportCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -79,7 +79,7 @@ func TestPackageJsonSideEffectsFalseKeepStarImportES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -104,7 +104,7 @@ func TestPackageJsonSideEffectsFalseKeepStarImportCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -129,7 +129,7 @@ func TestPackageJsonSideEffectsTrueKeepES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -154,7 +154,7 @@ func TestPackageJsonSideEffectsTrueKeepCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -180,7 +180,7 @@ func TestPackageJsonSideEffectsFalseKeepBareImportAndRequireES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -206,7 +206,7 @@ func TestPackageJsonSideEffectsFalseKeepBareImportAndRequireCommonJS(t *testing.
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -231,7 +231,7 @@ func TestPackageJsonSideEffectsFalseRemoveBareImportES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -256,7 +256,7 @@ func TestPackageJsonSideEffectsFalseRemoveBareImportCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -281,7 +281,7 @@ func TestPackageJsonSideEffectsFalseRemoveNamedImportES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -306,7 +306,7 @@ func TestPackageJsonSideEffectsFalseRemoveNamedImportCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -331,7 +331,7 @@ func TestPackageJsonSideEffectsFalseRemoveStarImportES6(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -356,7 +356,7 @@ func TestPackageJsonSideEffectsFalseRemoveStarImportCommonJS(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -381,7 +381,7 @@ func TestPackageJsonSideEffectsArrayRemove(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -406,7 +406,7 @@ func TestPackageJsonSideEffectsArrayKeep(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -431,7 +431,7 @@ func TestPackageJsonSideEffectsNestedDirectoryRemove(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -455,7 +455,7 @@ func TestPackageJsonSideEffectsKeepExportDefaultExpr(t *testing.T) {
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -472,7 +472,7 @@ func TestJSONLoaderRemoveUnused(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -489,7 +489,7 @@ func TestTextLoaderRemoveUnused(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -506,7 +506,7 @@ func TestBase64LoaderRemoveUnused(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
ExtensionToLoader: map[string]config.Loader{
".js": config.LoaderJS,
Expand All @@ -527,7 +527,7 @@ func TestDataURLLoaderRemoveUnused(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
ExtensionToLoader: map[string]config.Loader{
".js": config.LoaderJS,
Expand All @@ -548,7 +548,7 @@ func TestFileLoaderRemoveUnused(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
ExtensionToLoader: map[string]config.Loader{
".js": config.LoaderJS,
Expand All @@ -570,7 +570,7 @@ func TestRemoveUnusedImportMeta(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand Down Expand Up @@ -633,7 +633,7 @@ func TestRemoveUnusedPureCommentCalls(t *testing.T) {
},
entryPaths: []string{"/entry.js"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand All @@ -657,7 +657,7 @@ func TestTreeShakingReactElements(t *testing.T) {
},
entryPaths: []string{"/entry.jsx"},
options: config.Options{
IsBundling: true,
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
Expand Down
Loading

0 comments on commit b23bee3

Please sign in to comment.