import fs from 'fs-extra' ; import type { BuildOptions } from '../utils/options' ; import { cleanDts } from '../utils/bundle-dts' ; import { internalAppData } from './internal-app-data' ; import { internalClient } from './internal-platform-client' ; import { internalHydrate } from './internal-platform-hydrate' ; import { internalTesting } from './internal-platform-testing' ; import { join } from 'path' ; import { writePkgJson } from '../utils/write-pkg-json' ; export async function internal ( opts ) { const inputInternalDir = join ( opts . buildDir , 'internal' ) ; await fs . emptyDir ( opts . output . internalDir ) ; await copyStencilInternalDts ( opts , opts . output . internalDir ) ; await copyStencilCoreEntry ( opts ) ; await fs . copyFile ( join ( inputInternalDir , 'default.js' ) , join ( opts . output . internalDir , 'index.js' ) ) ; writePkgJson ( opts , opts . output . internalDir , { name : '@stencil/core/internal' , description : 'Stencil internals only to be imported by the Stencil Compiler. Breaking changes can and will happen at any time.' , main : 'index.js' , types : 'index.d.ts' , sideEffects : false , } ) ; const clientPlatformBundle = await internalClient ( opts ) ; const hydratePlatformBundles = await internalHydrate ( opts ) ; const testingPlatform = await internalTesting ( opts ) ; return [ ... clientPlatformBundle , ... hydratePlatformBundles , ... testingPlatform , await internalAppData ( opts ) ] ; } async function copyStencilInternalDts ( opts , outputInternalDir ) { const declarationsInputDir = join ( opts . buildDir , 'declarations' ) ; const indexDtsSrcPath = join ( declarationsInputDir , 'index.d.ts' ) ; const indexDtsDestPath = join ( outputInternalDir , 'index.d.ts' ) ; let indexDts = cleanDts ( await fs . readFile ( indexDtsSrcPath , 'utf8' ) ) ; indexDts = prependExtModules ( indexDts ) ; await fs . writeFile ( indexDtsDestPath , indexDts ) ; const privateDtsSrcPath = join ( declarationsInputDir , 'stencil-private.d.ts' ) ; const privateDtsDestPath = join ( outputInternalDir , 'stencil-private.d.ts' ) ; const privateDts = cleanDts ( await fs . readFile ( privateDtsSrcPath , 'utf8' ) ) ; await fs . writeFile ( privateDtsDestPath , privateDts ) ; const compilerDtsSrcPath = join ( declarationsInputDir , 'stencil-public-compiler.d.ts' ) ; const compilerDtsDestPath = join ( outputInternalDir , 'stencil-public-compiler.d.ts' ) ; const compilerDts = cleanDts ( await fs . readFile ( compilerDtsSrcPath , 'utf8' ) ) ; await fs . writeFile ( compilerDtsDestPath , compilerDts ) ; const docsDtsSrcPath = join ( declarationsInputDir , 'stencil-public-docs.d.ts' ) ; const docsDtsDestPath = join ( outputInternalDir , 'stencil-public-docs.d.ts' ) ; const docsDts = cleanDts ( await fs . readFile ( docsDtsSrcPath , 'utf8' ) ) ; await fs . writeFile ( docsDtsDestPath , docsDts ) ; const runtimeDtsSrcPath = join ( declarationsInputDir , 'stencil-public-runtime.d.ts' ) ; const runtimeDtsDestPath = join ( outputInternalDir , 'stencil-public-runtime.d.ts' ) ; const runtimeDts = cleanDts ( await fs . readFile ( runtimeDtsSrcPath , 'utf8' ) ) ; await fs . writeFile ( runtimeDtsDestPath , runtimeDts ) ; const srcExtModuleOutput = join ( opts . srcDir , 'declarations' , 'stencil-ext-modules.d.ts' ) ; const dstExtModuleOutput = join ( outputInternalDir , 'stencil-ext-modules.d.ts' ) ; await fs . copyFile ( srcExtModuleOutput , dstExtModuleOutput ) ; } function prependExtModules ( content ) { return `/// <reference path="./stencil-ext-modules.d.ts" />\n` + content ; } async function copyStencilCoreEntry ( opts ) { const stencilCoreSrcDir = join ( opts . srcDir , 'internal' , 'stencil-core' ) ; const stencilCoreDstDir = join ( opts . output . internalDir , 'stencil-core' ) ; await fs . ensureDir ( stencilCoreDstDir ) ; await fs . copy ( stencilCoreSrcDir , stencilCoreDstDir ) ; }