Skip to content

Commit

Permalink
fix #1013: parsing for the "[dir]" placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 19, 2021
1 parent 237870f commit 514a7b0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Fix parsing of the `[dir]` placeholder ([#1013](https://github.com/evanw/esbuild/issues/1013))

The entry names feature in the previous release accidentally didn't include parsing for the `[dir]` placeholder, so the `[dir]` placeholder was passed through verbatim into the resulting output paths. This release fixes the bug, which means you can now use the `[dir]` placeholder.

## 0.9.4

* Enable hashes in entry point file paths ([#518](https://github.com/evanw/esbuild/issues/518))
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func parseFile(args parseArgs) {
hashBytes := sha1.Sum([]byte(source.Contents))
hash = hashForFileName(hashBytes)
}
dir := "./"
dir := "/"
relPath := config.TemplateToString(config.SubstituteTemplate(args.options.AssetPathTemplate, config.PathPlaceholders{
Dir: &dir,
Name: &base,
Expand Down
5 changes: 3 additions & 2 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func (c *linkerContext) pathBetweenChunks(fromRelDir string, toRelPath string) s
// output directory.
func (c *linkerContext) pathRelativeToOutbase(sourceIndex uint32, stdExt string) (relDir string, baseName string, baseExt string) {
file := &c.files[sourceIndex]
relDir = "./"
relDir = "/"
baseExt = stdExt

// If the output path was configured explicitly, use it verbatim
Expand Down Expand Up @@ -942,6 +942,7 @@ func (c *linkerContext) pathRelativeToOutbase(sourceIndex uint32, stdExt string)
// with a "." means that it will not be hidden on Unix.
relDir = strings.Repeat("_.._/", dotDotCount) + relDir[dotDotCount*3:]
}
relDir = "/" + relDir
return
}

Expand Down Expand Up @@ -3093,7 +3094,7 @@ func (c *linkerContext) computeChunks() []chunkInfo {
dir, base, ext = c.pathRelativeToOutbase(chunk.sourceIndex, stdExt)
template = c.options.EntryPathTemplate
} else {
dir = "./"
dir = "/"
base = "chunk"
ext = stdExt
template = c.options.ChunkPathTemplate
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 @@ -48,6 +48,10 @@ func validatePathTemplate(template string) []config.PathTemplate {

// Check for a placeholder
switch {
case strings.HasPrefix(tail, "[dir]"):
placeholder = config.DirPlaceholder
search += len("[dir]")

case strings.HasPrefix(tail, "[name]"):
placeholder = config.NamePlaceholder
search += len("[name]")
Expand Down
47 changes: 47 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,53 @@
`,
}),

// Code splitting via sharing with name templates
test([
'a.js', 'b.js', '--outdir=out', '--splitting', '--format=esm', '--bundle',
'--entry-names=[name][dir]x', '--chunk-names=[name]/[hash]',
], {
'a.js': `
import * as ns from './common'
export let a = 'a' + ns.foo
`,
'b.js': `
import * as ns from './common'
export let b = 'b' + ns.foo
`,
'common.js': `
export let foo = 123
`,
'node.js': `
import {a} from './out/a/x.js'
import {b} from './out/b/x.js'
if (a !== 'a123' || b !== 'b123') throw 'fail'
`,
}),

// Code splitting via sharing with name templates
test([
'pages/a/index.js', 'pages/b/index.js', '--outbase=.',
'--outdir=out', '--splitting', '--format=esm', '--bundle',
'--entry-names=[name][dir]y', '--chunk-names=[name]/[hash]',
], {
'pages/a/index.js': `
import * as ns from '../common'
export let a = 'a' + ns.foo
`,
'pages/b/index.js': `
import * as ns from '../common'
export let b = 'b' + ns.foo
`,
'pages/common.js': `
export let foo = 123
`,
'node.js': `
import {a} from './out/index/pages/a/y.js'
import {b} from './out/index/pages/b/y.js'
if (a !== 'a123' || b !== 'b123') throw 'fail'
`,
}),

// Code splitting via ES6 module double-imported with sync and async imports
test(['a.js', '--outdir=out', '--splitting', '--format=esm', '--bundle'], {
'a.js': `
Expand Down

0 comments on commit 514a7b0

Please sign in to comment.