Skip to content

Instantly share code, notes, and snippets.

@wkillerud
Created April 4, 2019 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkillerud/36b119b98f9b9009cd50a733391941a4 to your computer and use it in GitHub Desktop.
Save wkillerud/36b119b98f9b9009cd50a733391941a4 to your computer and use it in GitHub Desktop.
A workaround for the poor performance described in styleguidist/react-docgen-typescript#112 based on the work done in strothj/react-docgen-typescript-loader#40.
/**
* The MIT License
*
* Further resources on the MIT License
* Copyright 2018 Jason Strothmann <jason@jasons.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
const fs = require('fs');
const path = require('path');
const ts = require('typescript');
const { withCustomConfig } = require('react-docgen-typescript');
let languageService = null;
const filesCache = new Map();
const parser = withCustomConfig('./tsconfig.json');
const tsConfigFile = getTSConfigFile('./tsconfig.json');
function propsParser(file, source) {
filesCache.set(file, {
text: source,
version: 0,
});
return parser.parseWithProgramProvider(file, function() {
if (languageService) {
return languageService.getProgram();
}
const servicesHost = createServiceHost(
tsConfigFile.compilerOptions,
filesCache
);
languageService = ts.createLanguageService(
servicesHost,
ts.createDocumentRegistry()
);
return languageService.getProgram();
});
}
function getTSConfigFile(tsconfigPath) {
const basePath = path.dirname(tsconfigPath);
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
return ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
basePath,
{},
tsconfigPath
);
}
function createServiceHost(compilerOptions, files) {
return {
getScriptFileNames: () => {
return [...files.keys()];
},
getScriptVersion: fileName => {
const file = files.get(fileName);
return (file && file.version.toString()) || '';
},
getScriptSnapshot: fileName => {
if (!fs.existsSync(fileName)) {
return undefined;
}
let file = files.get(fileName);
if (file === undefined) {
const text = fs.readFileSync(fileName).toString();
file = { version: 0, text };
files.set(fileName, file);
}
return ts.ScriptSnapshot.fromString(file.text);
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => compilerOptions,
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
};
}
module.exports = {
// ...,
propsParser: propsParser,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment