-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from bebraw/feat/update-to-webpack-5
- Loading branch information
Showing
13 changed files
with
1,094 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
dist | ||
.env | ||
example-dist | ||
coverage | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
registry=https://registry.npmjs.org/ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": [ | ||
|
@@ -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", | ||
|
@@ -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}": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.