Skip to content

Commit 10a3471

Browse files
authored
Merge pull request #395 from formidablejs/feature/vite
Feature/vite
2 parents 8b754c4 + 46083cb commit 10a3471

File tree

8 files changed

+123
-2
lines changed

8 files changed

+123
-2
lines changed

src/Http/View/View.imba

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import isString from '../../Support/Helpers/isString'
77
import querystring from 'querystring'
88
import UndefinedDataPropException from './Exceptions/UndefinedDataPropException'
99
import Language from '../../Support/Language/Language'
10+
import viteHelper from '../../Support/Helpers/vite'
1011

1112
export default class View
1213

@@ -36,6 +37,40 @@ export default class View
3637

3738
self
3839

40+
def vite file\string|string[]
41+
if Array.isArray(file)
42+
let tags = []
43+
44+
for asset in file
45+
if !isString(asset)
46+
throw TypeError "Expected string."
47+
48+
const jsTagExtensions = ['js', 'ts']
49+
const cssTagExtensions = ['css', 'scss', 'sass', 'less', 'styl', 'stylus']
50+
const imgTagExtensions = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'avif']
51+
const fontTagExtensions = ['woff', 'woff2', 'eot', 'ttf', 'otf']
52+
53+
const extension = asset.split('.').pop!.toLowerCase!
54+
55+
if !isString(extension)
56+
throw new Error "Could not determine file extension for: {asset}"
57+
58+
if jsTagExtensions.includes(extension)
59+
tags.push("<script type='module' src={viteHelper(asset)}></script>")
60+
else if cssTagExtensions.includes(extension)
61+
tags.push("<link rel='stylesheet' href={viteHelper(asset)}>")
62+
else if imgTagExtensions.includes(extension)
63+
tags.push("<img src={viteHelper(asset)} alt=''>")
64+
else if fontTagExtensions.includes(extension)
65+
tags.push("<link rel='preload' href={viteHelper(asset)} as='font' type='font/{extension}' crossorigin>")
66+
else
67+
throw new Error "Unsupported file extension: {extension}"
68+
69+
if tags.length > 0
70+
tags.join('\n')
71+
else
72+
viteHelper(file)
73+
3974
def translate key\string, default\any
4075
self.#_language.get(key, default)
4176

src/Support/Helpers/index.imba

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const { default: toBoolean } = require './toBoolean'
3535
const { default: updateLine } = require './updateLine'
3636
const { default: version } = require './version'
3737
const { default: view } = require './view'
38+
const { default: vite } = require './vite'
3839
const { default: wildcard } = require './wildcard'
3940
const { default: without } = require './without'
4041

@@ -81,6 +82,7 @@ export {
8182
updateLine
8283
version
8384
view
85+
vite
8486
wildcard
8587
without
8688
}

src/Support/Helpers/vite.imba

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Repository from '../../Vite/Repository'
2+
3+
export default def vite file\string
4+
Repository.get file

src/Vite/Repository.imba

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { readFileSync, existsSync } from 'fs'
2+
import { join } from 'path'
3+
4+
export default class Repository
5+
6+
static manifestCache\ViteManifest|null = null
7+
8+
static def get file\string
9+
const manifest = self.getManifest!
10+
11+
if !manifest
12+
return file
13+
14+
const normalizedFile = file.startsWith('/') ? file.slice(1) : file
15+
16+
if manifest[normalizedFile]
17+
return '/build/' + manifest[normalizedFile].file
18+
19+
if normalizedFile == 'css/app.css'
20+
for [key, value] in Object.entries(manifest)
21+
if key.includes('css/app.css')
22+
return '/build/' + value.file
23+
24+
if normalizedFile == 'js/app.js'
25+
for [key, value] in Object.entries(manifest)
26+
if key.includes('js/app.ts')
27+
return '/build/' + value.file
28+
29+
file
30+
31+
static def getManifest\ViteManifest|null
32+
if self.manifestCache != null
33+
return self.manifestCache
34+
35+
const location = join(process.cwd!, 'public', 'build', '.vite', 'manifest.json')
36+
37+
try
38+
if existsSync(location)
39+
const content = readFileSync(location, 'utf8')
40+
self.manifestCache = content ? JSON.parse(content) : null
41+
else
42+
self.manifestCache = null
43+
catch err
44+
self.manifestCache = null
45+
46+
self.manifestCache

types/Http/View/View.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export default class View {
3131
*/
3232
__(key: string, default$?: string): string;
3333

34+
/**
35+
* Get asset path(s) from Vite manifest.
36+
*
37+
* @param {string | string[]} file
38+
* @returns {string}
39+
*/
40+
vite(file: string | string[]): string;
41+
3442
/**
3543
* Get old input.
3644
*/

types/Support/Helpers/mix.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/**
2-
@param {string} file
3-
*/
2+
* Get asset path from Laravel Mix manifest.
3+
* @param {string} file
4+
* @returns {string}
5+
* @deprecated
6+
*/
47
export default function mix(file: string): string;

types/Support/Helpers/vite.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Get asset path(s) from Vite manifest.
3+
*
4+
* @param {string | string[]} file
5+
* @returns {string}
6+
*/
7+
export default function vite(file: string | string[]): string;

types/Vite/Repository.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface ViteManifest {
2+
[key: string]: {
3+
file: string;
4+
src?: string;
5+
isEntry?: boolean;
6+
imports?: string[];
7+
css?: string[];
8+
assets?: string[];
9+
};
10+
}
11+
12+
export default class Repository {
13+
static manifestCache: ViteManifest | null = null;
14+
static file(file: string): string;
15+
static getManifest(): ViteManifest | null;
16+
}

0 commit comments

Comments
 (0)