Skip to content

Commit

Permalink
feat: add support for vite 5, which changed default manifest path
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 16, 2023
1 parent cbacf01 commit 818132a
Show file tree
Hide file tree
Showing 11 changed files with 772 additions and 477 deletions.
1,174 changes: 730 additions & 444 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions vite-plugin-ruby/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
/// <reference types="vite/client" />
/// <reference types="vue/macros-global" />

interface ImportMeta {
globEagerDefault(pattern: string): Record<string, any>
}
2 changes: 1 addition & 1 deletion vite-plugin-ruby/example/app/frontend/ssr/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import createServer from '@inertiajs/server'
import logo from '~/images/logo.svg'
console.info({ logo })

const pages = import.meta.globEagerDefault('../pages/*.vue')
const pages = import.meta.glob('../pages/*.vue', { import: 'default', eager: true })

createServer(page => createInertiaApp({
page,
Expand Down
2 changes: 1 addition & 1 deletion vite-plugin-ruby/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"sass": "^1.59.3",
"terser": "^5.16.6",
"typescript": "^4.9.5",
"vite": "^4.3.3",
"vite": "^5.0.0",
"vite-plugin-ruby": "workspace:*",
"vue": "^3.2.47"
}
Expand Down
16 changes: 8 additions & 8 deletions vite-plugin-ruby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@
},
"dependencies": {
"debug": "^4.3.4",
"fast-glob": "^3.2.12"
"fast-glob": "^3.3.2"
},
"peerDependencies": {
"vite": ">=4.0.0"
"vite": ">=5.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/node": "^14.18.38",
"rollup": "^3.19.1",
"@types/debug": "^4.1.12",
"@types/node": "^18.7.14",
"rollup": "^4.2.0",
"standard-version": "^9.5.0",
"tsup": "^6.6.3",
"tsup": "^7.2.0",
"typescript": "^4.9.5",
"vite": "^4.3.3",
"vitest": "^0.18.1"
"vite": "^5.0.0",
"vitest": "^0.34.6"
}
}
11 changes: 7 additions & 4 deletions vite-plugin-ruby/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ export function resolveEntrypointFiles (projectRoot: string, sourceCodeDir: stri
return entrypointFiles.map(file => ['ssr', file])
}

return entrypointFiles.map(filename => [
relative(sourceCodeDir, filename),
filename,
])
return entrypointFiles.map((filename) => {
let name = relative(sourceCodeDir, filename)
if (name.startsWith('..'))
name = relative(projectRoot, filename)

return [name, filename]
})
}

// Internal: Allows to use the `~` shorthand in the config globs.
Expand Down
1 change: 1 addition & 0 deletions vite-plugin-ruby/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function outputOptions (assetsDir: string, ssrBuild: boolean) {
}

return {
assetFileNames: ssrBuild ? undefined : outputFileName('[ext]'),
entryFileNames: ssrBuild ? undefined : outputFileName('js'),
}
}
7 changes: 5 additions & 2 deletions vite-plugin-ruby/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ export function assetsManifestPlugin (): Plugin {
async generateBundle (_options, bundle) {
if (!config.build.manifest) return

const manifestDir = typeof config.build.manifest === 'string' ? path.basename(config.build.manifest) : '.vite'
const fileName = `${manifestDir}/manifest-assets.json`

const manifest: AssetsManifest = new Map()
await fingerprintRemainingAssets(this, bundle, manifest)
debug({ manifest })
debug({ manifest, fileName })

this.emitFile({
fileName: 'manifest-assets.json',
fileName,
type: 'asset',
source: JSON.stringify(Object.fromEntries(manifest), null, 2),
})
Expand Down
4 changes: 2 additions & 2 deletions vite-plugin-ruby/tests/entrypoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('resolveEntrypointFiles', () => {

expectEntrypoints({ additionalEntrypoints: ['app/assets/*.{js,css}'] }).toEqual([
...defaultEntrypoints,
['../assets/external.js', resolve('example/app/assets/external.js')],
['../assets/theme.css', resolve('example/app/assets/theme.css')],
['app/assets/external.js', resolve('example/app/assets/external.js')],
['app/assets/theme.css', resolve('example/app/assets/theme.css')],
])
})

Expand Down
23 changes: 14 additions & 9 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ def host_with_port
"#{ host }:#{ port }"
end

# Internal: Path where Vite outputs the manifest file.
def manifest_path
build_output_dir.join('manifest.json')
end

# Internal: Path where vite-plugin-ruby outputs the assets manifest file.
def assets_manifest_path
build_output_dir.join('manifest-assets.json')
# Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.
def known_manifest_paths
[
# NOTE: Generated by Vite when `manifest: true`, which vite-plugin-ruby enables.
'manifest.json',
# NOTE: Path where vite-plugin-ruby outputs the assets manifest file.
'manifest-assets.json',
].flat_map { |path|
[
build_output_dir.join(".vite/#{ path }"), # Vite 5 onwards
build_output_dir.join(path), # Vite 4 and below
]
}
end

# Internal: Path to the manifest files generated by Vite and vite-plugin-ruby.
def manifest_paths
[manifest_path, assets_manifest_path].select(&:exist?)
known_manifest_paths.select(&:exist?)
end

# Public: The directory where Vite will store the built assets.
Expand Down
5 changes: 3 additions & 2 deletions vite_ruby/lib/vite_ruby/missing_entrypoint_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class ViteRuby::MissingEntrypointError < ViteRuby::Error
def initialize(info)
@info = info
super <<~MSG
Vite Ruby can't find #{ file_name } in #{ config.manifest_path.relative_path_from(config.root) } or #{ config.assets_manifest_path.relative_path_from(config.root) }.
Vite Ruby can't find #{ file_name } in the manifests.
Possible causes:
#{ possible_causes(last_build) }
:troubleshooting:
#{ "\nContent in your manifests:\n#{ JSON.pretty_generate(manifest) }\n" if last_build.success }
#{ "Manifest files found:\n#{ config.manifest_paths.map { |path| " #{ path.relative_path_from(config.root) }" }.join("\n") }\n" if last_build.success }
#{ "Content in your manifests:\n#{ JSON.pretty_generate(manifest) }\n" if last_build.success }
#{ "Last build in #{ config.mode } mode:\n#{ last_build.to_json }\n" if last_build.success }
MSG
end
Expand Down

0 comments on commit 818132a

Please sign in to comment.