Skip to content

Commit 0a95a0f

Browse files
Merge pull request #33 from bebraw/feat/update-to-webpack-5
2 parents 6fe9fb7 + 96267e5 commit 0a95a0f

13 files changed

Lines changed: 1094 additions & 246 deletions

File tree

.eslintrc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@
88
"plugin:jest/recommended",
99
"plugin:jest/style"
1010
],
11-
"plugins": [
12-
"prettier",
13-
"eslint-plugin-jsdoc",
14-
"@typescript-eslint",
15-
"jest"
16-
],
11+
"plugins": ["prettier", "eslint-plugin-jsdoc", "@typescript-eslint", "jest"],
1712
"parser": "@typescript-eslint/parser",
1813
"parserOptions": {
1914
"project": "./tsconfig.json",
2015
"sourceType": "module"
2116
},
22-
"ignorePatterns": [
23-
"jest.config.js",
24-
"**/__fixtures__"
25-
],
17+
"ignorePatterns": ["jest.config.js", "**/__fixtures__"],
2618
"env": {
2719
"jest/globals": true
2820
},
@@ -31,6 +23,7 @@
3123
"@typescript-eslint/camelcase": 0,
3224
"complexity": 0,
3325
"import/no-unresolved": 0,
34-
"import/extensions": 0
26+
"import/extensions": 0,
27+
"class-methods-use-this": 0
3528
}
3629
}

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
strategy:
1616
matrix:
17-
node-version: [10.x, 12.x]
17+
node-version: [12.x, 14.x]
1818

1919
steps:
2020
- uses: actions/checkout@v1

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
dist
3-
.env
3+
example-dist
4+
coverage
5+
.env

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
registry=https://registry.npmjs.org/
12
package-lock=false

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

package.json

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
"repository": "hipstersmoothie/react-docgen-typescript-plugin",
77
"author": "Andrew Lisowski <lisowski54@gmail.com>",
88
"main": "dist/index.js",
9+
"publishConfig": {
10+
"registry": "https://registry.npmjs.org/",
11+
"access": "public"
12+
},
913
"scripts": {
14+
"build:example": "npm run build && webpack",
1015
"build": "tsc -p tsconfig.build.json",
1116
"start": "yarn build --watch",
1217
"lint": "eslint src --ext .ts,.js",
13-
"test": "jest",
18+
"test": "npm run test:v4 && npm run test:v5",
19+
"pretest:v4": "add-no-save @types/webpack ts-loader@8 webpack@4",
20+
"test:v4": "jest",
21+
"pretest:v5": "add-no-save webpack@5",
22+
"test:v5": "jest",
1423
"release": "auto shipit"
1524
},
1625
"keywords": [
@@ -29,18 +38,19 @@
2938
"find-cache-dir": "^3.3.1",
3039
"flat-cache": "^3.0.4",
3140
"micromatch": "^4.0.2",
32-
"react-docgen-typescript": "^1.20.5",
33-
"tslib": "^2.0.0"
41+
"react-docgen-typescript": "^1.22.0",
42+
"tslib": "^2.0.0",
43+
"webpack-sources": "^2.2.0"
3444
},
3545
"devDependencies": {
3646
"@types/debug": "^4.1.5",
3747
"@types/find-cache-dir": "^3.2.0",
3848
"@types/flat-cache": "^2.0.0",
39-
"@types/jest": "^26.0.16",
49+
"@types/jest": "^26.0.23",
4050
"@types/micromatch": "^4.0.1",
4151
"@types/node": "^14.0.12",
4252
"@types/react": "^17.0.0",
43-
"@types/webpack": "^4.41.17",
53+
"@types/webpack-sources": "^2.1.0",
4454
"@typescript-eslint/eslint-plugin": "^4.9.0",
4555
"@typescript-eslint/parser": "^4.9.0",
4656
"auto": "^10.2.3",
@@ -55,13 +65,20 @@
5565
"eslint-plugin-jsx-a11y": "^6.4.1",
5666
"eslint-plugin-prettier": "3.1.3",
5767
"jest": "^26.6.3",
68+
"memfs": "^3.2.2",
69+
"memory-fs": "^0.5.0",
5870
"prettier": "^2.0.5",
5971
"react": "^17.0.1",
60-
"ts-jest": "^26.4.4",
61-
"typescript": "3.8.3"
72+
"ts-jest": "^26.5.6",
73+
"ts-loader": "^9.1.2",
74+
"typescript": "3.8.3",
75+
"webpack": "^5.36.2",
76+
"webpack-cli": "^4.7.0",
77+
"yarn-add-no-save": "^1.0.3"
6278
},
6379
"peerDependencies": {
64-
"typescript": ">= 3.x"
80+
"typescript": ">= 3.x",
81+
"webpack": ">= 4"
6582
},
6683
"lint-staged": {
6784
"*.{js,css,md}": [

src/__tests__/plugin.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import webpack, { Configuration } from "webpack";
2+
import { createFsFromVolume, IFs, Volume } from "memfs";
3+
import ReactDocgenTypeScriptPlugin from "..";
4+
5+
// eslint-disable-next-line
6+
const joinPath = require("memory-fs/lib/join");
7+
8+
// Hack for webpack 4. This isn't needed with 5
9+
// See more: https://github.com/streamich/memfs/issues/404.
10+
function ensureWebpackMemoryFs(fs: IFs) {
11+
// Return it back, when it has Webpack 'join' method
12+
// eslint-disable-next-line
13+
// @ts-ignore
14+
if (fs.join) {
15+
return fs;
16+
}
17+
18+
// Create FS proxy, adding `join` method to memfs, but not modifying original object
19+
const nextFs = Object.create(fs);
20+
nextFs.join = joinPath;
21+
22+
return nextFs;
23+
}
24+
25+
function compile(config: Configuration): Promise<string> {
26+
return new Promise((resolve, reject) => {
27+
const compiler = webpack(config);
28+
29+
// eslint-disable-next-line
30+
// @ts-ignore: There's a type mismatch but this should work based on webpack source
31+
compiler.outputFileSystem = ensureWebpackMemoryFs(
32+
createFsFromVolume(new Volume())
33+
);
34+
const memfs = compiler.outputFileSystem;
35+
36+
compiler.run((error, stats) => {
37+
if (error) {
38+
return reject(error);
39+
}
40+
41+
if (stats?.hasErrors()) {
42+
return reject(stats.toString("errors-only"));
43+
}
44+
45+
memfs.readFile(
46+
"./dist/main.js",
47+
{
48+
encoding: "utf-8",
49+
},
50+
// eslint-disable-next-line
51+
// @ts-ignore: Type mismatch again
52+
(err, data) => (err ? reject(err) : resolve(data))
53+
);
54+
55+
return undefined;
56+
});
57+
});
58+
}
59+
60+
const getConfig = (
61+
options = {},
62+
config: { title?: string } = {}
63+
): Configuration => ({
64+
mode: "none",
65+
entry: { main: "./src/__tests__/__fixtures__/Simple.tsx" },
66+
plugins: [new ReactDocgenTypeScriptPlugin(options)],
67+
module: {
68+
rules: [
69+
{
70+
test: /\.tsx?$/,
71+
loader: "ts-loader",
72+
options: {
73+
transpileOnly: true,
74+
},
75+
},
76+
],
77+
},
78+
...config,
79+
});
80+
81+
// TODO: What else to test and how?
82+
test("default options", async () => {
83+
const result = await compile(getConfig({}));
84+
85+
expect(result).toContain("STORYBOOK_REACT_CLASSES");
86+
});

src/dependency.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint-disable max-classes-per-file */
2+
import * as webpack from "webpack";
3+
4+
// eslint-disable-next-line
5+
// @ts-ignore: What's the right way to refer to this one?
6+
import makeSerializable from "webpack/lib/util/makeSerializable.js";
7+
8+
// eslint-disable-next-line
9+
// @ts-ignore: What's the right way to refer to this one?
10+
import NullDependency from "webpack/lib/dependencies/NullDependency.js";
11+
12+
class DocGenDependency extends NullDependency {
13+
public codeBlock: string;
14+
15+
constructor(codeBlock: string) {
16+
super();
17+
18+
this.codeBlock = codeBlock;
19+
}
20+
21+
updateHash: webpack.dependencies.NullDependency["updateHash"] = (hash) => {
22+
hash.update(this.codeBlock);
23+
};
24+
}
25+
26+
makeSerializable(
27+
DocGenDependency,
28+
"react-docgen-typescript-plugin/dist/dependency"
29+
);
30+
31+
type NullDependencyTemplateType = InstanceType<
32+
typeof webpack.dependencies.NullDependency.Template
33+
>;
34+
class DocGenTemplate extends NullDependency.Template
35+
implements NullDependencyTemplateType {
36+
// eslint-disable-next-line
37+
// @ts-ignore: Webpack 4 type
38+
apply: NullDependencyTemplateType["apply"] = (
39+
dependency: DocGenDependency,
40+
source
41+
) => {
42+
if (dependency.codeBlock) {
43+
// Insert to the end
44+
source.insert(Infinity, dependency.codeBlock);
45+
}
46+
};
47+
}
48+
49+
// @ts-ignore TODO: How to type this correctly?
50+
DocGenDependency.Template = DocGenTemplate;
51+
52+
// Default imports are tricky with CommonJS
53+
// eslint-disable-next-line
54+
export { DocGenDependency };

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22

3-
import type { DocgenPluginType, PluginOptions } from './plugin'
3+
import type { DocgenPluginType, PluginOptions } from "./plugin";
44

55
class EmptyPlugin {
66
constructor(_: PluginOptions) {}
@@ -9,12 +9,15 @@ class EmptyPlugin {
99

1010
let plugin: DocgenPluginType;
1111

12+
// It should be possible to use the plugin without TypeScript.
13+
// In that case using it is a no-op.
1214
try {
1315
require.resolve("typescript");
14-
plugin = require('./plugin').default;
16+
plugin = require("./plugin").default;
1517
} catch (error) {
1618
plugin = EmptyPlugin as any;
1719
}
1820

1921
export { PluginOptions } from "./plugin";
22+
export { plugin as ReactDocgenTypeScriptPlugin };
2023
export default plugin;

0 commit comments

Comments
 (0)