-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from supercharge/vite-refactorings
Vite refactorings
- Loading branch information
Showing
23 changed files
with
857 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
export interface ViteConfig { | ||
/** | ||
* Stores the URL used as a prefix when creating asset URLs. This could be a | ||
* CDN URL for production builds. If empty, the created asset URL starts | ||
* with a leading slash to serve it locally from the running server. | ||
* | ||
* @default `/build` | ||
*/ | ||
assetsUrl?: string | ||
|
||
/** | ||
* Stores the path to the hot-reload file, relative from the application’s base directory. | ||
* | ||
* @default `<assetsUrl>/.vite/hot.json` | ||
*/ | ||
hotReloadFilePath?: string | ||
|
||
/** | ||
* Stores the Vite manifest file path, relative from the application’s base directory. | ||
* | ||
* @default `<assetsUrl>/.vite/manifest.json` | ||
*/ | ||
manifestFilePath?: string | ||
|
||
/** | ||
* Stores an object of attributes to apply on all HTML `script` tags. | ||
* | ||
* @default `{}` | ||
*/ | ||
scriptAttributes?: Record<string, string | boolean> | ||
|
||
/** | ||
* Stores an object of attributes to apply on all HTML `style` tags. | ||
* | ||
* @default `{}` | ||
*/ | ||
styleAttributes?: Record<string, string | boolean> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
|
||
import Path from 'node:path' | ||
import { Str } from '@supercharge/strings' | ||
import { ViteConfig as ViteConfigContract } from '@supercharge/contracts' | ||
|
||
export class ViteConfig { | ||
/** | ||
* Stores the Vite config object. | ||
*/ | ||
private readonly config: Required<ViteConfigContract> | ||
|
||
/** | ||
* Create a new instance. | ||
*/ | ||
constructor (config: ViteConfigContract) { | ||
this.config = this.createConfigFrom(config ?? {}) | ||
} | ||
|
||
/** | ||
* Returns a new instance for the given Vite `config`. | ||
*/ | ||
static from (config: ViteConfigContract): ViteConfig { | ||
return new this(config) | ||
} | ||
|
||
/** | ||
* Returns the resolved Vite config. | ||
*/ | ||
private createConfigFrom (config: Partial<ViteConfigContract> = {}): Required<ViteConfigContract> { | ||
const assetsUrl = Str( | ||
this.isCdnUrl(config.assetsUrl) | ||
? config.assetsUrl | ||
: Str(config.assetsUrl ?? '/build') | ||
.ltrim('/') | ||
.start('/') | ||
) | ||
.rtrim('/') | ||
.get() | ||
|
||
return { | ||
assetsUrl, | ||
hotReloadFilePath: config.hotReloadFilePath ?? Path.join(assetsUrl, '/.vite/hot.json'), | ||
manifestFilePath: config.manifestFilePath ?? Path.join(assetsUrl, '.vite/manifest.json'), | ||
styleAttributes: { ...config.styleAttributes }, | ||
scriptAttributes: { ...config.scriptAttributes }, | ||
} | ||
} | ||
|
||
/** | ||
* Determine whether the given `assetsUrl` is a full URL. | ||
*/ | ||
private isCdnUrl (assetsUrl: ViteConfigContract['assetsUrl']): assetsUrl is string { | ||
if (!assetsUrl) { | ||
return false | ||
} | ||
|
||
try { | ||
const url = new URL(assetsUrl) | ||
|
||
return url.protocol.startsWith('http') | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Vite config object. | ||
*/ | ||
toJSON (): ViteConfigContract { | ||
return { | ||
assetsUrl: this.assetsUrl(), | ||
hotReloadFilePath: this.hotReloadFilePath(), | ||
manifestFilePath: this.manifestFilePath(), | ||
styleAttributes: this.styleAttributes(), | ||
scriptAttributes: this.scriptAttributes(), | ||
} | ||
} | ||
|
||
/** | ||
* Returns the Vite hot-reload file path. The hot-reload file contains the Vite dev server URL. | ||
*/ | ||
hotReloadFilePath (): string { | ||
return this.config.hotReloadFilePath | ||
} | ||
|
||
/** | ||
* Returns the Vite manifest file path. | ||
*/ | ||
manifestFilePath (): string { | ||
return this.config.manifestFilePath | ||
} | ||
|
||
/** | ||
* Returns the assets URL. | ||
*/ | ||
assetsUrl (): string { | ||
return this.config.assetsUrl | ||
} | ||
|
||
/** | ||
* Returns the default attributes assigned to every `script` tag. | ||
*/ | ||
scriptAttributes (): ViteConfigContract['scriptAttributes'] { | ||
return this.config.scriptAttributes | ||
} | ||
|
||
/** | ||
* Returns the default attributes assigned to every `style` tag. | ||
*/ | ||
styleAttributes (): ViteConfigContract['styleAttributes'] { | ||
return this.config.styleAttributes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.