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

TS types output #1304

Merged
merged 5 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ tmp/
dist/
lib/
es/
types/
.idea
.vscode/
.DS_Store
.eslintcache
.yo-rc.json
npm-debug.log
yarn-error.log
eui.d.ts
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ tmp/
wiki/
generator-eui/
test/
types/
src/
src-docs/
src-framer/
.nvmrc
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions scripts/compile-eui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
59 changes: 59 additions & 0 deletions scripts/dtsgenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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');

const generator = 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;
}
},
});

// strip any `/// <reference` lines from the generated eui.d.ts
generator.then(() => {
const defsFilePath = path.resolve(baseDir, 'eui.d.ts');
fs.writeFileSync(
defsFilePath,
fs.readFileSync(defsFilePath).toString().replace(/\/\/\/\W+<reference.*/g, '')
);
});
20 changes: 11 additions & 9 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@
"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,

// 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",

Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,11 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

[email protected]:
version "3.3.3"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.3.3.tgz#cf96a45d77b9a97a43c46a365c4619f62bf976d0"
integrity sha1-z5akXXe5qXpDxGo2XEYZ9iv5dtA=

[email protected]:
version "3.4.6"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.6.tgz#01da8d821d87813d158967e743d5fe6c62cf8c0f"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -5390,6 +5404,17 @@ glob-parent@^3.1.0:
is-glob "^3.1.0"
path-dirname "^1.0.0"

[email protected]:
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"

[email protected], 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"
Expand Down