Skip to content

Commit 003bc55

Browse files
authored
feat: Added public getAssetPaths function to retrieve the asset paths
* Added public function to retrieve the asset paths. * Added documenting comment. * Wrote test for the getAssetPaths method. * Apply cfformat changes * Made use of the "getAssetPaths" function, to get paths and generate tags while in dev mode.
1 parent 88e2951 commit 003bc55

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

models/Vite.cfc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@ component singleton accessors="true" {
44
property name="buildDirectory" default="/includes/build";
55
property name="manifestFileName" default="manifest.json";
66

7+
/**
8+
* Gets the assets path based on the entrypoints passed
9+
*
10+
* @param entrypoints The entrypoints to get the assets for
11+
* @return array The asset paths
12+
*/
13+
function getAssetPaths( required any entrypoints ) {
14+
arguments.entrypoints = arrayWrap( arguments.entrypoints );
15+
if ( isRunningHot() ) {
16+
return arguments.entrypoints.map( ( entrypoint ) => generateHotAssetPath( entrypoint ) );
17+
}
18+
19+
var manifest = readManifest();
20+
return arguments.entrypoints.map( ( entrypoint ) => generateAssetPath( getEntrypointChunk( entrypoint ).file ) );
21+
}
22+
/**
23+
* Renders the assets for the entrypoints passed
24+
*
25+
* @param entrypoints The entrypoints to get the assets for
26+
*/
727
function render( required any entrypoints ) output="true" {
828
arguments.entrypoints = arrayWrap( arguments.entrypoints );
929

1030
if ( isRunningHot() ) {
1131
arrayPrepend( arguments.entrypoints, "/@vite/client" );
12-
write( arguments.entrypoints.map( ( entrypoint ) => generateTag( generateHotAssetPath( entrypoint ) ) ) );
32+
write( getAssetPaths( arguments.entrypoints ).map( ( path ) => generateTag( path ) ) );
1333
return;
1434
}
1535

tests/specs/unit/ViteHelperSpec.cfc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,27 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
142142
}
143143
}
144144
} );
145+
146+
it( "can generate an array of asset paths, using custom buildDirectory and manifestFileName parameters", () => {
147+
var customBuildDirectory = "/includes/somewhere-else";
148+
var customManifestFileName = "app-manifest.json";
149+
var customManifestFilePath = expandPath( "/app#customBuildDirectory#/#customManifestFileName#" );
150+
try {
151+
fileWrite( customManifestFilePath, serializeJSON( variables.manifestFileContents ) );
152+
var output = vite()
153+
.setBuildDirectory( customBuildDirectory )
154+
.setManifestFileName( customManifestFileName )
155+
.getAssetPaths( [ "resources/assets/css/app.css" ] );
156+
157+
expect( output ).toBeTypeOf( "Array" );
158+
expect( output ).toHaveLength( 1 );
159+
expect( output[ 1 ] ).toInclude( "includes/somewhere-else/assets/app-00d284d6.css" );
160+
} finally {
161+
if ( fileExists( customManifestFilePath ) ) {
162+
fileDelete( customManifestFilePath );
163+
}
164+
}
165+
} );
145166
} );
146167
} );
147168
}

0 commit comments

Comments
 (0)