1
1
import type * as d from '../../declarations' ;
2
- import { basename , join } from 'path' ;
2
+ import { basename , join , relative } from 'path' ;
3
3
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators' ;
4
4
import { generateAppTypes } from '../types/generate-app-types' ;
5
5
import { getComponentsFromModules , isOutputTargetDistTypes } from '../output-targets/output-utils' ;
@@ -11,7 +11,12 @@ import { updateModule } from '../transformers/static-to-meta/parse-static';
11
11
import { updateStencilTypesImports } from '../types/stencil-types' ;
12
12
import { validateTranspiledComponents } from './validate-components' ;
13
13
14
- export const runTsProgram = async ( config : d . Config , compilerCtx : d . CompilerCtx , buildCtx : d . BuildCtx , tsBuilder : ts . BuilderProgram ) => {
14
+ export const runTsProgram = async (
15
+ config : d . Config ,
16
+ compilerCtx : d . CompilerCtx ,
17
+ buildCtx : d . BuildCtx ,
18
+ tsBuilder : ts . BuilderProgram ,
19
+ ) => {
15
20
const tsSyntactic = loadTypeScriptDiagnostics ( tsBuilder . getSyntacticDiagnostics ( ) ) ;
16
21
const tsGlobal = loadTypeScriptDiagnostics ( tsBuilder . getGlobalDiagnostics ( ) ) ;
17
22
const tsOptions = loadTypeScriptDiagnostics ( tsBuilder . getOptionsDiagnostics ( ) ) ;
@@ -27,12 +32,15 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
27
32
28
33
const tsTypeChecker = tsProgram . getTypeChecker ( ) ;
29
34
const typesOutputTarget = config . outputTargets . filter ( isOutputTargetDistTypes ) ;
35
+ const emittedDts : string [ ] = [ ] ;
30
36
const emitCallback : ts . WriteFileCallback = ( emitFilePath , data , _w , _e , tsSourceFiles ) => {
31
37
if ( emitFilePath . endsWith ( '.js' ) ) {
32
38
updateModule ( config , compilerCtx , buildCtx , tsSourceFiles [ 0 ] , data , emitFilePath , tsTypeChecker , null ) ;
33
39
} else if ( emitFilePath . endsWith ( '.d.ts' ) ) {
34
- const relativeEmitFilepath = getRelativeDts ( config , tsSourceFiles [ 0 ] . fileName , emitFilePath ) ;
40
+ const srcDtsPath = normalizePath ( tsSourceFiles [ 0 ] . fileName ) ;
41
+ const relativeEmitFilepath = getRelativeDts ( config , srcDtsPath , emitFilePath ) ;
35
42
43
+ emittedDts . push ( srcDtsPath ) ;
36
44
typesOutputTarget . forEach ( o => {
37
45
const distPath = join ( o . typesDir , relativeEmitFilepath ) ;
38
46
data = updateStencilTypesImports ( o . typesDir , distPath , data ) ;
@@ -67,6 +75,29 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
67
75
return true ;
68
76
}
69
77
78
+ if ( typesOutputTarget . length > 0 ) {
79
+ // copy src dts files that do not get emitted by the compiler
80
+ // but we still want to ship them in the dist directory
81
+ const srcRootDtsFiles = tsProgram
82
+ . getRootFileNames ( )
83
+ . filter ( f => f . endsWith ( '.d.ts' ) && ! f . endsWith ( 'components.d.ts' ) )
84
+ . map ( normalizePath )
85
+ . filter ( f => ! emittedDts . includes ( f ) )
86
+ . map ( srcRootDtsFilePath => {
87
+ const relativeEmitFilepath = relative ( config . srcDir , srcRootDtsFilePath ) ;
88
+ return Promise . all (
89
+ typesOutputTarget . map ( async o => {
90
+ const distPath = join ( o . typesDir , relativeEmitFilepath ) ;
91
+ let dtsContent = await compilerCtx . fs . readFile ( srcRootDtsFilePath ) ;
92
+ dtsContent = updateStencilTypesImports ( o . typesDir , distPath , dtsContent ) ;
93
+ await compilerCtx . fs . writeFile ( distPath , dtsContent ) ;
94
+ } ) ,
95
+ ) ;
96
+ } ) ;
97
+
98
+ await Promise . all ( srcRootDtsFiles ) ;
99
+ }
100
+
70
101
if ( config . validateTypes ) {
71
102
const tsSemantic = loadTypeScriptDiagnostics ( tsBuilder . getSemanticDiagnostics ( ) ) ;
72
103
if ( config . devMode ) {
@@ -85,7 +116,6 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
85
116
86
117
const getRelativeDts = ( config : d . Config , srcPath : string , emitDtsPath : string ) => {
87
118
const parts : string [ ] = [ ] ;
88
- srcPath = normalizePath ( srcPath ) ;
89
119
for ( let i = 0 ; i < 30 ; i ++ ) {
90
120
if ( config . srcDir === srcPath ) {
91
121
break ;
0 commit comments