Skip to content

Commit 4035540

Browse files
committed
fix(compiler): reorder tsconfig#path transforms
this commit reorders the transformations run for rewriting paths to modules in import statements. previously, modules in import statements would be rewritten based on the contents of `tsconfig.json#paths` before any other transformation was run. this would conflict with the `updateStencilCoreImports` transformation if `@stencil/core` (or any of its valid subpaths) were listed in `tsconfig.json#paths`. this would cause the wrong versions of helpers such as `h` and `Host` to be picked up when a project used the `--prerender` flag on a component that imported either of the above helpers (the `lazy` output target version would be inlined to the output rather than the `hydrate` version). this commit removes the transformation `run-program` and places it (conditionally) in the "before" transformers list _after_ stencil imports are transformed.
1 parent 94eb855 commit 4035540

File tree

6 files changed

+88
-36
lines changed

6 files changed

+88
-36
lines changed

src/compiler/bundle/bundle-interface.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export interface BundleOptions {
1818
*/
1919
externalRuntime?: boolean;
2020
platform: 'client' | 'hydrate' | 'worker';
21-
customTransformers?: TransformerFactory<SourceFile>[];
21+
/**
22+
* A collection of TypeScript transformation factories to apply during the "before" stage of the TypeScript
23+
* compilation pipeline (before built-in .js transformations)
24+
*/
25+
customBeforeTransformers?: TransformerFactory<SourceFile>[];
2226
/**
2327
* This is equivalent to the Rollup `input` configuration option. It's
2428
* an object mapping names to entry points which tells Rollup to bundle

src/compiler/bundle/typescript-plugin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ export const typescriptPlugin = (compilerCtx: d.CompilerCtx, bundleOpts: BundleO
5757
const tsResult = ts.transpileModule(mod.staticSourceFileText, {
5858
compilerOptions: config.tsCompilerOptions,
5959
fileName: mod.sourceFilePath,
60-
transformers: { before: bundleOpts.customTransformers },
60+
transformers: {
61+
before: bundleOpts.customBeforeTransformers ?? [],
62+
},
6163
});
6264
const sourceMap: d.SourceMap = tsResult.sourceMapText ? JSON.parse(tsResult.sourceMapText) : null;
6365
return { code: tsResult.outputText, map: sourceMap };

src/compiler/output-targets/dist-custom-elements/index.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { addDefineCustomElementFunctions } from '../../transformers/component-na
2020
import { proxyCustomElement } from '../../transformers/component-native/proxy-custom-element-function';
2121
import { nativeComponentTransform } from '../../transformers/component-native/tranform-to-native-component';
2222
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
23+
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
2324
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
2425
import { getCustomElementsBuildConditionals } from './custom-elements-build-conditionals';
2526

@@ -75,7 +76,7 @@ export const getBundleOptions = (
7576
id: 'customElements',
7677
platform: 'client',
7778
conditionals: getCustomElementsBuildConditionals(config, buildCtx.components),
78-
customTransformers: getCustomElementCustomTransformer(config, compilerCtx, buildCtx.components, outputTarget),
79+
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx, buildCtx.components, outputTarget),
7980
externalRuntime: !!outputTarget.externalRuntime,
8081
inlineWorkers: true,
8182
inputs: {
@@ -314,7 +315,7 @@ export const generateEntryPoint = (
314315
* @param outputTarget the output target configuration
315316
* @returns a list of transformers to use in the transpilation process
316317
*/
317-
const getCustomElementCustomTransformer = (
318+
const getCustomBeforeTransformers = (
318319
config: d.ValidatedConfig,
319320
compilerCtx: d.CompilerCtx,
320321
components: d.ComponentCompilerMeta[],
@@ -329,11 +330,19 @@ const getCustomElementCustomTransformer = (
329330
style: 'static',
330331
styleImportData: 'queryparams',
331332
};
332-
return [
333+
const customBeforeTransformers = [
333334
addDefineCustomElementFunctions(compilerCtx, components, outputTarget),
334335
updateStencilCoreImports(transformOpts.coreImportPath),
336+
];
337+
338+
if (config.transformAliasedImportPaths) {
339+
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
340+
}
341+
342+
customBeforeTransformers.push(
335343
nativeComponentTransform(compilerCtx, transformOpts),
336344
proxyCustomElement(compilerCtx, transformOpts),
337-
removeCollectionImports(compilerCtx),
338-
];
345+
removeCollectionImports(compilerCtx)
346+
);
347+
return customBeforeTransformers;
339348
};

src/compiler/output-targets/dist-hydrate-script/bundle-hydrate-factory.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { loadRollupDiagnostics } from '@utils';
2+
import * as ts from 'typescript';
23

34
import type * as d from '../../../declarations';
45
import type { BundleOptions } from '../../bundle/bundle-interface';
56
import { bundleOutput } from '../../bundle/bundle-output';
67
import { STENCIL_INTERNAL_HYDRATE_ID } from '../../bundle/entry-alias-ids';
78
import { hydrateComponentTransform } from '../../transformers/component-hydrate/tranform-to-hydrate-component';
89
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
10+
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
911
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
1012
import { getHydrateBuildConditionals } from './hydrate-build-conditionals';
1113

@@ -21,7 +23,7 @@ export const bundleHydrateFactory = async (
2123
id: 'hydrate',
2224
platform: 'hydrate',
2325
conditionals: getHydrateBuildConditionals(buildCtx.components),
24-
customTransformers: getHydrateCustomTransformer(config, compilerCtx),
26+
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx),
2527
inlineDynamicImports: true,
2628
inputs: {
2729
'@app-factory-entry': '@app-factory-entry',
@@ -43,7 +45,19 @@ export const bundleHydrateFactory = async (
4345
return undefined;
4446
};
4547

46-
const getHydrateCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx) => {
48+
/**
49+
* Generate a collection of transformations that are to be applied as a part of the `before` step in the TypeScript
50+
* compilation process.
51+
#
52+
* @param config the Stencil configuration associated with the current build
53+
* @param compilerCtx the current compiler context
54+
* @returns a collection of transformations that should be applied to the source code, intended for the `before` part
55+
* of the pipeline
56+
*/
57+
const getCustomBeforeTransformers = (
58+
config: d.ValidatedConfig,
59+
compilerCtx: d.CompilerCtx
60+
): ts.TransformerFactory<ts.SourceFile>[] => {
4761
const transformOpts: d.TransformOptions = {
4862
coreImportPath: STENCIL_INTERNAL_HYDRATE_ID,
4963
componentExport: null,
@@ -53,10 +67,15 @@ const getHydrateCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.C
5367
style: 'static',
5468
styleImportData: 'queryparams',
5569
};
70+
const customBeforeTransformers = [updateStencilCoreImports(transformOpts.coreImportPath)];
71+
72+
if (config.transformAliasedImportPaths) {
73+
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
74+
}
5675

57-
return [
58-
updateStencilCoreImports(transformOpts.coreImportPath),
76+
customBeforeTransformers.push(
5977
hydrateComponentTransform(compilerCtx, transformOpts),
60-
removeCollectionImports(compilerCtx),
61-
];
78+
removeCollectionImports(compilerCtx)
79+
);
80+
return customBeforeTransformers;
6281
};

src/compiler/output-targets/dist-lazy/lazy-output.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { catchError, isOutputTargetDist, isOutputTargetDistLazy, sortBy } from '@utils';
22
import MagicString from 'magic-string';
3+
import * as ts from 'typescript';
34

45
import type * as d from '../../../declarations';
56
import type { BundleOptions } from '../../bundle/bundle-interface';
@@ -17,6 +18,7 @@ import { generateComponentBundles } from '../../entries/component-bundles';
1718
import { generateModuleGraph } from '../../entries/component-graph';
1819
import { lazyComponentTransform } from '../../transformers/component-lazy/transform-lazy-component';
1920
import { removeCollectionImports } from '../../transformers/remove-collection-imports';
21+
import { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
2022
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
2123
import { generateCjs } from './generate-cjs';
2224
import { generateEsm } from './generate-esm';
@@ -42,7 +44,7 @@ export const outputLazy = async (
4244
id: 'lazy',
4345
platform: 'client',
4446
conditionals: getLazyBuildConditionals(config, buildCtx.components),
45-
customTransformers: getLazyCustomTransformer(config, compilerCtx),
47+
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx),
4648
inlineWorkers: config.outputTargets.some(isOutputTargetDist),
4749
inputs: {
4850
[config.fsNamespace]: LAZY_BROWSER_ENTRY_ID,
@@ -97,7 +99,19 @@ export const outputLazy = async (
9799
timespan.finish(`${bundleEventMessage} finished`);
98100
};
99101

100-
const getLazyCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx) => {
102+
/**
103+
* Generate a collection of transformations that are to be applied as a part of the `before` step in the TypeScript
104+
* compilation process.
105+
#
106+
* @param config the Stencil configuration associated with the current build
107+
* @param compilerCtx the current compiler context
108+
* @returns a collection of transformations that should be applied to the source code, intended for the `before` part
109+
* of the pipeline
110+
*/
111+
const getCustomBeforeTransformers = (
112+
config: d.ValidatedConfig,
113+
compilerCtx: d.CompilerCtx
114+
): ts.TransformerFactory<ts.SourceFile>[] => {
101115
const transformOpts: d.TransformOptions = {
102116
coreImportPath: STENCIL_CORE_ID,
103117
componentExport: 'lazy',
@@ -107,11 +121,17 @@ const getLazyCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.Comp
107121
style: 'static',
108122
styleImportData: 'queryparams',
109123
};
110-
return [
111-
updateStencilCoreImports(transformOpts.coreImportPath),
124+
const customBeforeTransformers = [updateStencilCoreImports(transformOpts.coreImportPath)];
125+
126+
if (config.transformAliasedImportPaths) {
127+
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
128+
}
129+
130+
customBeforeTransformers.push(
112131
lazyComponentTransform(compilerCtx, transformOpts),
113-
removeCollectionImports(compilerCtx),
114-
];
132+
removeCollectionImports(compilerCtx)
133+
);
134+
return customBeforeTransformers;
115135
};
116136

117137
/**

src/compiler/transpile/run-program.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import type * as d from '../../declarations';
66
import { updateComponentBuildConditionals } from '../app-core/app-data';
77
import { resolveComponentDependencies } from '../entries/resolve-component-dependencies';
88
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators';
9-
import {
10-
rewriteAliasedDTSImportPaths,
11-
rewriteAliasedSourceFileImportPaths,
12-
} from '../transformers/rewrite-aliased-paths';
9+
import { rewriteAliasedDTSImportPaths } from '../transformers/rewrite-aliased-paths';
1310
import { updateModule } from '../transformers/static-to-meta/parse-static';
1411
import { generateAppTypes } from '../types/generate-app-types';
1512
import { updateStencilTypesImports } from '../types/stencil-types';
@@ -67,19 +64,20 @@ export const runTsProgram = async (
6764
};
6865

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

0 commit comments

Comments
 (0)