Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): reorder tsconfig#path transforms #4501

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/bundle/bundle-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export interface BundleOptions {
*/
externalRuntime?: boolean;
platform: 'client' | 'hydrate' | 'worker';
customTransformers?: TransformerFactory<SourceFile>[];
/**
* A collection of TypeScript transformation factories to apply during the "before" stage of the TypeScript
* compilation pipeline (before built-in .js transformations)
*/
customBeforeTransformers?: TransformerFactory<SourceFile>[];
/**
* 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
4 changes: 3 additions & 1 deletion src/compiler/bundle/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export const typescriptPlugin = (compilerCtx: d.CompilerCtx, bundleOpts: BundleO
const tsResult = ts.transpileModule(mod.staticSourceFileText, {
compilerOptions: config.tsCompilerOptions,
fileName: mod.sourceFilePath,
transformers: { before: bundleOpts.customTransformers },
transformers: {
before: bundleOpts.customBeforeTransformers ?? [],
},
});
const sourceMap: d.SourceMap = tsResult.sourceMapText ? JSON.parse(tsResult.sourceMapText) : null;
return { code: tsResult.outputText, map: sourceMap };
Expand Down
19 changes: 14 additions & 5 deletions src/compiler/output-targets/dist-custom-elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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 { 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 @@ -75,7 +76,7 @@ export const getBundleOptions = (
id: 'customElements',
platform: 'client',
conditionals: getCustomElementsBuildConditionals(config, buildCtx.components),
customTransformers: getCustomElementCustomTransformer(config, compilerCtx, buildCtx.components, outputTarget),
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx, buildCtx.components, outputTarget),
externalRuntime: !!outputTarget.externalRuntime,
inlineWorkers: true,
inputs: {
Expand Down Expand Up @@ -314,7 +315,7 @@ export const generateEntryPoint = (
* @param outputTarget the output target configuration
* @returns a list of transformers to use in the transpilation process
*/
const getCustomElementCustomTransformer = (
const getCustomBeforeTransformers = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
components: d.ComponentCompilerMeta[],
Expand All @@ -329,11 +330,19 @@ const getCustomElementCustomTransformer = (
style: 'static',
styleImportData: 'queryparams',
};
return [
const customBeforeTransformers = [
addDefineCustomElementFunctions(compilerCtx, components, outputTarget),
updateStencilCoreImports(transformOpts.coreImportPath),
];

if (config.transformAliasedImportPaths) {
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
}

customBeforeTransformers.push(
nativeComponentTransform(compilerCtx, transformOpts),
proxyCustomElement(compilerCtx, transformOpts),
removeCollectionImports(compilerCtx),
];
removeCollectionImports(compilerCtx)
);
return customBeforeTransformers;
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { loadRollupDiagnostics } from '@utils';
import * as ts from 'typescript';

import type * as d from '../../../declarations';
import type { BundleOptions } from '../../bundle/bundle-interface';
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 { rewriteAliasedSourceFileImportPaths } from '../../transformers/rewrite-aliased-paths';
import { updateStencilCoreImports } from '../../transformers/update-stencil-core-import';
import { getHydrateBuildConditionals } from './hydrate-build-conditionals';

Expand All @@ -21,7 +23,7 @@ export const bundleHydrateFactory = async (
id: 'hydrate',
platform: 'hydrate',
conditionals: getHydrateBuildConditionals(buildCtx.components),
customTransformers: getHydrateCustomTransformer(config, compilerCtx),
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx),
inlineDynamicImports: true,
inputs: {
'@app-factory-entry': '@app-factory-entry',
Expand All @@ -43,7 +45,19 @@ export const bundleHydrateFactory = async (
return undefined;
};

const getHydrateCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx) => {
/**
* Generate a collection of transformations that are to be applied as a part of the `before` step in the TypeScript
* compilation process.
#
* @param config the Stencil configuration associated with the current build
* @param compilerCtx the current compiler context
* @returns a collection of transformations that should be applied to the source code, intended for the `before` part
* of the pipeline
*/
const getCustomBeforeTransformers = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx
): ts.TransformerFactory<ts.SourceFile>[] => {
const transformOpts: d.TransformOptions = {
coreImportPath: STENCIL_INTERNAL_HYDRATE_ID,
componentExport: null,
Expand All @@ -53,10 +67,15 @@ const getHydrateCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.C
style: 'static',
styleImportData: 'queryparams',
};
const customBeforeTransformers = [updateStencilCoreImports(transformOpts.coreImportPath)];

if (config.transformAliasedImportPaths) {
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
}

return [
updateStencilCoreImports(transformOpts.coreImportPath),
customBeforeTransformers.push(
hydrateComponentTransform(compilerCtx, transformOpts),
removeCollectionImports(compilerCtx),
];
removeCollectionImports(compilerCtx)
);
return customBeforeTransformers;
};
32 changes: 26 additions & 6 deletions src/compiler/output-targets/dist-lazy/lazy-output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { catchError, isOutputTargetDist, isOutputTargetDistLazy, sortBy } from '@utils';
import MagicString from 'magic-string';
import * as ts from 'typescript';

import type * as d from '../../../declarations';
import type { BundleOptions } from '../../bundle/bundle-interface';
Expand All @@ -17,6 +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 { 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 @@ -42,7 +44,7 @@ export const outputLazy = async (
id: 'lazy',
platform: 'client',
conditionals: getLazyBuildConditionals(config, buildCtx.components),
customTransformers: getLazyCustomTransformer(config, compilerCtx),
customBeforeTransformers: getCustomBeforeTransformers(config, compilerCtx),
inlineWorkers: config.outputTargets.some(isOutputTargetDist),
inputs: {
[config.fsNamespace]: LAZY_BROWSER_ENTRY_ID,
Expand Down Expand Up @@ -97,7 +99,19 @@ export const outputLazy = async (
timespan.finish(`${bundleEventMessage} finished`);
};

const getLazyCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx) => {
/**
* Generate a collection of transformations that are to be applied as a part of the `before` step in the TypeScript
* compilation process.
#
* @param config the Stencil configuration associated with the current build
* @param compilerCtx the current compiler context
* @returns a collection of transformations that should be applied to the source code, intended for the `before` part
* of the pipeline
*/
const getCustomBeforeTransformers = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx
): ts.TransformerFactory<ts.SourceFile>[] => {
const transformOpts: d.TransformOptions = {
coreImportPath: STENCIL_CORE_ID,
componentExport: 'lazy',
Expand All @@ -107,11 +121,17 @@ const getLazyCustomTransformer = (config: d.ValidatedConfig, compilerCtx: d.Comp
style: 'static',
styleImportData: 'queryparams',
};
return [
updateStencilCoreImports(transformOpts.coreImportPath),
const customBeforeTransformers = [updateStencilCoreImports(transformOpts.coreImportPath)];

if (config.transformAliasedImportPaths) {
customBeforeTransformers.push(rewriteAliasedSourceFileImportPaths);
}

customBeforeTransformers.push(
lazyComponentTransform(compilerCtx, transformOpts),
removeCollectionImports(compilerCtx),
];
removeCollectionImports(compilerCtx)
);
return customBeforeTransformers;
};

/**
Expand Down
32 changes: 15 additions & 17 deletions src/compiler/transpile/run-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +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,
rewriteAliasedSourceFileImportPaths,
} from '../transformers/rewrite-aliased-paths';
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 @@ -67,19 +64,20 @@ export const runTsProgram = async (
};

if (config.transformAliasedImportPaths) {
transformers.before.push(rewriteAliasedSourceFileImportPaths);
// 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 transforming 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.
/**
* 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);
}

Expand Down