Skip to content

Commit

Permalink
fix #1661: remove implicit trailing "/" in "[dir]"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Oct 16, 2021
1 parent 2c147bb commit 3690949
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -34,6 +34,14 @@
}
```

* Remove the implicit `/` after `[dir]` in entry names ([#1661](https://github.com/evanw/esbuild/issues/1661))

The "entry names" feature lets you customize the way output file names are generated. The `[dir]` and `[name]` placeholders are filled in with the directory name and file name of the corresponding entry point file, respectively.

Previously `--entry-names=[dir]/[name]` and `--entry-names=[dir][name]` behaved the same because the value used for `[dir]` always had an implicit trailing slash, since it represents a directory. However, some people want to be able to remove the file name with `--entry-names=[dir]` and the implicit trailing slash gets in the way.

With this release, you can now use the `[dir]` placeholder without an implicit trailing slash getting in the way. For example, the command `esbuild foo/bar/index.js --outbase=. --outdir=out --entry-names=[dir]` previously generated the file `out/foo/bar/.js` but will now generate the file `out/foo/bar.js`.

## 0.13.7

* Minify CSS alpha values correctly ([#1682](https://github.com/evanw/esbuild/issues/1682))
Expand Down
24 changes: 24 additions & 0 deletions internal/bundler/bundler_default_test.go
Expand Up @@ -4742,3 +4742,27 @@ func TestBuiltInNodeModulePrecedence(t *testing.T) {
},
})
}

func TestEntryNamesNoSlashAfterDir(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/src/app1/main.ts": `console.log(1)`,
"/src/app2/main.ts": `console.log(2)`,
"/src/app3/main.ts": `console.log(3)`,
},
entryPathsAdvanced: []EntryPoint{
{InputPath: "/src/app1/main.ts"},
{InputPath: "/src/app2/main.ts"},
{InputPath: "/src/app3/main.ts", OutputPath: "customPath"},
},
options: config.Options{
Mode: config.ModePassThrough,
EntryPathTemplate: []config.PathTemplate{
// "[dir]-[name]"
{Data: "./", Placeholder: config.DirPlaceholder},
{Data: "-", Placeholder: config.NamePlaceholder},
},
AbsOutputDir: "/out",
},
})
}
4 changes: 3 additions & 1 deletion internal/bundler/bundler_test.go
Expand Up @@ -52,6 +52,7 @@ func hasErrors(msgs []logger.Msg) bool {
type bundled struct {
files map[string]string
entryPaths []string
entryPathsAdvanced []EntryPoint
expectedScanLog string
expectedCompileLog string
options config.Options
Expand Down Expand Up @@ -84,10 +85,11 @@ func (s *suite) expectBundled(t *testing.T, args bundled) {
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug)
caches := cache.MakeCacheSet()
resolver := resolver.NewResolver(fs, log, caches, args.options)
entryPoints := make([]EntryPoint, 0, len(args.entryPaths))
entryPoints := make([]EntryPoint, 0, len(args.entryPaths)+len(args.entryPathsAdvanced))
for _, path := range args.entryPaths {
entryPoints = append(entryPoints, EntryPoint{InputPath: path})
}
entryPoints = append(entryPoints, args.entryPathsAdvanced...)
bundle := ScanBundle(log, fs, resolver, caches, entryPoints, args.options, nil)
msgs := log.Done()
assertLog(t, msgs, args.expectedScanLog)
Expand Down
11 changes: 6 additions & 5 deletions internal/bundler/linker.go
Expand Up @@ -605,11 +605,6 @@ func (c *linkerContext) pathBetweenChunks(fromRelDir string, toRelPath string) s
// Returns the path of this file relative to "outbase", which is then ready to
// be joined with the absolute output directory path. The directory and name
// components are returned separately for convenience.
//
// This makes sure to have the directory end in a slash so that it can be
// substituted into a path template without necessarily having a "/" after it.
// Extra slashes should get cleaned up automatically when we join it with the
// output directory.
func pathRelativeToOutbase(
inputFile *graph.InputFile,
options *config.Options,
Expand Down Expand Up @@ -684,7 +679,13 @@ func pathRelativeToOutbase(
// with a "." means that it will not be hidden on Unix.
relDir = strings.Repeat("_.._/", dotDotCount) + relDir[dotDotCount*3:]
}
for strings.HasSuffix(relDir, "/") {
relDir = relDir[:len(relDir)-1]
}
relDir = "/" + relDir
if strings.HasSuffix(relDir, "/.") {
relDir = relDir[:len(relDir)-1]
}
}

// Strip the file extension if the output path is an input file
Expand Down
11 changes: 11 additions & 0 deletions internal/bundler/snapshots/snapshots_default.txt
Expand Up @@ -545,6 +545,17 @@ var init_types = __esm({
// entry.js
console.log((init_types(), types_exports));

================================================================================
TestEntryNamesNoSlashAfterDir
---------- /out/app1-main.js ----------
console.log(1);

---------- /out/app2-main.js ----------
console.log(2);

---------- /out/-customPath.js ----------
console.log(3);

================================================================================
TestExportChain
---------- /out.js ----------
Expand Down
4 changes: 2 additions & 2 deletions scripts/end-to-end-tests.js
Expand Up @@ -4290,8 +4290,8 @@
export let foo = 123
`,
'node.js': `
import {a} from './out/index/pages/a/y.js'
import {b} from './out/index/pages/b/y.js'
import {a} from './out/index/pages/ay.js'
import {b} from './out/index/pages/by.js'
if (a !== 'a123' || b !== 'b123') throw 'fail'
`,
}),
Expand Down
8 changes: 4 additions & 4 deletions scripts/js-api-tests.js
Expand Up @@ -2386,8 +2386,8 @@ require("/assets/file.png");
write: false,
})
assert.strictEqual(outputFiles.length, 2)
assert.strictEqual(outputFiles[0].path, path.join(testDir, 'entry', 'out', '4UQMP3ZH-1.cjs.js'))
assert.strictEqual(outputFiles[1].path, path.join(testDir, 'entry', 'out', 'ACBA75F5-2.mjs.js'))
assert.strictEqual(outputFiles[0].path, path.join(testDir, 'entry', 'out', 'CXHWNMAN-1.cjs.js'))
assert.strictEqual(outputFiles[1].path, path.join(testDir, 'entry', 'out', 'EYSNILNO-2.mjs.js'))
},

async customEntryPointOutputPathsAbs({ esbuild, testDir }) {
Expand All @@ -2407,8 +2407,8 @@ require("/assets/file.png");
write: false,
})
assert.strictEqual(outputFiles.length, 2)
assert.strictEqual(outputFiles[0].path, path.join(testDir, 'entry', 'out', 'MYINLEYF-1.js'))
assert.strictEqual(outputFiles[1].path, path.join(testDir, 'entry', 'out', 'R2MEQS4G-2.js'))
assert.strictEqual(outputFiles[0].path, path.join(testDir, 'entry', 'out', 'TIORPBNU-1.js'))
assert.strictEqual(outputFiles[1].path, path.join(testDir, 'entry', 'out', '3KY7NOSR-2.js'))
},
}

Expand Down

0 comments on commit 3690949

Please sign in to comment.