From 29c588b1fbbc9887060965be72f36938fadadf8c Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Mon, 12 Nov 2018 13:45:52 -0700 Subject: [PATCH 1/4] output a single eui.d.ts ts defs file, skip publishing src directory --- .gitignore | 2 ++ .npmignore | 2 ++ package.json | 3 ++- scripts/compile-eui.js | 3 +++ scripts/dtsgenerator.js | 50 +++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 20 +++++++++-------- yarn.lock | 25 +++++++++++++++++++++ 7 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 scripts/dtsgenerator.js diff --git a/.gitignore b/.gitignore index 15e8800d94e..92b2f9a8c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ tmp/ dist/ lib/ es/ +types/ .idea .vscode/ .DS_Store @@ -17,3 +18,4 @@ es/ .yo-rc.json npm-debug.log yarn-error.log +eui.d.ts diff --git a/.npmignore b/.npmignore index 73e80c75274..85c0dea1e1f 100644 --- a/.npmignore +++ b/.npmignore @@ -5,6 +5,8 @@ tmp/ wiki/ generator-eui/ test/ +types/ +src/ src-docs/ src-framer/ .nvmrc diff --git a/package.json b/package.json index 12c2c097731..1a8b851c6fe 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "5.0.1", "main": "lib", "module": "es", - "types": "src/index.d.ts", + "types": "eui.d.ts", "postcss": {}, "docker_image": "node:8", "scripts": { @@ -87,6 +87,7 @@ "circular-dependency-plugin": "^5.0.2", "css-loader": "^0.28.7", "cssnano": "^4.0.5", + "dts-generator": "^2.1.0", "enzyme": "^3.1.0", "enzyme-adapter-react-16": "^1.0.2", "enzyme-to-json": "^3.3.0", diff --git a/scripts/compile-eui.js b/scripts/compile-eui.js index 215ca3c991d..cc1173f25eb 100755 --- a/scripts/compile-eui.js +++ b/scripts/compile-eui.js @@ -2,6 +2,7 @@ const { execSync } = require('child_process'); const chalk = require('chalk'); const shell = require('shelljs'); const glob = require('glob'); +const path = require('path'); function compileLib() { shell.mkdir('-p', 'lib/components/icon/assets/tokens', 'lib/services', 'lib/test'); @@ -16,6 +17,8 @@ function compileLib() { console.log(chalk.green('✔ Finished compiling src/')); + execSync(`node ${path.resolve(__dirname, 'dtsgenerator.js')}`); + // Also copy over SVGs. Babel has a --copy-files option but that brings over // all kinds of things we don't want into the lib folder. shell.mkdir('-p', 'lib/components/icon/assets'); diff --git a/scripts/dtsgenerator.js b/scripts/dtsgenerator.js new file mode 100644 index 00000000000..40500821ed0 --- /dev/null +++ b/scripts/dtsgenerator.js @@ -0,0 +1,50 @@ +const fs = require('fs'); +const path = require('path'); +const dtsGenerator = require('dts-generator').default; + +const baseDir = path.resolve(__dirname, '..'); +const srcDir = path.resolve(baseDir, 'src'); + +dtsGenerator({ + name: '@elastic/eui', + project: baseDir, + out: 'eui.d.ts', + resolveModuleId(params) { + if (path.basename(params.currentModuleId) === 'index') { + // this module is exporting from an `index(.d)?.ts` file, declare its exports straight to @elastic/eui module + return '@elastic/eui'; + } else { + // otherwise export as the module's path relative to the @elastic/eui namespace + return path.join('@elastic/eui', params.currentModuleId); + } + }, + resolveModuleImport(params) { + // only intercept relative imports (don't modify node-modules references) + 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'; + + 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) { + // importing an `index` file, in `resolveModuleId` above we change those modules to '@elastic/eui' + return '@elastic/eui'; + } else { + // importing from a non-index TS source file, keep the import path but re-scope it to '@elastic/eui' namespace + return path.join( + '@elastic/eui', + path.dirname(params.currentModuleId), + params.importedModuleId + ); + } + } else { + return params.importedModuleId; + } + }, +}); diff --git a/tsconfig.json b/tsconfig.json index f0c12ff0e67..e93c7646d2a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,14 @@ "compilerOptions": { "baseUrl": ".", - // don't write output, only perform type checking - "noEmit": true, + // necessary to output declaration files relative to the correct path + "rootDir": "./src", + + // emit files to the `types` dir, these are ignored by everything but TS needs _somewhere_ to emit + "outDir": "types", + + // generate declaration files + "declaration": true, // don't allow un-used variables "noUnusedLocals": true, @@ -11,19 +17,15 @@ // Enables all strict type checking options. "strict": true, - // Use commonjs for node, overridden in webpack to keep import statements - // to maintain support for things like `await import()` - "module": "commonjs", + // Output as ES Modules & esnext code, webpack will handle further conversion + "module": "es6", + "target": "esnext", // Allows default imports from modules with no default export. This does not affect code emit, just type checking. // We have to enable this option explicitly since `esModuleInterop` doesn't enable it automatically when ES2015 or // ESNext module format is used. "allowSyntheticDefaultImports": true, - // Node 8 should support everything output by esnext, we override this - // in webpack with loader-level compiler options - "target": "esnext", - // Support .tsx files and transform JSX into calls to React.createElement "jsx": "react", diff --git a/yarn.lock b/yarn.lock index b0a6e55ca5f..a2fad7d7298 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1821,6 +1821,11 @@ block-stream@*: dependencies: inherits "~2.0.0" +bluebird@3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.3.3.tgz#cf96a45d77b9a97a43c46a365c4619f62bf976d0" + integrity sha1-z5akXXe5qXpDxGo2XEYZ9iv5dtA= + bluebird@3.4.6: version "3.4.6" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.6.tgz#01da8d821d87813d158967e743d5fe6c62cf8c0f" @@ -3962,6 +3967,15 @@ downgrade-root@^1.0.0: default-uid "^1.0.0" is-root "^1.0.0" +dts-generator@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/dts-generator/-/dts-generator-2.1.0.tgz#039b87a4f5f847b3b8ef00ddee3eb09545decefe" + integrity sha1-A5uHpPX4R7O47wDd7j6wlUXezv4= + dependencies: + bluebird "3.3.3" + glob "7.0.0" + mkdirp "0.5.1" + duplexer2@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1" @@ -5390,6 +5404,17 @@ glob-parent@^3.1.0: is-glob "^3.1.0" path-dirname "^1.0.0" +glob@7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.0.tgz#3b20a357fffcf46bb384aed6f8ae9a647fdb6ac4" + integrity sha1-OyCjV//89GuzhK7W+K6aZH/basQ= + dependencies: + inflight "^1.0.4" + inherits "2" + minimatch "2 || 3" + once "^1.3.0" + path-is-absolute "^1.0.0" + glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@~7.1.1: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" From 4ed4ed7ea253e627f5a8c619b793834ad6237069 Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Tue, 13 Nov 2018 13:02:06 -0700 Subject: [PATCH 2/4] strip /// { + const defsFilePath = path.resolve(baseDir, 'eui.d.ts'); + fs.writeFileSync( + defsFilePath, + fs.readFileSync(defsFilePath).toString().replace(/\/\/\/\W+ Date: Wed, 14 Nov 2018 14:45:35 -0700 Subject: [PATCH 3/4] src dir is still used downstream --- .npmignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.npmignore b/.npmignore index 85c0dea1e1f..5a9c731ccbc 100644 --- a/.npmignore +++ b/.npmignore @@ -6,7 +6,6 @@ wiki/ generator-eui/ test/ types/ -src/ src-docs/ src-framer/ .nvmrc From b7804d0dc2539da99b44f590cead80cc48d7981b Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Thu, 15 Nov 2018 11:52:47 -0700 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77d56449d42..5cef276581b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,7 @@ ## [`master`](https://github.com/elastic/eui/tree/master) - `EuiToken` now exports enumerated constants for `SHAPES` and `COLORS` ([#1301](https://github.com/elastic/eui/pull/1301)) - -No public interface changes since `5.0.1`. +- TypeScript types are now published to a `eui.d.ts` top-level file ([#1304](https://github.com/elastic/eui/pull/1304)) ## [`5.0.1`](https://github.com/elastic/eui/tree/v5.0.1)