This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
loader.ts
226 lines (193 loc) · 6.46 KB
/
loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import webpack from "webpack";
import * as ts from "typescript";
import path from "path";
import fs from "fs";
// TODO: Import from "react-docgen-typescript" directly when
// https://github.com/styleguidist/react-docgen-typescript/pull/104 is hopefully
// merged in. Will be considering to make a peer dependency as that point.
import {
withDefaultConfig,
withCustomConfig,
withCompilerOptions,
ParserOptions,
FileParser,
} from "react-docgen-typescript/lib/parser.js";
import LoaderOptions from "./LoaderOptions";
import validateOptions from "./validateOptions";
import generateDocgenCodeBlock from "./generateDocgenCodeBlock";
import { getOptions } from "loader-utils";
export interface TSFile {
text?: string;
version: number;
}
let languageService: ts.LanguageService | null = null;
const files: Map<string, TSFile> = new Map<string, TSFile>();
export default function loader(
this: webpack.loader.LoaderContext,
source: string,
) {
// Loaders can operate in either synchronous or asynchronous mode. Errors in
// asynchronous mode should be reported using the supplied callback.
// Will return a callback if operating in asynchronous mode.
const callback = this.async();
try {
const newSource = processResource(this, source);
if (!callback) return newSource;
callback(null, newSource);
return;
} catch (e) {
if (callback) {
callback(e);
return;
}
throw e;
}
}
function processResource(
context: webpack.loader.LoaderContext,
source: string,
): string {
// Mark the loader as being cacheable since the result should be
// deterministic.
context.cacheable(true);
const options: LoaderOptions = Object.assign({}, getOptions(context));
validateOptions(options);
options.docgenCollectionName =
options.docgenCollectionName || "STORYBOOK_REACT_CLASSES";
if (typeof options.setDisplayName !== "boolean") {
options.setDisplayName = true;
}
// Convert the loader's flat options into the expected structure for
// react-docgen-typescript.
// See: node_modules/react-docgen-typescript/lib/parser.d.ts
const parserOptions: ParserOptions = {
componentNameResolver: options.componentNameResolver,
propFilter:
options.skipPropsWithName || options.skipPropsWithoutDoc
? {
skipPropsWithName: options.skipPropsWithName || undefined,
skipPropsWithoutDoc: options.skipPropsWithoutDoc || undefined,
}
: options.propFilter,
shouldExtractLiteralValuesFromEnum:
options.shouldExtractLiteralValuesFromEnum,
savePropValueAsString: options.savePropValueAsString,
shouldRemoveUndefinedFromOptional:
options.shouldRemoveUndefinedFromOptional,
};
// Configure parser using settings provided to loader.
// See: node_modules/react-docgen-typescript/lib/parser.d.ts
let parser: FileParser = withDefaultConfig(parserOptions);
let compilerOptions: ts.CompilerOptions = {
allowJs: true,
};
let tsConfigFile: ts.ParsedCommandLine | null = null;
if (options.tsconfigPath) {
parser = withCustomConfig(options.tsconfigPath, parserOptions);
tsConfigFile = getTSConfigFile(options.tsconfigPath!);
compilerOptions = tsConfigFile.options;
const filesToLoad = tsConfigFile.fileNames;
loadFiles(filesToLoad);
} else if (options.compilerOptions) {
parser = withCompilerOptions(options.compilerOptions, parserOptions);
compilerOptions = options.compilerOptions;
}
if (!tsConfigFile) {
const basePath = path.dirname(context.context);
tsConfigFile = getDefaultTSConfigFile(basePath);
const filesToLoad = tsConfigFile.fileNames;
loadFiles(filesToLoad);
}
const componentDocs = parser.parseWithProgramProvider(
context.resourcePath,
() => {
if (languageService) {
return languageService.getProgram()!;
}
const servicesHost = createServiceHost(compilerOptions, files);
languageService = ts.createLanguageService(
servicesHost,
ts.createDocumentRegistry(),
);
return languageService!.getProgram()!;
},
);
options.typePropName = options.typePropName || "type";
// Return amended source code if there is docgen information available.
if (componentDocs.length) {
return generateDocgenCodeBlock({
filename: context.resourcePath,
source,
componentDocs,
docgenCollectionName: options.docgenCollectionName,
setDisplayName: options.setDisplayName,
typePropName: options.typePropName,
});
}
// Return unchanged source code if no docgen information was available.
return source;
}
function getTSConfigFile(tsconfigPath: string): ts.ParsedCommandLine {
const basePath = path.dirname(tsconfigPath);
const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
return ts.parseJsonConfigFileContent(
configFile!.config,
ts.sys,
basePath,
{},
tsconfigPath,
);
}
function getDefaultTSConfigFile(basePath: string): ts.ParsedCommandLine {
return ts.parseJsonConfigFileContent({}, ts.sys, basePath, {});
}
function loadFiles(filesToLoad: string[]): void {
filesToLoad.forEach(filePath => {
const normalizedFilePath = path.normalize(filePath);
const file = files.get(normalizedFilePath);
const text = fs.readFileSync(normalizedFilePath, "utf-8");
if (!file) {
files.set(normalizedFilePath, {
text,
version: 0,
});
} else if (file.text !== text) {
files.set(normalizedFilePath, {
text,
version: file.version + 1,
});
}
});
}
function createServiceHost(
compilerOptions: ts.CompilerOptions,
files: Map<string, TSFile>,
): ts.LanguageServiceHost {
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,
};
}