Skip to content

Commit

Permalink
fix(dist): ensure src dts files not emitted still get shipped in dist
Browse files Browse the repository at this point in the history
Closes #1797
  • Loading branch information
adamdbradley committed Aug 27, 2020
1 parent 7cd28ca commit dea56be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
38 changes: 34 additions & 4 deletions src/compiler/transpile/run-program.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as d from '../../declarations';
import { basename, join } from 'path';
import { basename, join, relative } from 'path';
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators';
import { generateAppTypes } from '../types/generate-app-types';
import { getComponentsFromModules, isOutputTargetDistTypes } from '../output-targets/output-utils';
Expand All @@ -11,7 +11,12 @@ import { updateModule } from '../transformers/static-to-meta/parse-static';
import { updateStencilTypesImports } from '../types/stencil-types';
import { validateTranspiledComponents } from './validate-components';

export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, tsBuilder: ts.BuilderProgram) => {
export const runTsProgram = async (
config: d.Config,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
tsBuilder: ts.BuilderProgram,
) => {
const tsSyntactic = loadTypeScriptDiagnostics(tsBuilder.getSyntacticDiagnostics());
const tsGlobal = loadTypeScriptDiagnostics(tsBuilder.getGlobalDiagnostics());
const tsOptions = loadTypeScriptDiagnostics(tsBuilder.getOptionsDiagnostics());
Expand All @@ -27,12 +32,15 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,

const tsTypeChecker = tsProgram.getTypeChecker();
const typesOutputTarget = config.outputTargets.filter(isOutputTargetDistTypes);
const emittedDts: string[] = [];
const emitCallback: ts.WriteFileCallback = (emitFilePath, data, _w, _e, tsSourceFiles) => {
if (emitFilePath.endsWith('.js')) {
updateModule(config, compilerCtx, buildCtx, tsSourceFiles[0], data, emitFilePath, tsTypeChecker, null);
} else if (emitFilePath.endsWith('.d.ts')) {
const relativeEmitFilepath = getRelativeDts(config, tsSourceFiles[0].fileName, emitFilePath);
const srcDtsPath = normalizePath(tsSourceFiles[0].fileName);
const relativeEmitFilepath = getRelativeDts(config, srcDtsPath, emitFilePath);

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

if (typesOutputTarget.length > 0) {
// copy src dts files that do not get emitted by the compiler
// but we still want to ship them in the dist directory
const srcRootDtsFiles = tsProgram
.getRootFileNames()
.filter(f => f.endsWith('.d.ts') && !f.endsWith('components.d.ts'))
.map(normalizePath)
.filter(f => !emittedDts.includes(f))
.map(srcRootDtsFilePath => {
const relativeEmitFilepath = relative(config.srcDir, srcRootDtsFilePath);
return Promise.all(
typesOutputTarget.map(async o => {
const distPath = join(o.typesDir, relativeEmitFilepath);
let dtsContent = await compilerCtx.fs.readFile(srcRootDtsFilePath);
dtsContent = updateStencilTypesImports(o.typesDir, distPath, dtsContent);
await compilerCtx.fs.writeFile(distPath, dtsContent);
}),
);
});

await Promise.all(srcRootDtsFiles);
}

if (config.validateTypes) {
const tsSemantic = loadTypeScriptDiagnostics(tsBuilder.getSemanticDiagnostics());
if (config.devMode) {
Expand All @@ -85,7 +116,6 @@ export const runTsProgram = async (config: d.Config, compilerCtx: d.CompilerCtx,

const getRelativeDts = (config: d.Config, srcPath: string, emitDtsPath: string) => {
const parts: string[] = [];
srcPath = normalizePath(srcPath);
for (let i = 0; i < 30; i++) {
if (config.srcDir === srcPath) {
break;
Expand Down
3 changes: 2 additions & 1 deletion test/end-to-end/src/app-root/app-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import _ from 'lodash';
import _es from 'lodash-es';
import videojs from 'video.js';
import { css } from 'linaria';
import { MeString } from './interfaces';

const linariaCss = css`
background-color: rgba(0, 0, 255, 0.1);
Expand Down Expand Up @@ -30,7 +31,7 @@ function format(target: any, propertyKey: string) {
export class AppRoot {
@format something = '12';
@State() first: string;
@State() last: string;
@State() last: MeString;

componentWillLoad() {
const url = new URL(window.location.href);
Expand Down
1 change: 1 addition & 0 deletions test/end-to-end/src/app-root/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MeString = string;
2 changes: 2 additions & 0 deletions test/end-to-end/test-end-to-end-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ JSON.parse(fs.readFileSync(path.join(collectionDir, 'collection-manifest.json'),
const typesDir = path.join(distDir, 'types');
fs.accessSync(path.join(typesDir, 'components.d.ts'));
fs.accessSync(path.join(typesDir, 'stencil-public-runtime.d.ts'));
fs.accessSync(path.join(typesDir, 'app-root', 'app-root.d.ts'));
fs.accessSync(path.join(typesDir, 'app-root', 'interfaces.d.ts'));
fs.accessSync(path.join(typesDir, 'car-list', 'car-data.d.ts'));
fs.accessSync(path.join(typesDir, 'car-list', 'car-list.d.ts'));

Expand Down

0 comments on commit dea56be

Please sign in to comment.