Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat - Add support for webpack 5 #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
24b379f
chore: Add initial plugin test setup
bebraw May 6, 2021
cb28064
chore: Add webpack 5 as a development dependency
bebraw May 6, 2021
eefd9b7
fix: Fix entry path
bebraw May 6, 2021
4e7b763
chore: Add webpack 5 as a peer dependency
bebraw May 6, 2021
c10c494
chore: Add structure needed by the new logic
bebraw May 6, 2021
3e20aa8
chore: Add prettier config to fix vs code
bebraw May 6, 2021
9fda542
chore: Add implementation instructions
bebraw May 6, 2021
87b1a16
chore: Sketch out implementation
bebraw May 6, 2021
0824af0
chore: Complete a simple test harness for testing against webpack 5
bebraw May 6, 2021
8ee5003
chore: Sketch out implementation
bebraw May 7, 2021
a1c94ae
chore: Load simple component for testing
bebraw May 7, 2021
7499a99
chore: Clean up implementation
bebraw May 7, 2021
6b78f8a
chore: Figure out SourceReplace API
bebraw May 7, 2021
e450114
chore: Add logging to understand code better
bebraw May 7, 2021
cbf7e72
chore: Update Jest related dependencies
bebraw May 7, 2021
766374a
chore: Add an initial example to help in debugging
bebraw May 7, 2021
d079532
chore: Make project compile
bebraw May 7, 2021
312dcbb
chore: Make export more convenient
bebraw May 7, 2021
64db51d
chore: Mute webpack warning
bebraw May 7, 2021
2ad2b85
chore: Add logging
bebraw May 7, 2021
19a1865
chore: Improve logging
bebraw May 7, 2021
8a5f37a
chore: Add a todo
bebraw May 7, 2021
7fab167
fix: Fix type name
bebraw May 7, 2021
ec089f0
chore: Sketch out the usage of parser api
bebraw May 10, 2021
e076106
fix: Use the correct plugin name
bebraw May 10, 2021
bf0635f
fix: Use the right hook to add the module dependency
bebraw May 10, 2021
df50685
fix: Pass the right value to the dependency
bebraw May 10, 2021
605dac3
chore: Add logging
bebraw May 10, 2021
6ff7875
chore: Simplify code
bebraw May 10, 2021
3b5e7bb
fix: Emit hello world demo correctly
bebraw May 10, 2021
df2042e
refactor: Extract documentation generator + fix include/exclude
bebraw May 10, 2021
927d97b
fix: Use same defaults for include/exclude as before
bebraw May 10, 2021
6a0f609
chore: Sketch out doc generation through ts
bebraw May 10, 2021
49f70cb
chore: Add logging
bebraw May 10, 2021
c9faa09
fix: Fix type error
bebraw May 10, 2021
2fc6014
chore: Tidy up
bebraw May 10, 2021
8452db5
chore: Simplify docgen usage
bebraw May 10, 2021
48ff075
chore: Use Column example
bebraw May 10, 2021
d3bd544
fix: Make doc generation work
bebraw May 10, 2021
201dcf0
chore: Drop Column fixture as that's not needed anymore
bebraw May 10, 2021
69dc989
chore: Improve logic
bebraw May 10, 2021
cbe836c
chore: Drop redundant code
bebraw May 10, 2021
6dd88f2
chore: Add initial test to make sure docgen emits
bebraw May 10, 2021
a0b4c6d
chore: Add a todo
bebraw May 10, 2021
da18365
chore: Drop a redundant snapshot
bebraw May 10, 2021
64f91ef
chore: Add a todo
bebraw May 11, 2021
a3965f6
fix: Use NullDependency
bebraw May 11, 2021
19f789b
fix: Use the correct path for serialization
bebraw May 11, 2021
521af9c
chore: Drop redundant code
bebraw May 11, 2021
a0f559f
chore: Simplify logic
bebraw May 11, 2021
ed02af9
fix: Insert code to the beginning
bebraw May 11, 2021
cea84f4
refactor: Push parsing earlier
bebraw May 11, 2021
50e5ae4
chore: Drop a ts-ignore
bebraw May 11, 2021
d1b32fc
refactor: Push codeblock generation to the right place
bebraw May 11, 2021
688ec3f
chore: Drop a todo
bebraw May 11, 2021
2559070
chore: Drop dead code
bebraw May 11, 2021
4c8165b
chore: Implement updateHash
bebraw May 11, 2021
769229e
chore: Improve typing
bebraw May 11, 2021
d7708ef
chore: Drop dead code
bebraw May 11, 2021
f6e2133
refactor: Push option logic to apply
bebraw May 11, 2021
2543aa4
fix: Pass generator/parser options correctly
bebraw May 11, 2021
c9a7324
fix: Fix typing
bebraw May 11, 2021
340a36e
add publish config
hipstersmoothie May 11, 2021
29e1a62
update matrix
hipstersmoothie May 11, 2021
ef5131f
fix: Write the types to the end
bebraw May 11, 2021
292669d
Merge branch 'master' into feat/update-to-webpack-5
hipstersmoothie May 12, 2021
bcfc489
chore: Simplify typing
bebraw May 14, 2021
5b1c571
refactor: Write docs at seal
bebraw May 14, 2021
95eb37b
refactor: Create a single tsProgram to match old logic
bebraw May 14, 2021
8da9df4
feat: Add initial support for webpack 4
bebraw May 14, 2021
5d2e706
fix: Make dependency portion webpack 5 specific
bebraw May 18, 2021
ca236db
fix: Use yarn over npm to avoid mixed usage
bebraw May 18, 2021
e08cf7f
fix: Use the right no-save in scripts
bebraw May 18, 2021
e099d3b
fix: webpack object might be missing
bebraw May 18, 2021
0cac4f2
fix: Add a splittable default value
bebraw May 18, 2021
0d2d6e4
fix: Fix tests for webpack 4
bebraw May 18, 2021
59e16a7
fix: Restore webpack 4 cache
bebraw May 18, 2021
a658945
fix: If module.nameForCondition is missing, do nothing
bebraw May 20, 2021
27a93e3
fix: Use other type of import for NullDependency
bebraw May 20, 2021
c1f61e4
fix: Don't include file path to the generated code
bebraw May 20, 2021
96267e5
fix: Don't console.error if importing ts fails
bebraw May 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@
"plugin:jest/recommended",
"plugin:jest/style"
],
"plugins": [
"prettier",
"eslint-plugin-jsdoc",
"@typescript-eslint",
"jest"
],
"plugins": ["prettier", "eslint-plugin-jsdoc", "@typescript-eslint", "jest"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"sourceType": "module"
},
"ignorePatterns": [
"jest.config.js",
"**/__fixtures__"
],
"ignorePatterns": ["jest.config.js", "**/__fixtures__"],
"env": {
"jest/globals": true
},
Expand All @@ -31,6 +23,7 @@
"@typescript-eslint/camelcase": 0,
"complexity": 0,
"import/no-unresolved": 0,
"import/extensions": 0
"import/extensions": 0,
"class-methods-use-this": 0
}
}
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [12.x, 14.x]

steps:
- uses: actions/checkout@v1
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
.env
example-dist
coverage
.env
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
registry=https://registry.npmjs.org/
package-lock=false
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
33 changes: 25 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
"repository": "hipstersmoothie/react-docgen-typescript-plugin",
"author": "Andrew Lisowski <[email protected]>",
"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",
"start": "yarn build --watch",
"lint": "eslint src --ext .ts,.js",
"test": "jest",
"test": "npm run test:v4 && npm run test:v5",
"pretest:v4": "add-no-save @types/webpack ts-loader@8 webpack@4",
"test:v4": "jest",
"pretest:v5": "add-no-save webpack@5",
"test:v5": "jest",
"release": "auto shipit"
},
"keywords": [
Expand All @@ -29,18 +38,19 @@
"find-cache-dir": "^3.3.1",
"flat-cache": "^3.0.4",
"micromatch": "^4.0.2",
"react-docgen-typescript": "^1.20.5",
"tslib": "^2.0.0"
"react-docgen-typescript": "^1.22.0",
"tslib": "^2.0.0",
"webpack-sources": "^2.2.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/find-cache-dir": "^3.2.0",
"@types/flat-cache": "^2.0.0",
"@types/jest": "^26.0.16",
"@types/jest": "^26.0.23",
"@types/micromatch": "^4.0.1",
"@types/node": "^14.0.12",
"@types/react": "^17.0.0",
"@types/webpack": "^4.41.17",
"@types/webpack-sources": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"auto": "^10.2.3",
Expand All @@ -55,13 +65,20 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "3.1.3",
"jest": "^26.6.3",
"memfs": "^3.2.2",
"memory-fs": "^0.5.0",
"prettier": "^2.0.5",
"react": "^17.0.1",
"ts-jest": "^26.4.4",
"typescript": "3.8.3"
"ts-jest": "^26.5.6",
"ts-loader": "^9.1.2",
"typescript": "3.8.3",
"webpack": "^5.36.2",
"webpack-cli": "^4.7.0",
"yarn-add-no-save": "^1.0.3"
},
"peerDependencies": {
"typescript": ">= 3.x"
"typescript": ">= 3.x",
"webpack": ">= 4"
},
"lint-staged": {
"*.{js,css,md}": [
Expand Down
86 changes: 86 additions & 0 deletions src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import webpack, { Configuration } from "webpack";
import { createFsFromVolume, IFs, Volume } from "memfs";
import ReactDocgenTypeScriptPlugin from "..";

// eslint-disable-next-line
const joinPath = require("memory-fs/lib/join");

// Hack for webpack 4. This isn't needed with 5
// See more: https://github.com/streamich/memfs/issues/404.
function ensureWebpackMemoryFs(fs: IFs) {
// Return it back, when it has Webpack 'join' method
// eslint-disable-next-line
// @ts-ignore
if (fs.join) {
return fs;
}

// Create FS proxy, adding `join` method to memfs, but not modifying original object
const nextFs = Object.create(fs);
nextFs.join = joinPath;

return nextFs;
}

function compile(config: Configuration): Promise<string> {
return new Promise((resolve, reject) => {
const compiler = webpack(config);

// eslint-disable-next-line
// @ts-ignore: There's a type mismatch but this should work based on webpack source
compiler.outputFileSystem = ensureWebpackMemoryFs(
createFsFromVolume(new Volume())
);
const memfs = compiler.outputFileSystem;

compiler.run((error, stats) => {
if (error) {
return reject(error);
}

if (stats?.hasErrors()) {
return reject(stats.toString("errors-only"));
}

memfs.readFile(
"./dist/main.js",
{
encoding: "utf-8",
},
// eslint-disable-next-line
// @ts-ignore: Type mismatch again
(err, data) => (err ? reject(err) : resolve(data))
);

return undefined;
});
});
}

const getConfig = (
options = {},
config: { title?: string } = {}
): Configuration => ({
mode: "none",
entry: { main: "./src/__tests__/__fixtures__/Simple.tsx" },
plugins: [new ReactDocgenTypeScriptPlugin(options)],
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
transpileOnly: true,
},
},
],
},
...config,
});

// TODO: What else to test and how?
test("default options", async () => {
const result = await compile(getConfig({}));

expect(result).toContain("STORYBOOK_REACT_CLASSES");
});
54 changes: 54 additions & 0 deletions src/dependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable max-classes-per-file */
import * as webpack from "webpack";

// eslint-disable-next-line
// @ts-ignore: What's the right way to refer to this one?
import makeSerializable from "webpack/lib/util/makeSerializable.js";

// eslint-disable-next-line
// @ts-ignore: What's the right way to refer to this one?
import NullDependency from "webpack/lib/dependencies/NullDependency.js";

class DocGenDependency extends NullDependency {
public codeBlock: string;

constructor(codeBlock: string) {
super();

this.codeBlock = codeBlock;
}

updateHash: webpack.dependencies.NullDependency["updateHash"] = (hash) => {
hash.update(this.codeBlock);
};
}

makeSerializable(
DocGenDependency,
"react-docgen-typescript-plugin/dist/dependency"
);

type NullDependencyTemplateType = InstanceType<
typeof webpack.dependencies.NullDependency.Template
>;
class DocGenTemplate extends NullDependency.Template
implements NullDependencyTemplateType {
// eslint-disable-next-line
// @ts-ignore: Webpack 4 type
apply: NullDependencyTemplateType["apply"] = (
dependency: DocGenDependency,
source
) => {
if (dependency.codeBlock) {
// Insert to the end
source.insert(Infinity, dependency.codeBlock);
}
};
}

// @ts-ignore TODO: How to type this correctly?
DocGenDependency.Template = DocGenTemplate;

// Default imports are tricky with CommonJS
// eslint-disable-next-line
export { DocGenDependency };
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */

import type { DocgenPluginType, PluginOptions } from './plugin'
import type { DocgenPluginType, PluginOptions } from "./plugin";

class EmptyPlugin {
constructor(_: PluginOptions) {}
Expand All @@ -9,12 +9,15 @@ class EmptyPlugin {

let plugin: DocgenPluginType;

// It should be possible to use the plugin without TypeScript.
// In that case using it is a no-op.
try {
require.resolve("typescript");
plugin = require('./plugin').default;
plugin = require("./plugin").default;
} catch (error) {
plugin = EmptyPlugin as any;
}

export { PluginOptions } from "./plugin";
export { plugin as ReactDocgenTypeScriptPlugin };
export default plugin;
Loading