Skip to content

Commit

Permalink
feat(docz-plugin-babel6): add package
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jun 17, 2018
1 parent 420eb88 commit 68e364a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/docz-plugin-babel6/package.json
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"
}
}
62 changes: 62 additions & 0 deletions packages/docz-plugin-babel6/src/index.ts
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
},
})
1 change: 1 addition & 0 deletions packages/docz-plugin-babel6/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'happypack'
12 changes: 12 additions & 0 deletions packages/docz-plugin-babel6/tsconfig.json
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/**"]
}
3 changes: 3 additions & 0 deletions packages/docz-plugin-babel6/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tslint.json"
}

0 comments on commit 68e364a

Please sign in to comment.