1- const fs = require ( 'fs' ) ;
2- const path = require ( 'path' ) ;
3- const { fileURLToPath, pathToFileURL } = require ( 'url' ) ;
4- const { promisify } = require ( 'util' ) ;
5-
6- const access = promisify ( fs . access ) ;
1+ import { access } from 'node:fs/promises' ;
2+ import path from 'node:path' ;
3+ import { fileURLToPath , pathToFileURL } from 'node:url' ;
4+ import { net , protocol } from 'electron' ;
75
86//
97// Patch asset loading -- Ember apps use absolute paths to reference their
108// assets, e.g. `<img src="/images/foo.jpg">`. When the current URL is a `file:`
119// URL, that ends up resolving to the absolute filesystem path `/images/foo.jpg`
1210// rather than being relative to the root of the Ember app. So, we intercept
13- // `file:` URL request and look to see if they point to an asset when
11+ // `file:` URL requests and look to see if they point to an asset when
1412// interpreted as being relative to the root of the Ember app. If so, we return
1513// that path, and if not we leave them as-is, as their absolute path.
1614//
17- async function getAssetPath ( emberAppDir , url ) {
18- let urlPath = fileURLToPath ( url ) ;
15+ export async function getAssetPath ( emberAppDir : string , url : string ) {
16+ const urlPath = fileURLToPath ( url ) ;
1917 // Get the root of the path -- should be '/' on MacOS or something like
20- // 'C:\' on Windows
21- let { root } = path . parse ( urlPath ) ;
18+ // 'C:\\ ' on Windows
19+ const { root } = path . parse ( urlPath ) ;
2220 // Get the relative path from the root to the full path
23- let relPath = path . relative ( root , urlPath ) ;
21+ const relPath = path . relative ( root , urlPath ) ;
2422 // Join the relative path with the Ember app directory
25- let appPath = path . join ( emberAppDir , relPath ) ;
23+ const appPath = path . join ( emberAppDir , relPath ) ;
24+
2625 try {
2726 await access ( appPath ) ;
2827 return appPath ;
@@ -31,14 +30,12 @@ async function getAssetPath(emberAppDir, url) {
3130 }
3231}
3332
34- module . exports = function handleFileURLs ( emberAppDir ) {
35- const { protocol, net } = require ( 'electron' ) ;
36-
33+ export default function handleFileURLs ( emberAppDir : string ) {
3734 if ( protocol . handle ) {
3835 // Electron >= 25
3936 protocol . handle ( 'file' , async ( { url } ) => {
40- let path = await getAssetPath ( emberAppDir , url ) ;
41- return net . fetch ( pathToFileURL ( path ) , {
37+ const assetPath = await getAssetPath ( emberAppDir , url ) ;
38+ return net . fetch ( pathToFileURL ( assetPath ) , {
4239 bypassCustomProtocolHandlers : true ,
4340 } ) ;
4441 } ) ;
@@ -48,6 +45,4 @@ module.exports = function handleFileURLs(emberAppDir) {
4845 callback ( await getAssetPath ( emberAppDir , url ) ) ;
4946 } ) ;
5047 }
51- } ;
52-
53- module . exports . getAssetPath = getAssetPath ;
48+ }
0 commit comments