clean up chunk-relative paths with public path

This commit is contained in:
Evan Wallace
2021-03-27 15:16:36 -07:00
parent 23a085ea8d
commit d4bfad73f6
5 changed files with 63 additions and 15 deletions

View File

@@ -334,13 +334,7 @@ func parseFile(args parseArgs) {
})) + ext
// Determine the final path that this asset will have in the output directory
var publicPath string
if strings.HasSuffix(args.options.PublicPath, "/") && strings.HasPrefix(relPath, "./") {
// Avoid an unnecessary "./" in this case
publicPath = args.options.PublicPath + relPath[2:] + source.KeyPath.IgnoredSuffix
} else {
publicPath = args.options.PublicPath + relPath + source.KeyPath.IgnoredSuffix
}
publicPath := joinWithPublicPath(args.options.PublicPath, relPath+source.KeyPath.IgnoredSuffix)
// Export the resulting relative path as a string
expr := js_ast.Expr{Data: &js_ast.EString{Value: js_lexer.StringToUTF16(publicPath)}}
@@ -519,6 +513,35 @@ func parseFile(args parseArgs) {
args.results <- result
}
func joinWithPublicPath(publicPath string, relPath string) string {
if strings.HasPrefix(relPath, "./") {
relPath = relPath[2:]
// Strip any amount of further no-op slashes (i.e. ".///././/x/y" => "x/y")
for {
if strings.HasPrefix(relPath, "/") {
relPath = relPath[1:]
} else if strings.HasPrefix(relPath, "./") {
relPath = relPath[2:]
} else {
break
}
}
}
// Use a relative path if there is no public path
if publicPath == "" {
publicPath = "."
}
// Join with a slash
slash := "/"
if strings.HasSuffix(publicPath, "/") {
slash = ""
}
return fmt.Sprintf("%s%s%s", publicPath, slash, relPath)
}
func isASCIIOnly(text string) bool {
for _, c := range text {
if c < 0x20 || c > 0x7E {

View File

@@ -504,3 +504,24 @@ func TestSplittingHybridESMAndCJSIssue617(t *testing.T) {
},
})
}
func TestSplittingPublicPathEntryName(t *testing.T) {
splitting_suite.expectBundled(t, bundled{
files: map[string]string{
"/a.js": `
import("./b")
`,
"/b.js": `
console.log('b')
`,
},
entryPaths: []string{"/a.js"},
options: config.Options{
Mode: config.ModeBundle,
CodeSplitting: true,
OutputFormat: config.FormatESModule,
PublicPath: "/www/",
AbsOutputDir: "/out",
},
})
}

View File

@@ -804,12 +804,9 @@ func (c *linkerContext) substituteFinalPaths(
}
func (c *linkerContext) pathBetweenChunks(fromRelDir string, toRelPath string) string {
// Return an absolute path if a public path has been configured
// Join with the public path if it has been configured
if c.options.PublicPath != "" {
if strings.HasPrefix(toRelPath, "./") {
toRelPath = toRelPath[2:]
}
return c.options.PublicPath + toRelPath
return joinWithPublicPath(c.options.PublicPath, toRelPath)
}
// Otherwise, return a relative path

View File

@@ -431,6 +431,16 @@ export {
shared_default
};
================================================================================
TestSplittingPublicPathEntryName
---------- /out/a.js ----------
// a.js
import("/www/b.js");
---------- /out/b.js ----------
// b.js
console.log("b");
================================================================================
TestSplittingReExportIssue273
---------- /out/a.js ----------

View File

@@ -792,9 +792,6 @@ func rebuildImpl(
for i, path := range buildOpts.NodePaths {
options.AbsNodePaths[i] = validatePath(log, realFS, path, "node path")
}
if options.PublicPath != "" && !strings.HasSuffix(options.PublicPath, "/") && !strings.HasSuffix(options.PublicPath, "\\") {
options.PublicPath += "/"
}
entryPoints := append([]string{}, buildOpts.EntryPoints...)
entryPointCount := len(entryPoints)
if buildOpts.Stdin != nil {