Skip to content

Commit dea56be

Browse files
committed
fix(dist): ensure src dts files not emitted still get shipped in dist
Closes #1797
1 parent 7cd28ca commit dea56be

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/compiler/transpile/run-program.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type * as d from '../../declarations';
2-
import { basename, join } from 'path';
2+
import { basename, join, relative } from 'path';
33
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators';
44
import { generateAppTypes } from '../types/generate-app-types';
55
import { getComponentsFromModules, isOutputTargetDistTypes } from '../output-targets/output-utils';
@@ -11,7 +11,12 @@ import { updateModule } from '../transformers/static-to-meta/parse-static';
1111
import { updateStencilTypesImports } from '../types/stencil-types';
1212
import { validateTranspiledComponents } from './validate-components';
1313

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+
) => {
1520
const tsSyntactic = loadTypeScriptDiagnostics(tsBuilder.getSyntacticDiagnostics());
1621
const tsGlobal = loadTypeScriptDiagnostics(tsBuilder.getGlobalDiagnostics());
1722
const tsOptions = loadTypeScriptDiagnostics(tsBuilder.getOptionsDiagnostics());
@@ -27,12 +32,15 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
2732

2833
const tsTypeChecker = tsProgram.getTypeChecker();
2934
const typesOutputTarget = config.outputTargets.filter(isOutputTargetDistTypes);
35+
const emittedDts: string[] = [];
3036
const emitCallback: ts.WriteFileCallback = (emitFilePath, data, _w, _e, tsSourceFiles) => {
3137
if (emitFilePath.endsWith('.js')) {
3238
updateModule(config, compilerCtx, buildCtx, tsSourceFiles[0], data, emitFilePath, tsTypeChecker, null);
3339
} 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);
3542

43+
emittedDts.push(srcDtsPath);
3644
typesOutputTarget.forEach(o => {
3745
const distPath = join(o.typesDir, relativeEmitFilepath);
3846
data = updateStencilTypesImports(o.typesDir, distPath, data);
@@ -67,6 +75,29 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
6775
return true;
6876
}
6977

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+
70101
if (config.validateTypes) {
71102
const tsSemantic = loadTypeScriptDiagnostics(tsBuilder.getSemanticDiagnostics());
72103
if (config.devMode) {
@@ -85,7 +116,6 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,
85116

86117
const getRelativeDts = (config: d.Config, srcPath: string, emitDtsPath: string) => {
87118
const parts: string[] = [];
88-
srcPath = normalizePath(srcPath);
89119
for (let i = 0; i < 30; i++) {
90120
if (config.srcDir === srcPath) {
91121
break;

test/end-to-end/src/app-root/app-root.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import _ from 'lodash';
33
import _es from 'lodash-es';
44
import videojs from 'video.js';
55
import { css } from 'linaria';
6+
import { MeString } from './interfaces';
67

78
const linariaCss = css`
89
background-color: rgba(0, 0, 255, 0.1);
@@ -30,7 +31,7 @@ function format(target: any, propertyKey: string) {
3031
export class AppRoot {
3132
@format something = '12';
3233
@State() first: string;
33-
@State() last: string;
34+
@State() last: MeString;
3435

3536
componentWillLoad() {
3637
const url = new URL(window.location.href);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MeString = string;

test/end-to-end/test-end-to-end-dist.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ JSON.parse(fs.readFileSync(path.join(collectionDir, 'collection-manifest.json'),
2626
const typesDir = path.join(distDir, 'types');
2727
fs.accessSync(path.join(typesDir, 'components.d.ts'));
2828
fs.accessSync(path.join(typesDir, 'stencil-public-runtime.d.ts'));
29+
fs.accessSync(path.join(typesDir, 'app-root', 'app-root.d.ts'));
30+
fs.accessSync(path.join(typesDir, 'app-root', 'interfaces.d.ts'));
2931
fs.accessSync(path.join(typesDir, 'car-list', 'car-data.d.ts'));
3032
fs.accessSync(path.join(typesDir, 'car-list', 'car-list.d.ts'));
3133

0 commit comments

Comments
 (0)