Skip to content

Commit

Permalink
fixup! feat(scripts): implement public api enforcement during build e…
Browse files Browse the repository at this point in the history
…xec on dts level
  • Loading branch information
Hotell committed Jun 16, 2022
1 parent 361394c commit 8ac823a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scripts/tasks/api-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function apiExtractor() {
}

logger.info(`api-extractor: package is using TS path aliases. Overriding TS compiler settings.`);
const compilerConfig = getTsPathAliasesApiExtractorConfig({ tsConfig, tsConfigPath });
const compilerConfig = getTsPathAliasesApiExtractorConfig({ tsConfig, tsConfigPath, packageJson });

config.compiler = compilerConfig;
},
Expand Down
30 changes: 29 additions & 1 deletion scripts/tasks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,33 @@ export function getTsPathAliasesConfig() {
return { tsConfig, isUsingTsSolutionConfigs, tsConfigFile, tsConfigPath, packageJson };
}

export function getTsPathAliasesApiExtractorConfig(options: { tsConfig: TsConfig; tsConfigPath: string }) {
const packagesWithInvalidTypes = [
/**
* @see @storybook/api/dist/ts3.9/lib/stories.d.ts:1:8 - `import React from 'react'`
*/
'@storybook/api',
];

/**
* Some 3rd party packages might ship invalid types for consumers that don't have synthetic default imports enabled
* In that case our package needs to have `allowSyntheticDefaultImports` to pass the TS lib check.
*
* NOTE: This is safe to use on type declaration level for following reasons:
* - it doesn't affect emitted runtime code
* - it doesn't affect our declaration types emit
*/
function enableAllowSyntheticDefaultImports(options: { pkgJson: PackageJson }) {
const dependencies = Object.keys({ ...options.pkgJson.dependencies, ...options.pkgJson.peerDependencies });
const shouldEnable = dependencies.some(dependency => packagesWithInvalidTypes.includes(dependency));

return shouldEnable ? { allowSyntheticDefaultImports: true } : null;
}

export function getTsPathAliasesApiExtractorConfig(options: {
tsConfig: TsConfig;
tsConfigPath: string;
packageJson: PackageJson;
}) {
const rootOffset = offsetFromRoot(path.dirname(options.tsConfigPath.replace(appRootPath, '')));
/**
* This special TSConfig config is all that's needed for api-extractor so it has all type information used for package:
Expand All @@ -35,6 +61,7 @@ export function getTsPathAliasesApiExtractorConfig(options: { tsConfig: TsConfig
*/
...(options.tsConfig.files ? { files: options.tsConfig.files } : null),
compilerOptions: {
...enableAllowSyntheticDefaultImports({ pkgJson: options.packageJson }),
strict: true,
lib: options.tsConfig.compilerOptions.lib,
typeRoots: ['node_modules/@types', `${rootOffset}typings`],
Expand All @@ -60,6 +87,7 @@ interface PackageJson {
scripts?: Record<string, string>;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
}

export interface TsConfig {
Expand Down

0 comments on commit 8ac823a

Please sign in to comment.