react-docgen-typescript-plugin
Advanced tools
Comparing version 0.7.1-canary.29e1a62.0 to 0.7.1
@@ -0,1 +1,17 @@ | ||
# v0.7.1 (Tue May 11 2021) | ||
:tada: This release contains work from a new contributor! :tada: | ||
Thank you, Kyle Herock ([@kherock](https://github.com/kherock)), for all your work! | ||
#### 🐛 Bug Fix | ||
- Include dotfiles in micromatch globs [#34](https://github.com/hipstersmoothie/react-docgen-typescript-plugin/pull/34) ([@kherock](https://github.com/kherock)) | ||
#### Authors: 1 | ||
- Kyle Herock ([@kherock](https://github.com/kherock)) | ||
--- | ||
# v0.7.0 (Fri Dec 04 2020) | ||
@@ -2,0 +18,0 @@ |
@@ -1,6 +0,5 @@ | ||
import type { DocgenPluginType } from "./plugin"; | ||
import type { DocgenPluginType } from './plugin'; | ||
declare let plugin: DocgenPluginType; | ||
export { PluginOptions } from "./plugin"; | ||
export { plugin as ReactDocgenTypeScriptPlugin }; | ||
export default plugin; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,11 +9,10 @@ "use strict"; | ||
let plugin; | ||
exports.ReactDocgenTypeScriptPlugin = plugin; | ||
try { | ||
require.resolve("typescript"); | ||
exports.ReactDocgenTypeScriptPlugin = plugin = require("./plugin").default; | ||
plugin = require('./plugin').default; | ||
} | ||
catch (error) { | ||
exports.ReactDocgenTypeScriptPlugin = plugin = EmptyPlugin; | ||
plugin = EmptyPlugin; | ||
} | ||
exports.default = plugin; | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,4 @@ | ||
import { Compiler, WebpackPluginInstance } from "webpack"; | ||
import * as webpack from "webpack"; | ||
import ts from "typescript"; | ||
import * as docGen from "react-docgen-typescript"; | ||
import { LoaderOptions } from "./types"; | ||
import { GeneratorOptions } from "./generateDocgenCodeBlock"; | ||
interface TypescriptOptions { | ||
@@ -15,2 +13,34 @@ /** | ||
} | ||
interface LoaderOptions { | ||
/** | ||
* Specify the docgen collection name to use. All docgen information will | ||
* be collected into this global object. Set to null to disable. | ||
* | ||
* @default STORYBOOK_REACT_CLASSES | ||
* @see https://github.com/gongreg/react-storybook-addon-docgen | ||
**/ | ||
docgenCollectionName?: string | null; | ||
/** | ||
* Automatically set the component's display name. If you want to set display | ||
* names yourself or are using another plugin to do this, you should disable | ||
* this option. | ||
* | ||
* ``` | ||
* class MyComponent extends React.Component { | ||
* ... | ||
* } | ||
* | ||
* MyComponent.displayName = "MyComponent"; | ||
* ``` | ||
* | ||
* @default true | ||
*/ | ||
setDisplayName?: boolean; | ||
/** | ||
* Specify the name of the property for docgen info prop type. | ||
* | ||
* @default "type" | ||
*/ | ||
typePropName?: string; | ||
} | ||
export declare type PluginOptions = docGen.ParserOptions & LoaderOptions & TypescriptOptions & { | ||
@@ -23,16 +53,7 @@ /** Glob patterns to ignore */ | ||
/** Inject typescript docgen information into modules at the end of a build */ | ||
export default class DocgenPlugin implements WebpackPluginInstance { | ||
export default class DocgenPlugin { | ||
private name; | ||
private options; | ||
constructor(options?: PluginOptions); | ||
apply(compiler: Compiler): void; | ||
getOptions(): { | ||
docgenOptions: docGen.ParserOptions; | ||
generateOptions: { | ||
docgenCollectionName: GeneratorOptions["docgenCollectionName"]; | ||
setDisplayName: GeneratorOptions["setDisplayName"]; | ||
typePropName: GeneratorOptions["typePropName"]; | ||
}; | ||
compilerOptions: ts.CompilerOptions; | ||
}; | ||
apply(compiler: webpack.Compiler): void; | ||
} | ||
@@ -39,0 +60,0 @@ export declare type DocgenPluginType = typeof DocgenPlugin; |
@@ -10,5 +10,38 @@ "use strict"; | ||
const micromatch_1 = require("micromatch"); | ||
const dependency_1 = tslib_1.__importDefault(require("./dependency")); | ||
const find_cache_dir_1 = tslib_1.__importDefault(require("find-cache-dir")); | ||
const flat_cache_1 = tslib_1.__importDefault(require("flat-cache")); | ||
const crypto_1 = tslib_1.__importDefault(require("crypto")); | ||
const generateDocgenCodeBlock_1 = require("./generateDocgenCodeBlock"); | ||
const debugExclude = debug_1.default("docgen:exclude"); | ||
const debugInclude = debug_1.default("docgen:include"); | ||
const debugDocs = debug_1.default("docgen:docs"); | ||
const cacheId = "ts-docgen"; | ||
const cacheDir = find_cache_dir_1.default({ name: cacheId }); | ||
const cache = flat_cache_1.default.load(cacheId, cacheDir); | ||
/** Run the docgen parser and inject the result into the output */ | ||
function processModule(parser, webpackModule, tsProgram, loaderOptions) { | ||
if (!webpackModule) { | ||
return; | ||
} | ||
const hash = crypto_1.default | ||
.createHash("sha1") | ||
.update(webpackModule._source._value) | ||
.digest("hex"); | ||
const cached = cache.getKey(hash); | ||
if (cached) { | ||
debugInclude(`Got cached docgen for "${webpackModule.request}"`); | ||
webpackModule._source._value = cached; | ||
return; | ||
} | ||
const componentDocs = parser.parseWithProgramProvider(webpackModule.userRequest, () => tsProgram); | ||
if (!componentDocs.length) { | ||
return; | ||
} | ||
const docs = generateDocgenCodeBlock_1.generateDocgenCodeBlock(Object.assign({ filename: webpackModule.userRequest, source: webpackModule.userRequest, componentDocs }, loaderOptions)).substring(webpackModule.userRequest.length); | ||
debugDocs(docs); | ||
let sourceWithDocs = webpackModule._source._value; | ||
sourceWithDocs += `\n${docs}\n`; | ||
webpackModule._source._value = sourceWithDocs; | ||
cache.setKey(hash, sourceWithDocs); | ||
} | ||
/** Get the contents of the tsconfig in the system */ | ||
@@ -27,3 +60,3 @@ function getTSConfigFile(tsconfigPath) { | ||
const matchGlob = (globs) => { | ||
const matchers = (globs || []).map((g) => micromatch_1.matcher(g)); | ||
const matchers = globs.map((g) => micromatch_1.matcher(g, { dot: true })); | ||
return (filename) => Boolean(filename && matchers.find((match) => match(filename))); | ||
@@ -38,44 +71,5 @@ }; | ||
apply(compiler) { | ||
const pluginName = "DocGenPlugin"; | ||
const { docgenOptions, compilerOptions, generateOptions, } = this.getOptions(); | ||
const docGenParser = docGen.withCompilerOptions(compilerOptions, docgenOptions); | ||
const { exclude = [], include = ["**/**.tsx"] } = this.options; | ||
const _a = this.options, { tsconfigPath = "./tsconfig.json", docgenCollectionName = "STORYBOOK_REACT_CLASSES", setDisplayName = true, typePropName = "type", compilerOptions: userCompilerOptions, exclude = [], include = ["**/**.tsx"] } = _a, docgenOptions = tslib_1.__rest(_a, ["tsconfigPath", "docgenCollectionName", "setDisplayName", "typePropName", "compilerOptions", "exclude", "include"]); | ||
const isExcluded = matchGlob(exclude); | ||
const isIncluded = matchGlob(include); | ||
compiler.hooks.compilation.tap(pluginName, (compilation, { normalModuleFactory }) => { | ||
compilation.dependencyTemplates.set( | ||
// eslint-disable-next-line | ||
// @ts-ignore TODO: Figure out why this isn't allowed | ||
dependency_1.default, new dependency_1.default.Template()); | ||
const handler = (parser) => { | ||
parser.hooks.program.tap(pluginName, () => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
const { module } = parser.state; | ||
const nameForCondition = module.nameForCondition(); | ||
if (isExcluded(nameForCondition)) { | ||
debugExclude(`Module not matched in "exclude": ${nameForCondition}`); | ||
return; | ||
} | ||
if (!isIncluded(nameForCondition)) { | ||
debugExclude(`Module not matched in "include": ${nameForCondition}`); | ||
return; | ||
} | ||
const componentDocs = docGenParser.parse(nameForCondition); | ||
module.addDependency(new dependency_1.default(module.request, generateDocgenCodeBlock_1.generateDocgenCodeBlock(Object.assign({ filename: nameForCondition, source: nameForCondition, componentDocs }, generateOptions)).substring(module.userRequest.length))); | ||
}); | ||
}; | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/auto") | ||
.tap(pluginName, handler); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/dynamic") | ||
.tap(pluginName, handler); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/esm") | ||
.tap(pluginName, handler); | ||
}); | ||
} | ||
getOptions() { | ||
const _a = this.options, { tsconfigPath = "./tsconfig.json", compilerOptions: userCompilerOptions, docgenCollectionName, setDisplayName, typePropName } = _a, docgenOptions = tslib_1.__rest(_a, ["tsconfigPath", "compilerOptions", "docgenCollectionName", "setDisplayName", "typePropName"]); | ||
let compilerOptions = { | ||
@@ -90,14 +84,42 @@ jsx: typescript_1.default.JsxEmit.React, | ||
else { | ||
const { options: tsOptions } = getTSConfigFile(tsconfigPath); | ||
compilerOptions = Object.assign(Object.assign({}, compilerOptions), tsOptions); | ||
const { options } = getTSConfigFile(tsconfigPath); | ||
compilerOptions = Object.assign(Object.assign({}, compilerOptions), options); | ||
} | ||
return { | ||
docgenOptions, | ||
generateOptions: { | ||
docgenCollectionName: docgenCollectionName || "STORYBOOK_REACT_CLASSES", | ||
setDisplayName: setDisplayName || true, | ||
typePropName: typePropName || "type", | ||
}, | ||
compilerOptions, | ||
}; | ||
const parser = docGen.withCompilerOptions(compilerOptions, docgenOptions); | ||
compiler.hooks.make.tap(this.name, (compilation) => { | ||
compilation.hooks.seal.tap(this.name, () => { | ||
const modulesToProcess = []; | ||
compilation.modules.forEach((module) => { | ||
if (!module.built) { | ||
debugExclude(`Ignoring un-built module: ${module.userRequest}`); | ||
return; | ||
} | ||
if (module.external) { | ||
debugExclude(`Ignoring external module: ${module.userRequest}`); | ||
return; | ||
} | ||
if (!module.rawRequest) { | ||
debugExclude(`Ignoring module without "rawRequest": ${module.userRequest}`); | ||
return; | ||
} | ||
if (isExcluded(module.userRequest)) { | ||
debugExclude(`Module not matched in "exclude": ${module.userRequest}`); | ||
return; | ||
} | ||
if (!isIncluded(module.userRequest)) { | ||
debugExclude(`Module not matched in "include": ${module.userRequest}`); | ||
return; | ||
} | ||
debugInclude(module.userRequest); | ||
modulesToProcess.push(module); | ||
}); | ||
const tsProgram = typescript_1.default.createProgram(modulesToProcess.map((v) => v.userRequest), compilerOptions); | ||
modulesToProcess.forEach((m) => processModule(parser, m, tsProgram, { | ||
docgenCollectionName, | ||
setDisplayName, | ||
typePropName, | ||
})); | ||
cache.save(); | ||
}); | ||
}); | ||
} | ||
@@ -104,0 +126,0 @@ } |
{ | ||
"name": "react-docgen-typescript-plugin", | ||
"version": "0.7.1-canary.29e1a62.0", | ||
"version": "0.7.1", | ||
"description": "A webpack plugin to inject react typescript docgen information.", | ||
@@ -9,8 +9,3 @@ "license": "MIT", | ||
"main": "dist/index.js", | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/", | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build:example": "npm run build && webpack", | ||
"build": "tsc -p tsconfig.build.json", | ||
@@ -35,6 +30,7 @@ "start": "yarn build --watch", | ||
"endent": "^2.0.1", | ||
"find-cache-dir": "^3.3.1", | ||
"flat-cache": "^3.0.4", | ||
"micromatch": "^4.0.2", | ||
"react-docgen-typescript": "^1.22.0", | ||
"tslib": "^2.0.0", | ||
"webpack-sources": "^2.2.0" | ||
"react-docgen-typescript": "^1.20.5", | ||
"tslib": "^2.0.0" | ||
}, | ||
@@ -45,7 +41,7 @@ "devDependencies": { | ||
"@types/flat-cache": "^2.0.0", | ||
"@types/jest": "^26.0.23", | ||
"@types/jest": "^26.0.16", | ||
"@types/micromatch": "^4.0.1", | ||
"@types/node": "^14.0.12", | ||
"@types/react": "^17.0.0", | ||
"@types/webpack-sources": "^2.1.0", | ||
"@types/webpack": "^4.41.17", | ||
"@typescript-eslint/eslint-plugin": "^4.9.0", | ||
@@ -65,14 +61,9 @@ "@typescript-eslint/parser": "^4.9.0", | ||
"jest": "^26.6.3", | ||
"memfs": "^3.2.2", | ||
"prettier": "^2.0.5", | ||
"react": "^17.0.1", | ||
"ts-jest": "^26.5.6", | ||
"ts-loader": "^9.1.2", | ||
"typescript": "3.8.3", | ||
"webpack": "^5.36.2", | ||
"webpack-cli": "^4.7.0" | ||
"ts-jest": "^26.4.4", | ||
"typescript": "3.8.3" | ||
}, | ||
"peerDependencies": { | ||
"typescript": ">= 3.x", | ||
"webpack": ">= 5" | ||
"typescript": ">= 3.x" | ||
}, | ||
@@ -79,0 +70,0 @@ "lint-staged": { |
@@ -83,2 +83,3 @@ <div align="center"> | ||
<td align="center"><a href="http://shilman.net/"><img src="https://avatars2.githubusercontent.com/u/488689?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Shilman</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=shilman" title="Code">💻</a></td> | ||
<td align="center"><a href="https://github.com/kherock"><img src="https://avatars.githubusercontent.com/u/4993980?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kyle Herock</b></sub></a><br /><a href="https://github.com/hipstersmoothie/react-docgen-typescript-plugin/commits?author=kherock" title="Code">💻</a></td> | ||
</tr> | ||
@@ -85,0 +86,0 @@ </table> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26
92
48638
16
435
+ Addedfind-cache-dir@^3.3.1
+ Addedflat-cache@^3.0.4
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcommondir@1.0.1(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedfind-cache-dir@3.3.2(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedflat-cache@3.2.0(transitive)
+ Addedflatted@3.3.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpkg-dir@4.2.0(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedwebpack-sources@^2.2.0
- Removed@jridgewell/gen-mapping@0.3.8(transitive)
- Removed@jridgewell/resolve-uri@3.1.2(transitive)
- Removed@jridgewell/set-array@1.2.1(transitive)
- Removed@jridgewell/source-map@0.3.6(transitive)
- Removed@jridgewell/sourcemap-codec@1.5.0(transitive)
- Removed@jridgewell/trace-mapping@0.3.25(transitive)
- Removed@types/eslint@9.6.1(transitive)
- Removed@types/eslint-scope@3.7.7(transitive)
- Removed@types/estree@1.0.6(transitive)
- Removed@types/json-schema@7.0.15(transitive)
- Removed@types/node@22.10.5(transitive)
- Removed@webassemblyjs/ast@1.14.1(transitive)
- Removed@webassemblyjs/floating-point-hex-parser@1.13.2(transitive)
- Removed@webassemblyjs/helper-api-error@1.13.2(transitive)
- Removed@webassemblyjs/helper-buffer@1.14.1(transitive)
- Removed@webassemblyjs/helper-numbers@1.13.2(transitive)
- Removed@webassemblyjs/helper-wasm-bytecode@1.13.2(transitive)
- Removed@webassemblyjs/helper-wasm-section@1.14.1(transitive)
- Removed@webassemblyjs/ieee754@1.13.2(transitive)
- Removed@webassemblyjs/leb128@1.13.2(transitive)
- Removed@webassemblyjs/utf8@1.13.2(transitive)
- Removed@webassemblyjs/wasm-edit@1.14.1(transitive)
- Removed@webassemblyjs/wasm-gen@1.14.1(transitive)
- Removed@webassemblyjs/wasm-opt@1.14.1(transitive)
- Removed@webassemblyjs/wasm-parser@1.14.1(transitive)
- Removed@webassemblyjs/wast-printer@1.14.1(transitive)
- Removed@xtuc/ieee754@1.2.0(transitive)
- Removed@xtuc/long@4.2.2(transitive)
- Removedacorn@8.14.0(transitive)
- Removedajv@6.12.68.17.1(transitive)
- Removedajv-formats@2.1.1(transitive)
- Removedajv-keywords@3.5.25.1.0(transitive)
- Removedbrowserslist@4.24.4(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedcaniuse-lite@1.0.30001690(transitive)
- Removedchrome-trace-event@1.0.4(transitive)
- Removedcommander@2.20.3(transitive)
- Removedelectron-to-chromium@1.5.79(transitive)
- Removedenhanced-resolve@5.18.0(transitive)
- Removedes-module-lexer@1.6.0(transitive)
- Removedescalade@3.2.0(transitive)
- Removedeslint-scope@5.1.1(transitive)
- Removedesrecurse@4.3.0(transitive)
- Removedestraverse@4.3.05.3.0(transitive)
- Removedevents@3.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-uri@3.0.5(transitive)
- Removedglob-to-regexp@0.4.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-flag@4.0.0(transitive)
- Removedjest-worker@27.5.1(transitive)
- Removedjson-parse-even-better-errors@2.3.1(transitive)
- Removedjson-schema-traverse@0.4.11.0.0(transitive)
- Removedloader-runner@4.3.0(transitive)
- Removedmerge-stream@2.0.0(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedneo-async@2.6.2(transitive)
- Removednode-releases@2.0.19(transitive)
- Removedpicocolors@1.1.1(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedrandombytes@2.1.0(transitive)
- Removedrequire-from-string@2.0.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedschema-utils@3.3.04.3.0(transitive)
- Removedserialize-javascript@6.0.2(transitive)
- Removedsource-list-map@2.0.1(transitive)
- Removedsource-map@0.6.1(transitive)
- Removedsource-map-support@0.5.21(transitive)
- Removedsupports-color@8.1.1(transitive)
- Removedtapable@2.2.1(transitive)
- Removedterser@5.37.0(transitive)
- Removedterser-webpack-plugin@5.3.11(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedupdate-browserslist-db@1.1.2(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedwatchpack@2.4.2(transitive)
- Removedwebpack@5.97.1(transitive)
- Removedwebpack-sources@2.3.13.2.3(transitive)