Skip to content

Commit

Permalink
fix(ts): correct entry point (algolia#4829)
Browse files Browse the repository at this point in the history
The entry point used to be:

```ts
export { default } from './index.es';
export * from './connectors';
export * from './helpers';
export * from './types';
export * from './widgets';
export * from './lib/routers';
```

While really the entry point should be the same as index.es.ts, as otherwise the js bundle doesn't match the declaration.

This commit fixes that by no longer generating an entry point, and renaming index.es.d.ts to index.d.ts in the built files.

In the same commit I also move the declaration-related configuration files to the subfolder to make the root a little cleaner
  • Loading branch information
Haroenv authored Aug 16, 2021
1 parent d06f82c commit 24a45f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
10 changes: 6 additions & 4 deletions api-extractor.json → scripts/build/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",

"projectFolder": "../../",
"mainEntryPointFilePath": "<projectFolder>/es/index.d.ts",
"dtsRollup": {
"enabled": false
},

"apiReport": {
"enabled": false
},
Expand Down Expand Up @@ -40,9 +46,5 @@
"logLevel": "none"
}
}
},
"mainEntryPointFilePath": "es/index.d.ts",
"dtsRollup": {
"enabled": false
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"extends": "./tsconfig",
"extends": "../../tsconfig",
"compilerOptions": {
"stripInternal": true,
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
"allowJs": true
},
"include": ["src", "./global.d.ts"],
"exclude": ["**/__tests__"]
"include": ["../../src", "../../global.d.ts"],
"exclude": ["../../**/__tests__"]
}
36 changes: 10 additions & 26 deletions scripts/build/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

/* eslint-disable import/no-commonjs, no-console */

const fs = require('fs');
const path = require('path');
const shell = require('shelljs');

console.log(`Compiling definitions...`);

shell.exec(`tsc -p tsconfig.declaration.json --outDir es/`);
shell.exec(
`tsc -p ${path.join(__dirname, 'tsconfig.declaration.json')} --outDir es/`
);

// replace block ts-ignore comments with line ones to support TS < 3.9
shell.sed(
Expand All @@ -18,36 +19,19 @@ shell.sed(
path.join(__dirname, '../../es/**/*.d.ts')
);

// expose only the es entry point, not the umd entry point
shell.mv(
path.join(__dirname, '../../es/index.es.d.ts'),
path.join(__dirname, '../../es/index.d.ts')
);

console.log();
console.log(`Validating definitions...`);

const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor');

const publicExports = [
// 'components' -> does not contains index.d.ts yet
'connectors',
// 'lib', -> Api extractor "import * as ___ from ___;" is not supported yet for local files
// 'middleware',
'helpers',
'types',
'widgets', // -> It does not compile as WidgetFactory is not imported in all files
'lib/routers',
];

fs.writeFileSync(
path.join(__dirname, '../../', 'es/index.d.ts'),
[
`export { default } from './index.es';`,
...publicExports
.map(publicExport => `./${publicExport}`)
.map(exportedFile => {
return `export * from '${exportedFile}';`;
}),
].join('\r\n')
);

const extractorConfig = ExtractorConfig.loadFileAndPrepare(
path.resolve(path.join(__dirname, '../../', 'api-extractor.json'))
path.resolve(path.join(__dirname, 'api-extractor.json'))
);

const result = Extractor.invoke(extractorConfig, {
Expand Down

0 comments on commit 24a45f9

Please sign in to comment.