From e65a4a2e3c319829866846c10a4842f91e1cf17a Mon Sep 17 00:00:00 2001 From: GuyARoss Date: Wed, 23 Oct 2024 11:38:44 -0400 Subject: [PATCH] fix orbit --- internal/assets/embed/orbit.go | 4 +++ internal/assets/embed/orbit_dynamic.go | 2 ++ internal/build.go | 10 +++--- internal/libout/goout.go | 10 ++++++ internal/libout/libout.go | 11 ++++--- pkg/webwrap/react_csr.go | 44 ++++++++++++++++++++++---- 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/internal/assets/embed/orbit.go b/internal/assets/embed/orbit.go index eb3088c..6927355 100644 --- a/internal/assets/embed/orbit.go +++ b/internal/assets/embed/orbit.go @@ -103,6 +103,10 @@ func buildHTMLPages(data []byte, pages ...PageRender) *htmlDoc { ctx := context.Background() for _, p := range pages { if op := wrapDocRender[p]; op != nil { + if useManifestConfig { + html.Head = append(html.Head, fmt.Sprintf(``, string(p))) + } + html, ctx = op.fn(ctx, string(p), data, html) } } diff --git a/internal/assets/embed/orbit_dynamic.go b/internal/assets/embed/orbit_dynamic.go index 1d30740..af2abeb 100644 --- a/internal/assets/embed/orbit_dynamic.go +++ b/internal/assets/embed/orbit_dynamic.go @@ -41,3 +41,5 @@ type DocumentRenderer struct { } var wrapDocRender = map[PageRender]*DocumentRenderer{} + +var useManifestConfig bool = false diff --git a/internal/build.go b/internal/build.go index ef98d84..68a6ed4 100644 --- a/internal/build.go +++ b/internal/build.go @@ -40,7 +40,6 @@ func Build(opts *BuildOpts) (srcpack.PackedComponentList, error) { fileAssets := make([]fs.DirEntry, 0) switch { case experiments.GlobalExperimentalFeatures.PreferViteCompiler: - fileAssets = append(fileAssets, ats.AssetEntry(assets.VitePackConfig)) default: fileAssets = append(fileAssets, ats.AssetEntry(assets.WebPackConfig), ats.AssetEntry(assets.SSRProtoFile), @@ -80,10 +79,11 @@ func Build(opts *BuildOpts) (srcpack.PackedComponentList, error) { } bg := libout.New(&libout.BundleGroupOpts{ - PackageName: opts.Packname, - BaseBundleOut: ".orbit/dist", - BundleMode: opts.Mode, - PublicDir: opts.PublicDir, + PackageName: opts.Packname, + BaseBundleOut: ".orbit/dist", + BundleMode: opts.Mode, + PublicDir: opts.PublicDir, + UseManifestConfig: experiments.GlobalExperimentalFeatures.PreferViteCompiler, }) ctx := context.Background() diff --git a/internal/libout/goout.go b/internal/libout/goout.go index 1ca2f48..f998a92 100644 --- a/internal/libout/goout.go +++ b/internal/libout/goout.go @@ -219,6 +219,13 @@ func (l *GOLibout) HTTPFile(packageName string) (LiboutFile, error) { }, nil } +func boolToString(b bool) string { + if b { + return "true" + } + return "false" +} + func (l *GOLibout) EnvFile(bg *BundleGroup) (LiboutFile, error) { out := strings.Builder{} @@ -300,6 +307,9 @@ type DocumentRenderer struct { out.WriteString(fmt.Sprintf(`var hotReloadPort int = %d`, bg.HotReloadPort)) out.WriteString("\n") + out.WriteString(fmt.Sprintf(`var useManifestConfig bool = %s`, boolToString(bg.UseManifestConfig))) + out.WriteString("\n") + out.WriteString("type PageRender string\n\n") for idx, p := range bg.pages { diff --git a/internal/libout/libout.go b/internal/libout/libout.go index c4c1c81..5f145eb 100644 --- a/internal/libout/libout.go +++ b/internal/libout/libout.go @@ -14,11 +14,12 @@ import ( ) type BundleGroupOpts struct { - PackageName string - BaseBundleOut string - BundleMode string - PublicDir string - HotReloadPort int + PackageName string + BaseBundleOut string + BundleMode string + PublicDir string + HotReloadPort int + UseManifestConfig bool } type page struct { diff --git a/pkg/webwrap/react_csr.go b/pkg/webwrap/react_csr.go index ea9c702..d3ac4b5 100644 --- a/pkg/webwrap/react_csr.go +++ b/pkg/webwrap/react_csr.go @@ -126,13 +126,43 @@ func (s *ReactCSR) RequiredBodyDOMElements(ctx context.Context, cache *CacheDOMO func (b *ReactCSR) Setup(ctx context.Context, settings *BundleOpts) (*BundledResource, error) { page := jsparse.NewEmptyDocument() - bundleFilePath := fmt.Sprintf("%s/%s.js", b.PageOutputDir, settings.BundleKey) if experiments.GlobalExperimentalFeatures.PreferViteCompiler { + bundleFilePath := fmt.Sprintf("%s/%s.jsx", b.PageOutputDir, settings.BundleKey) + + page.AddOther(fmt.Sprintf(` +import { defineConfig } from 'vite'; +import { resolve } from 'path'; +import react from '@vitejs/plugin-react'; + +export default defineConfig({ + plugins: [react()], + build: { + outDir: '../../dist', + manifest: true, + rollupOptions: { + input: resolve(__dirname, "./%s.jsx"), + output: { + entryFileNames: '[name].js', + chunkFileNames: '[name].js', + assetFileNames: '[name].[ext]' + } + } + }, + optimizeDeps: { + include: ['@emotion/styled'], + }, +}); +`, settings.BundleKey)) return &BundledResource{ BundleOpFileDescriptor: map[string]string{"normal": bundleFilePath}, - Configurators: []BundleConfigurator{}, + Configurators: []BundleConfigurator{ + { + FilePath: fmt.Sprintf("%s/%s.config.js", b.PageOutputDir, settings.BundleKey), + Page: page, + }, + }, }, nil } @@ -156,6 +186,8 @@ func (b *ReactCSR) Setup(ctx context.Context, settings *BundleOpts) (*BundledRes outputFileName := fmt.Sprintf("%s.js", settings.BundleKey) + bundleFilePath := fmt.Sprintf("%s/%s.js", b.PageOutputDir, settings.BundleKey) + page.AddOther(fmt.Sprintf(`module.exports = merge(baseConfig, { entry: ['./%s'], mode: '%s', @@ -176,12 +208,12 @@ func (b *ReactCSR) Setup(ctx context.Context, settings *BundleOpts) (*BundledRes } func (b *ReactCSR) Bundle(configuratorFilePath string, filePath string) error { - if experiments.GlobalExperimentalFeatures.PreferViteCompiler { // TODO: do this once - cmd := exec.Command("vite", "build", ".orbit/base/pages", "--config", ".orbit/assets/vite.config.js") - output, err := cmd.Output() + if experiments.GlobalExperimentalFeatures.PreferViteCompiler { + cmd := exec.Command("vite", "build", ".orbit/base/pages", "--config", configuratorFilePath) + _, err := cmd.Output() if err != nil { - b.Logger.Warn(fmt.Sprintf(`invalid pack: vite '%s'`, output)) + b.Logger.Warn(fmt.Sprintf(`invalid pack: vite '%s'`, err)) return parseerror.New("failed to bundle, this could denote a syntax error", filePath) } return nil