-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docz-plugin-babel6): add package
- Loading branch information
1 parent
420eb88
commit 68e364a
Showing
5 changed files
with
109 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "docz-plugin-babel6", | ||
"version": "0.2.4", | ||
"main": "dist/index.js", | ||
"umd:main": "dist/index.umd.js", | ||
"module": "dist/index.m.js", | ||
"typings": "dist/index.d.ts", | ||
"source": "src/index.ts", | ||
"files": [ | ||
"dist/", | ||
"package.json", | ||
"README.md" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "libundler watch --ts -e all", | ||
"build": "libundler build --ts -e all --c", | ||
"fix": "run-s fix:*", | ||
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" --write", | ||
"fix:tslint": "tslint --fix --project .", | ||
"tslint": "tslint --project ." | ||
}, | ||
"dependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-loader": "^7.1.4", | ||
"babel-preset-react-app": "^3.1.1", | ||
"docz-core": "^0.2.4", | ||
"happypack": "^5.0.0", | ||
"ts-loader": "^4.4.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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { BabelRC, createPlugin } from 'docz-core' | ||
import HappyPack from 'happypack' | ||
|
||
const findHappypackJsx = (plugin: any) => plugin.config.id === 'jsx' | ||
const findHappypackMdx = (plugin: any) => plugin.config.id === 'mdx' | ||
const findTsxRule = (rule: any) => rule.test.toString() === '/\\.(ts|tsx)$/' | ||
|
||
const modifyHappypackLoader = (plugin: any) => { | ||
plugin.config.loaders[0].loader = require.resolve('babel-loader') | ||
} | ||
|
||
export const babel = () => | ||
createPlugin({ | ||
modifyBabelRc: (babelrc: BabelRC) => { | ||
if (!babelrc.presets) return babelrc | ||
|
||
const idx = babelrc.presets.findIndex(preset => | ||
/babel-preset-react-app/.test(preset) | ||
) | ||
|
||
const tsIdx = babelrc.presets.findIndex(preset => | ||
/@babel\/preset-typescript/.test(preset) | ||
) | ||
|
||
babelrc.presets[idx] = require.resolve('babel-preset-react-app') | ||
tsIdx > -1 && babelrc.presets.splice(tsIdx, 1) | ||
|
||
return babelrc | ||
}, | ||
modifyBundlerConfig: config => { | ||
const happypackJsx = config.plugins.findIndex(findHappypackJsx) | ||
const happypackMdx = config.plugins.findIndex(findHappypackMdx) | ||
const tsxRule = config.module.rules.find(findTsxRule) | ||
const tsxIdx = config.module.rules.findIndex(findTsxRule) | ||
|
||
modifyHappypackLoader(config.plugins[happypackJsx]) | ||
modifyHappypackLoader(config.plugins[happypackMdx]) | ||
|
||
if (tsxRule) { | ||
config.module.rules[tsxIdx] = { | ||
...tsxRule, | ||
use: 'happypack/loader?id=tsx', | ||
} | ||
|
||
config.plugins.push( | ||
new HappyPack({ | ||
id: 'tsx', | ||
loaders: [ | ||
{ | ||
loader: require.resolve('ts-loader'), | ||
options: { | ||
happyPackMode: true, | ||
}, | ||
}, | ||
], | ||
}) | ||
) | ||
} | ||
|
||
return config | ||
}, | ||
}) |
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 @@ | ||
declare module 'happypack' |
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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"declaration": true, | ||
"types": ["node"], | ||
"typeRoots": ["node_modules/@types"] | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules/**"] | ||
} |
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,3 @@ | ||
{ | ||
"extends": "../../tslint.json" | ||
} |