Skip to content

Commit

Permalink
fixup! feat(scripts): remove @internal stripping and enable no deps b…
Browse files Browse the repository at this point in the history
…uild needed for api generation
  • Loading branch information
Hotell committed Nov 9, 2022
1 parent 9890008 commit cbfa8bf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions scripts/tasks/api-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function apiExtractor() {
tsConfig,
tsConfigPath,
packageJson,
definitionsRootPath: 'dist/out-tsc/types',
});

config.compiler = compilerConfig;
Expand Down
52 changes: 52 additions & 0 deletions scripts/tasks/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getTsPathAliasesApiExtractorConfig } from './utils';

type DeepPartial<T> = Partial<{ [P in keyof T]: DeepPartial<T[P]> }>;
describe(`utils`, () => {
describe(`#getTsPathAliasesApiExtractorConfig`, () => {
type Options = Parameters<typeof getTsPathAliasesApiExtractorConfig>[0];
function setup(options: DeepPartial<Options> = {}) {
const defaults = {
tsConfig: {
compilerOptions: {
...options.tsConfig?.compilerOptions,
},
},
tsConfigPath: 'tsconfig.lib.json',
packageJson: {
name: '@proj/one',
version: '0.0.1',
main: 'lib/index.js',
dependencies: { ...options.packageJson?.dependencies },
peerDependencies: { ...options.packageJson?.peerDependencies },
},
definitionsRootPath: options.definitionsRootPath ?? 'dist/types',
};

return getTsPathAliasesApiExtractorConfig(defaults as Options);
}

it(`should set compilerOptions`, () => {
const actual = setup();

expect(actual.overrideTsconfig.compilerOptions).toEqual(
expect.objectContaining({ isolatedModules: false, skipLibCheck: false }),
);
});

it(`should override path aliases to emitted declaration files instead of source files`, () => {
const actual = setup({ definitionsRootPath: 'dist/for/types' });

const newPaths = (actual.overrideTsconfig.compilerOptions.paths as unknown) as Record<string, string[]>;

const newPath = Object.values(newPaths)[0][0];
expect(newPath).toMatch(new RegExp('^dist/for/types.+src/index.d.ts$', 'i'));
});

it(`should set allowSyntheticDefaultImports if package has invalid deps/peerDeps`, () => {
const actual = setup({ packageJson: { dependencies: { '@storybook/api': '6.5.0' } } });
expect(actual.overrideTsconfig.compilerOptions).toEqual(
expect.objectContaining({ allowSyntheticDefaultImports: true }),
);
});
});
});
21 changes: 11 additions & 10 deletions scripts/tasks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ function enableAllowSyntheticDefaultImports(options: { pkgJson: PackageJson }) {
return shouldEnable ? { allowSyntheticDefaultImports: true } : null;
}

function createNormalizedTsPaths() {
const rootTsConf = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../tsconfig.base.json'), 'utf-8'),
) as TsConfig;
const paths = (rootTsConf.compilerOptions.paths as unknown) as Record<string, string[]>;

const normalizedPaths = Object.entries(paths).reduce((acc, [pkgName, pathArr]) => {
acc[pkgName] = ['dist/out-tsc/types/' + pathArr[0].replace('index.ts', 'index.d.ts')];
const rootTsConfig = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../../tsconfig.base.json'), 'utf-8'),
) as TsConfig;

function createNormalizedTsPaths(options: { definitionsRootPath: string; rootTsConfig: TsConfig }) {
const paths = (options.rootTsConfig.compilerOptions.paths as unknown) as Record<string, string[]>;

const normalizedPaths = Object.entries(paths).reduce((acc, [pkgName, pathAliases]) => {
acc[pkgName] = [path.join(options.definitionsRootPath, pathAliases[0].replace('index.ts', 'index.d.ts'))];
return acc;
}, {} as typeof paths);

Expand All @@ -57,10 +58,10 @@ export function getTsPathAliasesApiExtractorConfig(options: {
tsConfig: TsConfig;
tsConfigPath: string;
packageJson: PackageJson;
definitionsRootPath: string;
}) {
const normalizedPaths = createNormalizedTsPaths();
const normalizedPaths = createNormalizedTsPaths({ definitionsRootPath: options.definitionsRootPath, rootTsConfig });

// console.log({ paths: normalizedPaths });
/**
* Customized TSConfig that uses `tsconfig.lib.json` as base with some required overrides:
*
Expand Down

0 comments on commit cbfa8bf

Please sign in to comment.