From afdef25f332e015bc2709a38795c7da810e0e890 Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Tue, 11 Dec 2018 14:40:35 -0700 Subject: [PATCH 1/2] Validate eui.d.ts after generating during build --- scripts/compile-eui.js | 4 +++- scripts/dtsgenerator.js | 17 ++++++++++++++--- tsconfig-builttypes.json | 6 ++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tsconfig-builttypes.json diff --git a/scripts/compile-eui.js b/scripts/compile-eui.js index 64e72ef463f..53bb3322c21 100755 --- a/scripts/compile-eui.js +++ b/scripts/compile-eui.js @@ -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' }); + // 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 diff --git a/scripts/dtsgenerator.js b/scripts/dtsgenerator.js index eaca302a49d..523e839cdf3 100644 --- a/scripts/dtsgenerator.js +++ b/scripts/dtsgenerator.js @@ -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', @@ -24,10 +23,22 @@ const generator = dtsGenerator({ 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`; + const importTargetDTs = `${path.resolve(baseDir, path.dirname(params.currentModuleId), params.importedModuleId)}.d.ts`; // if importing an `index` file - const isModuleIndex = path.basename(params.importedModuleId) === 'index'; + let isModuleIndex = false; + 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 diff --git a/tsconfig-builttypes.json b/tsconfig-builttypes.json new file mode 100644 index 00000000000..fd38431308d --- /dev/null +++ b/tsconfig-builttypes.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "eui.d.ts" + ] +} From b0a33c18128e30a0e0b2a02658eb88b725dc4bcd Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Tue, 11 Dec 2018 14:53:02 -0700 Subject: [PATCH 2/2] remove unused code path --- scripts/dtsgenerator.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/scripts/dtsgenerator.js b/scripts/dtsgenerator.js index 523e839cdf3..321f281186d 100644 --- a/scripts/dtsgenerator.js +++ b/scripts/dtsgenerator.js @@ -22,9 +22,6 @@ 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(baseDir, path.dirname(params.currentModuleId), params.importedModuleId)}.d.ts`; - // if importing an `index` file let isModuleIndex = false; if (path.basename(params.importedModuleId) === 'index') { @@ -40,10 +37,7 @@ const generator = dtsGenerator({ } } - 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) { // importing an `index` file, in `resolveModuleId` above we change those modules to '@elastic/eui' return '@elastic/eui'; } else {