Skip to content

Commit

Permalink
fixup(): ensure the .d.ts file transformations run
Browse files Browse the repository at this point in the history
ensure that file transforms on .d.ts files run - when we transform TS
files via the rollup plugin `TypeScriptPlugin`, we actually don't
generate/save type declaration files, which is why we weren't
generating/transforming properly!
  • Loading branch information
rwaskiewicz committed Jun 22, 2023
1 parent 0975662 commit 91402c8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 106 deletions.
7 changes: 1 addition & 6 deletions src/compiler/bundle/bundle-interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PreserveEntrySignaturesOption } from 'rollup';
import type { Bundle, CustomTransformerFactory, SourceFile, TransformerFactory } from 'typescript';
import type { SourceFile, TransformerFactory } from 'typescript';

import type { BuildConditionals } from '../../declarations';

Expand All @@ -23,11 +23,6 @@ export interface BundleOptions {
* compilation pipeline (before built-in .js transformations)
*/
customBeforeTransformers?: TransformerFactory<SourceFile>[];
/**
* A collection of TypeScript transformation factories to apply during the "afterDeclarations" stage of the TypeScript
* compilation pipeline (after build-in .d.ts transformations)
*/
customAfterDeclarationsTransformers?: (TransformerFactory<Bundle | SourceFile> | CustomTransformerFactory)[];
/**
* This is equivalent to the Rollup `input` configuration option. It's
* an object mapping names to entry points which tells Rollup to bundle
Expand Down
1 change: 0 additions & 1 deletion src/compiler/bundle/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export const typescriptPlugin = (compilerCtx: d.CompilerCtx, bundleOpts: BundleO
fileName: mod.sourceFilePath,
transformers: {
before: bundleOpts.customBeforeTransformers,
afterDeclarations: bundleOpts.customAfterDeclarationsTransformers ?? [],
},
});
const sourceMap: d.SourceMap = tsResult.sourceMapText ? JSON.parse(tsResult.sourceMapText) : null;
Expand Down
34 changes: 1 addition & 33 deletions src/compiler/output-targets/dist-custom-elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import { addDefineCustomElementFunctions } from '../../transformers/component-na
import { proxyCustomElement } from '../../transformers/component-native/proxy-custom-element-function';
import { nativeComponentTransform } from '../../transformers/component-native/tranform-to-native-component';
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
import {
rewriteAliasedDTSImportPaths,
rewriteAliasedSourceFileImportPaths,
} from '../../transformers/rewrite-aliased-paths';
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
import { getCustomElementsBuildConditionals } from './custom-elements-build-conditionals';

Expand Down Expand Up @@ -80,7 +77,6 @@ export const getBundleOptions = (
platform: 'client',
conditionals: getCustomElementsBuildConditionals(config, buildCtx.components),
customBeforeTransformers: getBeforeCustomTransformers(config, compilerCtx, buildCtx.components, outputTarget),
customAfterDeclarationsTransformers: getAfterDeclarationsCustomTransformers(config),
externalRuntime: !!outputTarget.externalRuntime,
inlineWorkers: true,
inputs: {
Expand Down Expand Up @@ -350,31 +346,3 @@ const getBeforeCustomTransformers = (
);
return beforeCustomTransformers;
};

/**
* Generate a collection of transformations that are to be applied as a part of the `afterDeclarations` step in the
* TypeScript compilation process.
*
* TypeScript handles the generation of JS and `.d.ts` files through different pipelines. One (possibly surprising)
* consequence of this is that if you modify a source file using a transformer, it will not automatically result in
* changes to the corresponding `.d.ts` file. Instead, if you want to, for instance, rewrite some import specifiers in
* both the source file _and_ its typedef you'll need to run a transformer for both of them.
*
* See here: https://github.com/itsdouges/typescript-transformer-handbook#transforms
* and here: https://github.com/microsoft/TypeScript/pull/23946
*
* This quirk is not terribly well documented, unfortunately.
*
* @param config the Stencil configuration associated with the current build
* @returns a collection of transformations that should be applied to the source code, intended for the
* `afterDeclarations` part of the pipeline
*/
const getAfterDeclarationsCustomTransformers = (
config: d.ValidatedConfig
): ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] => {
const afterDeclarationTransformers: ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] = [];
if (config.transformAliasedImportPaths) {
afterDeclarationTransformers.push(rewriteAliasedDTSImportPaths);
}
return afterDeclarationTransformers;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { bundleOutput } from '../../bundle/bundle-output';
import { STENCIL_INTERNAL_HYDRATE_ID } from '../../bundle/entry-alias-ids';
import { hydrateComponentTransform } from '../../transformers/component-hydrate/tranform-to-hydrate-component';
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
import {
rewriteAliasedDTSImportPaths,
rewriteAliasedSourceFileImportPaths,
} from '../../transformers/rewrite-aliased-paths';
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
import { getHydrateBuildConditionals } from './hydrate-build-conditionals';

Expand All @@ -27,7 +24,6 @@ export const bundleHydrateFactory = async (
platform: 'hydrate',
conditionals: getHydrateBuildConditionals(buildCtx.components),
customBeforeTransformers: getBeforeCustomTransformers(config, compilerCtx),
customAfterDeclarationsTransformers: getAfterDeclarationsCustomTransformers(config),
inlineDynamicImports: true,
inputs: {
'@app-factory-entry': '@app-factory-entry',
Expand Down Expand Up @@ -83,31 +79,3 @@ const getBeforeCustomTransformers = (
);
return beforeCustomTransformers;
};

/**
* Generate a collection of transformations that are to be applied as a part of the `afterDeclarations` step in the
* TypeScript compilation process.
*
* TypeScript handles the generation of JS and `.d.ts` files through different pipelines. One (possibly surprising)
* consequence of this is that if you modify a source file using a transformer, it will not automatically result in
* changes to the corresponding `.d.ts` file. Instead, if you want to, for instance, rewrite some import specifiers in
* both the source file _and_ its typedef you'll need to run a transformer for both of them.
*
* See here: https://github.com/itsdouges/typescript-transformer-handbook#transforms
* and here: https://github.com/microsoft/TypeScript/pull/23946
*
* This quirk is not terribly well documented, unfortunately.
*
* @param config the Stencil configuration associated with the current build
* @returns a collection of transformations that should be applied to the source code, intended for the
* `afterDeclarations` part of the pipeline
*/
const getAfterDeclarationsCustomTransformers = (
config: d.ValidatedConfig
): ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] => {
const afterDeclarationTransformers: ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] = [];
if (config.transformAliasedImportPaths) {
afterDeclarationTransformers.push(rewriteAliasedDTSImportPaths);
}
return afterDeclarationTransformers;
};
34 changes: 1 addition & 33 deletions src/compiler/output-targets/dist-lazy/lazy-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import { generateComponentBundles } from '../../entries/component-bundles';
import { generateModuleGraph } from '../../entries/component-graph';
import { lazyComponentTransform } from '../../transformers/component-lazy/transform-lazy-component';
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
import {
rewriteAliasedDTSImportPaths,
rewriteAliasedSourceFileImportPaths,
} from '../../transformers/rewrite-aliased-paths';
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
import { generateCjs } from './generate-cjs';
import { generateEsm } from './generate-esm';
Expand All @@ -48,7 +45,6 @@ export const outputLazy = async (
platform: 'client',
conditionals: getLazyBuildConditionals(config, buildCtx.components),
customBeforeTransformers: getBeforeCustomTransformers(config, compilerCtx),
customAfterDeclarationsTransformers: getAfterDeclarationsCustomTransformers(config),
inlineWorkers: config.outputTargets.some(isOutputTargetDist),
inputs: {
[config.fsNamespace]: LAZY_BROWSER_ENTRY_ID,
Expand Down Expand Up @@ -138,34 +134,6 @@ const getBeforeCustomTransformers = (
return beforeCustomTransformers;
};

/**
* Generate a collection of transformations that are to be applied as a part of the `afterDeclarations` step in the
* TypeScript compilation process.
*
* TypeScript handles the generation of JS and `.d.ts` files through different pipelines. One (possibly surprising)
* consequence of this is that if you modify a source file using a transformer, it will not automatically result in
* changes to the corresponding `.d.ts` file. Instead, if you want to, for instance, rewrite some import specifiers in
* both the source file _and_ its typedef you'll need to run a transformer for both of them.
*
* See here: https://github.com/itsdouges/typescript-transformer-handbook#transforms
* and here: https://github.com/microsoft/TypeScript/pull/23946
*
* This quirk is not terribly well documented, unfortunately.
*
* @param config the Stencil configuration associated with the current build
* @returns a collection of transformations that should be applied to the source code, intended for the
* `afterDeclarations` part of the pipeline
*/
const getAfterDeclarationsCustomTransformers = (
config: d.ValidatedConfig
): ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] => {
const afterDeclarationTransformers: ts.TransformerFactory<ts.Bundle | ts.SourceFile>[] = [];
if (config.transformAliasedImportPaths) {
afterDeclarationTransformers.push(rewriteAliasedDTSImportPaths);
}
return afterDeclarationTransformers;
};

/**
* Generate entry modules to be used by the build process by determining how
* modules and components are connected
Expand Down
20 changes: 20 additions & 0 deletions src/compiler/transpile/run-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type * as d from '../../declarations';
import { updateComponentBuildConditionals } from '../app-core/app-data';
import { resolveComponentDependencies } from '../entries/resolve-component-dependencies';
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators';
import { rewriteAliasedDTSImportPaths } from '../transformers/rewrite-aliased-paths';
import { updateModule } from '../transformers/static-to-meta/parse-static';
import { generateAppTypes } from '../types/generate-app-types';
import { updateStencilTypesImports } from '../types/stencil-types';
Expand Down Expand Up @@ -59,8 +60,27 @@ export const runTsProgram = async (

const transformers: ts.CustomTransformers = {
before: [convertDecoratorsToStatic(config, buildCtx.diagnostics, tsTypeChecker)],
afterDeclarations: [],
};

if (config.transformAliasedImportPaths) {
/**
* Generate a collection of transformations that are to be applied as a part of the `afterDeclarations` step in the
* TypeScript compilation process.
*
* TypeScript handles the generation of JS and `.d.ts` files through different pipelines. One (possibly surprising)
* consequence of this is that if you modify a source file using a transformer, it will not automatically result in
* changes to the corresponding `.d.ts` file. Instead, if you want to, for instance, rewrite some import specifiers
* in both the source file _and_ its typedef you'll need to run a transformer for both of them.
*
* See here: https://github.com/itsdouges/typescript-transformer-handbook#transforms
* and here: https://github.com/microsoft/TypeScript/pull/23946
*
* This quirk is not terribly well documented, unfortunately.
*/
transformers.afterDeclarations.push(rewriteAliasedDTSImportPaths);
}

// Emit files that changed
tsBuilder.emit(undefined, emitCallback, undefined, false, transformers);

Expand Down

0 comments on commit 91402c8

Please sign in to comment.