Skip to content

Commit

Permalink
add --outbase option (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsky authored Nov 1, 2020
1 parent 0bc46e3 commit 38d1a19
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Options:
--bundle Bundle all dependencies into the output files
--outfile=... The output file (for one entry point)
--outdir=... The output directory (for multiple entry points)
--outbase=... The base path used to determine entry point output paths (for multiple entry points)
--sourcemap Emit a source map
--target=... Environment target (e.g. es2017, chrome58, firefox57,
safari11, edge16, node10, default esnext)
Expand Down
13 changes: 9 additions & 4 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,13 @@ func (b *Bundle) Compile(log logger.Log, options config.Options) []OutputFile {
options.OutputFormat = config.FormatESModule
}

// Determine the lowest common ancestor of all entry points
lcaAbsPath := b.lowestCommonAncestorDirectory(options.CodeSplitting)
// Get the base path from the options or choose the lowest common ancestor of all entry points
var baseAbsPath string
if options.AbsOutputBase != "" {
baseAbsPath = options.AbsOutputBase
} else {
baseAbsPath = b.lowestCommonAncestorDirectory(options.CodeSplitting)
}

type linkGroup struct {
outputFiles []OutputFile
Expand All @@ -901,7 +906,7 @@ func (b *Bundle) Compile(log logger.Log, options config.Options) []OutputFile {
var resultGroups []linkGroup
if options.CodeSplitting {
// If code splitting is enabled, link all entry points together
c := newLinkerContext(&options, log, b.fs, b.res, b.files, b.entryPoints, lcaAbsPath)
c := newLinkerContext(&options, log, b.fs, b.res, b.files, b.entryPoints, baseAbsPath)
resultGroups = []linkGroup{{
outputFiles: c.link(),
reachableFiles: c.reachableFiles,
Expand All @@ -913,7 +918,7 @@ func (b *Bundle) Compile(log logger.Log, options config.Options) []OutputFile {
for i, entryPoint := range b.entryPoints {
waitGroup.Add(1)
go func(i int, entryPoint uint32) {
c := newLinkerContext(&options, log, b.fs, b.res, b.files, []uint32{entryPoint}, lcaAbsPath)
c := newLinkerContext(&options, log, b.fs, b.res, b.files, []uint32{entryPoint}, baseAbsPath)
resultGroups[i] = linkGroup{
outputFiles: c.link(),
reachableFiles: c.reachableFiles,
Expand Down
19 changes: 19 additions & 0 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3169,6 +3169,25 @@ func TestInjectImportOrder(t *testing.T) {
})
}

func TestOutbase(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/a/b/c.js": `
console.log('c')
`,
"/a/b/d.js": `
console.log('d')
`,
},
entryPaths: []string{"/a/b/c.js", "/a/b/d.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
AbsOutputBase: "/",
},
})
}

func TestAvoidTDZNoBundle(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type linkerContext struct {
entryPoints []uint32
files []file
hasErrors bool
lcaAbsPath string
baseAbsPath string

// We should avoid traversing all files in the bundle, because the linker
// should be able to run a linking operation on a large bundle where only
Expand Down Expand Up @@ -305,7 +305,7 @@ func newLinkerContext(
res resolver.Resolver,
files []file,
entryPoints []uint32,
lcaAbsPath string,
baseAbsPath string,
) linkerContext {
// Clone information about symbols and files so we don't mutate the input data
c := linkerContext{
Expand All @@ -317,7 +317,7 @@ func newLinkerContext(
files: make([]file, len(files)),
symbols: js_ast.NewSymbolMap(len(files)),
reachableFiles: findReachableFiles(files, entryPoints),
lcaAbsPath: lcaAbsPath,
baseAbsPath: baseAbsPath,
}

// Clone various things since we may mutate them later
Expand Down Expand Up @@ -2388,7 +2388,7 @@ func (c *linkerContext) computeChunks() []chunkInfo {
source := file.source
if source.KeyPath.Namespace != "file" {
baseName = source.IdentifierName
} else if relPath, ok := c.fs.Rel(c.lcaAbsPath, source.KeyPath.Text); ok {
} else if relPath, ok := c.fs.Rel(c.baseAbsPath, source.KeyPath.Text); ok {
relDir = c.fs.Dir(relPath)
baseName = c.fs.Base(relPath)
} else {
Expand Down
10 changes: 10 additions & 0 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,16 @@ var require_demo_pkg = __commonJS((exports, module) => {
const demo_pkg = __toModule(require_demo_pkg());
console.log(demo_pkg.default());

================================================================================
TestOutbase
---------- /out/a/b/c.js ----------
// /a/b/c.js
console.log("c");

---------- /out/a/b/d.js ----------
// /a/b/d.js
console.log("d");

================================================================================
TestOutputExtensionRemappingDir
---------- /out/entry.notjs ----------
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ type Options struct {

AbsOutputFile string
AbsOutputDir string
AbsOutputBase string
OutputExtensions map[string]string
ModuleName []string
TsConfigOverride string
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ type BuildOptions struct {
Outfile string
Metafile string
Outdir string
Outbase string
Platform Platform
Format Format
External []string
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ func buildImpl(buildOpts BuildOptions) BuildResult {
OutputFormat: validateFormat(buildOpts.Format),
AbsOutputFile: validatePath(log, realFS, buildOpts.Outfile),
AbsOutputDir: validatePath(log, realFS, buildOpts.Outdir),
AbsOutputBase: validatePath(log, realFS, buildOpts.Outbase),
AbsMetadataFile: validatePath(log, realFS, buildOpts.Metafile),
OutputExtensions: validateOutputExtensions(log, buildOpts.OutExtensions),
ExtensionToLoader: validateLoaders(log, buildOpts.Loader),
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/cli_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func parseOptionsImpl(osArgs []string, buildOpts *api.BuildOptions, transformOpt
case strings.HasPrefix(arg, "--outdir=") && buildOpts != nil:
buildOpts.Outdir = arg[len("--outdir="):]

case strings.HasPrefix(arg, "--outbase=") && buildOpts != nil:
buildOpts.Outbase = arg[len("--outbase="):]

case strings.HasPrefix(arg, "--tsconfig=") && buildOpts != nil:
buildOpts.Tsconfig = arg[len("--tsconfig="):]

Expand Down

0 comments on commit 38d1a19

Please sign in to comment.