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

Validate eui.d.ts after generating during build #1366

Merged
merged 2 commits into from
Dec 12, 2018
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
4 changes: 3 additions & 1 deletion scripts/compile-eui.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function compileLib() {

// Use `tsc` to emit typescript declaration files for .ts files
console.log('Generating typescript definitions file');
execSync(`node ${path.resolve(__dirname, 'dtsgenerator.js')}`);
execSync(`node ${path.resolve(__dirname, 'dtsgenerator.js')}`, { stdio: 'inherit' });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added stdio: 'inherit' here to give more visibility if any errors occur

// validate the generated eui.d.ts doesn't contain errors
execSync(`tsc --noEmit -p tsconfig-builttypes.json`, { stdio: 'inherit' });
console.log(chalk.green('✔ Finished generating definitions'));

// Also copy over SVGs. Babel has a --copy-files option but that brings over
Expand Down
23 changes: 14 additions & 9 deletions scripts/dtsgenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const dtsGenerator = require('dts-generator').default;

const baseDir = path.resolve(__dirname, '..');
const srcDir = path.resolve(baseDir, 'src');

const generator = dtsGenerator({
name: '@elastic/eui',
Expand All @@ -23,16 +22,22 @@ const generator = dtsGenerator({
const isRelativeImport = params.importedModuleId[0] === '.';

if (isRelativeImport) {
// path to the import target, assuming it's a `.d.ts` file
const importTargetDTs = `${path.resolve(srcDir, path.dirname(params.currentModuleId), params.importedModuleId)}.d.ts`;

// if importing an `index` file
const isModuleIndex = path.basename(params.importedModuleId) === 'index';
let isModuleIndex = false;
Copy link
Contributor Author

@chandlerprall chandlerprall Dec 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our structure of
icon/
index.ts
icon.tsx

means that index.ts should import from icon and export into @elastic/eui, but anything pointing to icon or icon/index should import from @elastic/eui (as the index file's exports target that namespace). Previously, this only worked correctly for import { IconType } from '../icon/index' and this change adds support for import { IconType } from '../icon'

if (path.basename(params.importedModuleId) === 'index') {
isModuleIndex = true;
} else {
const basePath = path.resolve(baseDir, path.dirname(params.currentModuleId));
// check if the imported module resolves to ${importedModuleId}/index.ts
if (!fs.existsSync(path.resolve(basePath, `${params.importedModuleId}.ts`))) {
// not pointing at ${importedModuleId}.ts, check if it's a directory with `index.ts`
if (fs.existsSync(path.resolve(basePath, `${params.importedModuleId}/index.ts`))) {
isModuleIndex = true;
}
}
}

if (fs.existsSync(importTargetDTs)) {
// the import target is a `.d.ts` file which means it is hand-crafted and already added to the right places, don't modify
return path.join('@elastic/eui', params.importedModuleId);
} else if (isModuleIndex) {
if (isModuleIndex) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fs.existsSync(importTargetDTs) was actually never hit, removed it

// importing an `index` file, in `resolveModuleId` above we change those modules to '@elastic/eui'
return '@elastic/eui';
} else {
Expand Down
6 changes: 6 additions & 0 deletions tsconfig-builttypes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"include": [
"eui.d.ts"
]
}