import type * as d from '../../../declarations' ; import { isCommonDirModuleFile , isTsFile , isTsxFile } from '../resolve/resolve-utils' ; import { isFunction , normalizePath } from '@utils' ; export const httpFetch = ( sys , input , init ? ) : Promise < Response > => { console . trace ( input ) ; if ( sys && isFunction ( sys . fetch ) ) { return sys . fetch ( input , init ) ; } return fetch ( input , init ) ; } ; export const packageVersions = new Map < string , string > ( ) ; export const known404Urls = new Set < string > ( ) ; export const getStencilRootUrl = ( compilerExe ) => new URL ( '../' , compilerExe ) . href ; export const getStencilModuleUrl = ( compilerExe , p ) => { p = normalizePath ( p ) ; let parts = p . split ( '/' ) ; const nmIndex = parts . lastIndexOf ( 'node_modules' ) ; if ( nmIndex > - 1 && nmIndex < parts . length - 1 ) { parts = parts . slice ( nmIndex + 1 ) ; if ( parts [ 0 ] . startsWith ( '@' ) ) { parts = parts . slice ( 2 ) ; } else { parts = parts . slice ( 1 ) ; } p = parts . join ( '/' ) ; } return new URL ( './' + p , getStencilRootUrl ( compilerExe ) ) . href ; } ; export const getStencilInternalDtsUrl = ( compilerExe ) => getStencilModuleUrl ( compilerExe , 'internal/index.d.ts' ) ; export const getCommonDirUrl = ( sys , pkgVersions , dirPath , fileName ) => getNodeModuleFetchUrl ( sys , pkgVersions , dirPath ) + '/' + fileName ; export const getNodeModuleFetchUrl = ( sys , pkgVersions , filePath ) => { filePath = normalizePath ( filePath ) ; let pathParts = filePath . split ( '/' ) . filter ( ( p ) => p . length ) ; const nmIndex = pathParts . lastIndexOf ( 'node_modules' ) ; if ( nmIndex > - 1 && nmIndex < pathParts . length - 1 ) { pathParts = pathParts . slice ( nmIndex + 1 ) ; } let moduleId = pathParts . shift ( ) ; if ( moduleId . startsWith ( '@' ) ) { moduleId += '/' + pathParts . shift ( ) ; } const path = pathParts . join ( '/' ) ; if ( moduleId === '@stencil/core' ) { const compilerExe = sys . getCompilerExecutingPath ( ) ; return getStencilModuleUrl ( compilerExe , path ) ; } return sys . getRemoteModuleUrl ( { moduleId , version : pkgVersions . get ( moduleId ) , path , } ) ; } ; export const skipFilePathFetch = ( filePath ) => { if ( isTsFile ( filePath ) || isTsxFile ( filePath ) ) { return true ; } const pathParts = filePath . split ( '/' ) ; const secondToLast = pathParts [ pathParts . length - 2 ] ; const lastPart = pathParts [ pathParts . length - 1 ] ; if ( secondToLast === 'node_modules' && isCommonDirModuleFile ( lastPart ) ) { return true ; } return false ; } ; export const skipUrlFetch = ( url ) => knownUrlSkips . some ( ( knownSkip ) => url . endsWith ( knownSkip ) ) ; const knownUrlSkips = [ '/@stencil/core/internal.js' , '/@stencil/core/internal.json' , '/@stencil/core/internal.mjs' , '/@stencil/core/internal/stencil-core.js/index.json' , '/@stencil/core/internal/stencil-core.js.json' , '/@stencil/core/internal/stencil-core.js/package.json' , '/@stencil/core.js' , '/@stencil/core.json' , '/@stencil/core.mjs' , '/@stencil/core.css' , '/@stencil/core/index.js' , '/@stencil/core/index.json' , '/@stencil/core/index.mjs' , '/@stencil/core/index.css' , '/@stencil/package.json' , ] ;