Skip to content

Commit

Permalink
fix #674: add "--platform=neutral"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 18, 2021
1 parent a6c4a03 commit e8c2b62
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
}
```

* Add the `--platform=neutral` API option ([#674](https://github.com/evanw/esbuild/issues/674))

There are currently two platform values: `browser` (the default) and `node`. These settings are a convenient way to configure multiple defaults for other API options for maximum compatibility. However, some users want to configure everything themselves so esbuild does not assume any platform-specific behavior. In this case you can now use `--platform=neutral` to disable platform-specific default values. Note that this means if you want to use npm-style packages you will have to configure a main field yourself with something like `--main-fields=main`.

* Provide minified and non-minified versions of in-browser API library ([#616](https://github.com/evanw/esbuild/issues/616))

The in-browser JavaScript API libraries for esbuild are in the [esbuild-wasm](https://www.npmjs.com/package/esbuild-wasm) package. There are two: `esbuild-wasm/lib/browser.js` in UMD format and `esbuild-wasm/esm/browser.js` in ESM format. Previously these were minified since they contain a large string of JavaScript that cannot be minified by other tools. Now they are no longer minified, and there are new minified versions available at `esbuild-wasm/lib/browser.min.js` and `esbuild-wasm/esm/browser.min.js`.
Expand Down
2 changes: 2 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,8 @@ func (cache *runtimeCache) processedDefines(key config.Platform) (defines *confi
platform = "browser"
case config.PlatformNode:
platform = "node"
case config.PlatformNeutral:
platform = "neutral"
}
result := config.ProcessDefines(map[string]config.DefineData{
"__platform": {
Expand Down
64 changes: 64 additions & 0 deletions internal/bundler/bundler_packagejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,67 @@ func TestPackageJsonMainFieldsB(t *testing.T) {
},
})
}

func TestPackageJsonNeutralNoDefaultMainFields(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/entry.js": `
import fn from 'demo-pkg'
console.log(fn())
`,
"/Users/user/project/node_modules/demo-pkg/package.json": `
{
"main": "./main.js",
"module": "./main.esm.js"
}
`,
"/Users/user/project/node_modules/demo-pkg/main.js": `
module.exports = function() {
return 123
}
`,
"/Users/user/project/node_modules/demo-pkg/main.esm.js": `
export default function() {
return 123
}
`,
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
Platform: config.PlatformNeutral,
AbsOutputFile: "/Users/user/project/out.js",
},
expectedScanLog: `Users/user/project/src/entry.js: error: Could not resolve "demo-pkg" (mark it as external to exclude it from the bundle)
`,
})
}

func TestPackageJsonNeutralExplicitMainFields(t *testing.T) {
packagejson_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/src/entry.js": `
import fn from 'demo-pkg'
console.log(fn())
`,
"/Users/user/project/node_modules/demo-pkg/package.json": `
{
"hello": "./main.js",
"module": "./main.esm.js"
}
`,
"/Users/user/project/node_modules/demo-pkg/main.js": `
module.exports = function() {
return 123
}
`,
},
entryPaths: []string{"/Users/user/project/src/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
Platform: config.PlatformNeutral,
MainFields: []string{"hello"},
AbsOutputFile: "/Users/user/project/out.js",
},
})
}
14 changes: 14 additions & 0 deletions internal/bundler/snapshots/snapshots_packagejson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,17 @@ function main_esm_default() {

// Users/user/project/src/entry.js
console.log(main_esm_default());

================================================================================
TestPackageJsonNeutralExplicitMainFields
---------- /Users/user/project/out.js ----------
// Users/user/project/node_modules/demo-pkg/main.js
var require_main = __commonJS((exports, module) => {
module.exports = function() {
return 123;
};
});

// Users/user/project/src/entry.js
var import_demo_pkg = __toModule(require_main());
console.log(import_demo_pkg.default());
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Platform uint8
const (
PlatformBrowser Platform = iota
PlatformNode
PlatformNeutral
)

type StrictOptions struct {
Expand Down
5 changes: 5 additions & 0 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ var defaultMainFields = map[config.Platform][]string{
// configure the main fields to be "module" and then "main". Keep in mind
// that some packages may break if you do this.
config.PlatformNode: []string{"main", "module"},

// The neutral platform is for people that don't want esbuild to try to
// pick good defaults for their platform. In that case, the list of main
// fields is empty by default. You must explicitly configure it yourself.
config.PlatformNeutral: []string{},
}

// Path resolution is a mess. One tricky issue is the "module" override for the
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Platform = 'browser' | 'node';
export type Platform = 'browser' | 'node' | 'neutral';
export type Format = 'iife' | 'cjs' | 'esm';
export type Loader = 'js' | 'jsx' | 'ts' | 'tsx' | 'css' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'binary' | 'default';
export type LogLevel = 'info' | 'warning' | 'error' | 'silent';
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type Platform uint8
const (
PlatformBrowser Platform = iota
PlatformNode
PlatformNeutral
)

type Format uint8
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func validatePlatform(value Platform) config.Platform {
return config.PlatformBrowser
case PlatformNode:
return config.PlatformNode
case PlatformNeutral:
return config.PlatformNeutral
default:
panic("Invalid platform")
}
Expand Down Expand Up @@ -637,6 +639,8 @@ func rebuildImpl(
options.OutputFormat = config.FormatIIFE
case config.PlatformNode:
options.OutputFormat = config.FormatCommonJS
case config.PlatformNeutral:
options.OutputFormat = config.FormatESModule
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func parseOptionsImpl(osArgs []string, buildOpts *api.BuildOptions, transformOpt
buildOpts.Platform = api.PlatformBrowser
case "node":
buildOpts.Platform = api.PlatformNode
case "neutral":
buildOpts.Platform = api.PlatformNeutral
default:
return fmt.Errorf("Invalid platform: %q (valid: browser, node)", value)
}
Expand Down

0 comments on commit e8c2b62

Please sign in to comment.