Skip to content

Commit

Permalink
Update eui.d.ts generator to properly handle nested index files
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall authored and pugnascotia committed Jan 3, 2019
1 parent 49cf167 commit 4f598bd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"url": "https://github.com/elastic/eui.git"
},
"dependencies": {
"@types/lodash": "^4.14.116",
"@types/numeral": "^0.0.25",
"classnames": "^2.2.5",
"core-js": "^2.5.1",
Expand Down Expand Up @@ -74,7 +75,6 @@
"@types/classnames": "^2.2.6",
"@types/enzyme": "^3.1.13",
"@types/jest": "^23.3.9",
"@types/lodash": "^4.14.116",
"@types/react": "^16.3.0",
"@types/react-virtualized": "^9.18.6",
"@types/uuid": "^3.4.4",
Expand Down Expand Up @@ -112,6 +112,7 @@
"eslint-plugin-prettier": "^2.6.0",
"eslint-plugin-react": "^7.4.0",
"file-loader": "^1.1.11",
"findup": "^0.1.5",
"fork-ts-checker-webpack-plugin": "^0.4.4",
"geckodriver": "^1.11.0",
"glob": "^7.1.2",
Expand Down Expand Up @@ -142,6 +143,7 @@
"react-test-renderer": "^16.2.0",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"resolve": "^1.5.0",
"rimraf": "^2.6.2",
"sass-extract": "^2.1.0",
"sass-extract-js": "^0.3.0",
Expand Down
62 changes: 44 additions & 18 deletions scripts/dtsgenerator.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,65 @@
const findup = require('findup');
const resolve = require('resolve');
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');

function hasParentIndex(pathToFile) {
const isIndexFile = path.basename(pathToFile, path.extname(pathToFile)) === 'index';
try {
const fileDirectory = path.dirname(pathToFile);
const parentIndex = findup.sync(
// if this is an index file start looking in its parent directory
isIndexFile ? path.resolve(fileDirectory, '..') : fileDirectory,
'index.ts'
);
// ensure the found file is in the project
return parentIndex.startsWith(baseDir);
} catch (e) {
return false;
}
}

const generator = dtsGenerator({
name: '@elastic/eui',
project: baseDir,
out: 'eui.d.ts',
exclude: ['node_modules/**/*.d.ts', 'src/custom_typings/**/*.d.ts'],
resolveModuleId(params) {
if (path.basename(params.currentModuleId) === 'index') {
if (path.basename(params.currentModuleId) === 'index' && !hasParentIndex(path.resolve(baseDir, params.currentModuleId))) {
// 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);
if (params.currentModuleId.endsWith('/index')) {
return path.join('@elastic/eui', path.dirname(params.currentModuleId));
} else {
return path.join('@elastic/eui', params.currentModuleId);
}
}
},
resolveModuleImport(params) {
// only intercept relative imports (don't modify node-modules references)
const isRelativeImport = params.importedModuleId[0] === '.';
const importFromBaseDir = path.resolve(baseDir, path.dirname(params.currentModuleId));
const isFromEuiSrc = importFromBaseDir.startsWith(srcDir);
const isRelativeImport = isFromEuiSrc && params.importedModuleId[0] === '.';

if (isRelativeImport) {
// if importing an `index` file
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 importing from an `index` file (directly or targeting a directory with an index),
// then if there is no parent index file this should import from @elastic/eui
const importPathTarget = resolve.sync(
params.importedModuleId,
{
basedir: importFromBaseDir,
extensions: ['.ts', '.tsx'],
}
}
);

const isIndexFile = importPathTarget.endsWith('/index.ts');
const isModuleIndex = isIndexFile && !hasParentIndex(importPathTarget);

if (isModuleIndex) {
// importing an `index` file, in `resolveModuleId` above we change those modules to '@elastic/eui'
Expand All @@ -55,11 +78,14 @@ const generator = dtsGenerator({
},
});

// strip any `/// <reference` lines from the generated eui.d.ts
// 1. strip any `/// <reference` lines from the generated eui.d.ts
// 2. replace any import("src/...") declarations to import("@elastic/src/...")
generator.then(() => {
const defsFilePath = path.resolve(baseDir, 'eui.d.ts');
fs.writeFileSync(
defsFilePath,
fs.readFileSync(defsFilePath).toString().replace(/\/\/\/\W+<reference.*/g, '')
fs.readFileSync(defsFilePath).toString()
.replace(/\/\/\/\W+<reference.*/g, '') // 1.
.replace(/import\(\"src\/(.*?)\"\)/g, 'import("@elastic/eui/src/$1")') // 2.
);
});
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,11 @@ colors@^1.1.2:
resolved "https://registry.yarnpkg.com/colors/-/colors-1.3.0.tgz#5f20c9fef6945cb1134260aab33bfbdc8295e04e"
integrity sha512-EDpX3a7wHMWFA7PUHWPHNWqOxIIRSJetuwl0AS5Oi/5FMV8kWm69RTlgm00GKjBO1xFHMtBbL49yRtMMdticBw==

colors@~0.6.0-1:
version "0.6.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-0.6.2.tgz#2423fe6678ac0c5dae8852e5d0e5be08c997abcc"
integrity sha1-JCP+ZnisDF2uiFLl0OW+CMmXq8w=

colors@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"
Expand Down Expand Up @@ -2952,6 +2957,11 @@ commander@^2.9.0, commander@~2.13.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"
integrity sha512-MVuS359B+YzaWqjCL/c+22gfryv+mCBPHAv3zyVI2GN8EY6IRP8VwtasXn8jyyhvvq84R4ImN1OKRtcbIasjYA==

commander@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.1.0.tgz#d121bbae860d9992a3d517ba96f56588e47c6781"
integrity sha1-0SG7roYNmZKj1Re6lvVliOR8Z4E=

commander@~2.17.1:
version "2.17.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"
Expand Down Expand Up @@ -5272,6 +5282,14 @@ find-versions@^1.0.0:
meow "^3.5.0"
semver-regex "^1.0.0"

findup@^0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/findup/-/findup-0.1.5.tgz#8ad929a3393bac627957a7e5de4623b06b0e2ceb"
integrity sha1-itkpozk7rGJ5V6fl3kYjsGsOLOs=
dependencies:
colors "~0.6.0-1"
commander "~2.1.0"

first-chunk-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/first-chunk-stream/-/first-chunk-stream-2.0.0.tgz#1bdecdb8e083c0664b91945581577a43a9f31d70"
Expand Down

0 comments on commit 4f598bd

Please sign in to comment.