mirror of
				https://github.com/gohugoio/hugo.git
				synced 2024-05-11 05:54:58 +00:00 
			
		
		
		
	resources/js: Fix some import discrepancies between Hugo and ESBuild
This fixes the cases where ```js import 'imp2/index.js'; import 'imp3/foo.js'; ``` And these files lives in `assets` as: ``` imp2/index.ts imp3/foo.ts ``` Fixes #10527
This commit is contained in:
		@@ -259,3 +259,47 @@ JS Content:{{ $js.Content }}:End:
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// See issue 10527.
 | 
				
			||||||
 | 
					func TestImportHugoVsESBuild(t *testing.T) {
 | 
				
			||||||
 | 
						c := qt.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, importSrcDir := range []string{"node_modules", "assets"} {
 | 
				
			||||||
 | 
							c.Run(importSrcDir, func(c *qt.C) {
 | 
				
			||||||
 | 
								files := `
 | 
				
			||||||
 | 
					-- IMPORT_SRC_DIR/imp1/index.js --
 | 
				
			||||||
 | 
					console.log("IMPORT_SRC_DIR:imp1/index.js");
 | 
				
			||||||
 | 
					-- IMPORT_SRC_DIR/imp2/index.ts --
 | 
				
			||||||
 | 
					console.log("IMPORT_SRC_DIR:imp2/index.ts");
 | 
				
			||||||
 | 
					-- IMPORT_SRC_DIR/imp3/foo.ts --
 | 
				
			||||||
 | 
					console.log("IMPORT_SRC_DIR:imp3/foo.ts");
 | 
				
			||||||
 | 
					-- assets/js/main.js --
 | 
				
			||||||
 | 
					import 'imp1/index.js';
 | 
				
			||||||
 | 
					import 'imp2/index.js';
 | 
				
			||||||
 | 
					import 'imp3/foo.js';
 | 
				
			||||||
 | 
					-- layouts/index.html --
 | 
				
			||||||
 | 
					{{ $js := resources.Get "js/main.js" | js.Build }}
 | 
				
			||||||
 | 
					{{ $js.RelPermalink }}
 | 
				
			||||||
 | 
								`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								files = strings.ReplaceAll(files, "IMPORT_SRC_DIR", importSrcDir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								b := hugolib.NewIntegrationTestBuilder(
 | 
				
			||||||
 | 
									hugolib.IntegrationTestConfig{
 | 
				
			||||||
 | 
										T:           c,
 | 
				
			||||||
 | 
										NeedsOsFS:   true,
 | 
				
			||||||
 | 
										TxtarString: files,
 | 
				
			||||||
 | 
									}).Build()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								expected := `
 | 
				
			||||||
 | 
					IMPORT_SRC_DIR:imp1/index.js	
 | 
				
			||||||
 | 
					IMPORT_SRC_DIR:imp2/index.ts
 | 
				
			||||||
 | 
					IMPORT_SRC_DIR:imp3/foo.ts		
 | 
				
			||||||
 | 
					`
 | 
				
			||||||
 | 
								expected = strings.ReplaceAll(expected, "IMPORT_SRC_DIR", importSrcDir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								b.AssertFileContent("public/js/main.js", expected)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -150,7 +150,7 @@ func resolveComponentInAssets(fs afero.Fs, impPath string) *hugofs.FileMeta {
 | 
				
			|||||||
		for _, ext := range []string{".js", ".ts", ".tsx", ".jsx"} {
 | 
							for _, ext := range []string{".js", ".ts", ".tsx", ".jsx"} {
 | 
				
			||||||
			if strings.HasSuffix(impPath, ext) {
 | 
								if strings.HasSuffix(impPath, ext) {
 | 
				
			||||||
				// Import of foo.js.js need the full name.
 | 
									// Import of foo.js.js need the full name.
 | 
				
			||||||
				return nil
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if fi, err := fs.Stat(base + ext); err == nil {
 | 
								if fi, err := fs.Stat(base + ext); err == nil {
 | 
				
			||||||
				return fi.(hugofs.FileMetaInfo).Meta()
 | 
									return fi.(hugofs.FileMetaInfo).Meta()
 | 
				
			||||||
@@ -163,12 +163,10 @@ func resolveComponentInAssets(fs afero.Fs, impPath string) *hugofs.FileMeta {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var m *hugofs.FileMeta
 | 
						var m *hugofs.FileMeta
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// See issue #8949.
 | 
					 | 
				
			||||||
	// We need to check if this is a regular file imported without an extension.
 | 
						// We need to check if this is a regular file imported without an extension.
 | 
				
			||||||
	// There may be ambigous situations where both foo.js and foo/index.js exists.
 | 
						// There may be ambigous situations where both foo.js and foo/index.js exists.
 | 
				
			||||||
	// This import order is in line with both how Node and ESBuild's native
 | 
						// This import order is in line with both how Node and ESBuild's native
 | 
				
			||||||
	// import resolver works.
 | 
						// import resolver works.
 | 
				
			||||||
	// This was fixed in Hugo 0.88.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// It may be a regular file imported without an extension, e.g.
 | 
						// It may be a regular file imported without an extension, e.g.
 | 
				
			||||||
	// foo or foo/index.
 | 
						// foo or foo/index.
 | 
				
			||||||
@@ -176,14 +174,17 @@ func resolveComponentInAssets(fs afero.Fs, impPath string) *hugofs.FileMeta {
 | 
				
			|||||||
	if m != nil {
 | 
						if m != nil {
 | 
				
			||||||
		return m
 | 
							return m
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if filepath.Base(impPath) == "index" {
 | 
					
 | 
				
			||||||
 | 
						base := filepath.Base(impPath)
 | 
				
			||||||
 | 
						if base == "index" {
 | 
				
			||||||
 | 
							// try index.esm.js etc.
 | 
				
			||||||
		m = findFirst(impPath + ".esm")
 | 
							m = findFirst(impPath + ".esm")
 | 
				
			||||||
		if m != nil {
 | 
							if m != nil {
 | 
				
			||||||
			return m
 | 
								return m
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Finally check the path as is.
 | 
						// Check the path as is.
 | 
				
			||||||
	fi, err := fs.Stat(impPath)
 | 
						fi, err := fs.Stat(impPath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
@@ -195,6 +196,8 @@ func resolveComponentInAssets(fs afero.Fs, impPath string) *hugofs.FileMeta {
 | 
				
			|||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			m = fi.(hugofs.FileMetaInfo).Meta()
 | 
								m = fi.(hugofs.FileMetaInfo).Meta()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						} else if strings.HasSuffix(base, ".js") {
 | 
				
			||||||
 | 
							m = findFirst(strings.TrimSuffix(impPath, ".js"))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return m
 | 
						return m
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user