mirror of
https://github.com/zhigang1992/docz.git
synced 2026-04-29 01:45:54 +08:00
feat(docz-core): add svgr built-in
This commit is contained in:
60
packages/babel-plugin-named-asset-import/index.js
Normal file
60
packages/babel-plugin-named-asset-import/index.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const { extname } = require('path')
|
||||
|
||||
function namedAssetImportPlugin({ types: t }) {
|
||||
const visited = new WeakSet()
|
||||
|
||||
return {
|
||||
visitor: {
|
||||
ImportDeclaration(path, { opts: { loaderMap } }) {
|
||||
const sourcePath = path.node.source.value
|
||||
const ext = extname(sourcePath).substr(1)
|
||||
|
||||
if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
|
||||
return
|
||||
}
|
||||
|
||||
if (loaderMap[ext]) {
|
||||
path.replaceWithMultiple(
|
||||
path.node.specifiers.map(specifier => {
|
||||
if (t.isImportDefaultSpecifier(specifier)) {
|
||||
const newDefaultImport = t.importDeclaration(
|
||||
[
|
||||
t.importDefaultSpecifier(
|
||||
t.identifier(specifier.local.name)
|
||||
),
|
||||
],
|
||||
t.stringLiteral(sourcePath)
|
||||
)
|
||||
|
||||
visited.add(newDefaultImport)
|
||||
return newDefaultImport
|
||||
}
|
||||
|
||||
const newImport = t.importDeclaration(
|
||||
[
|
||||
t.importSpecifier(
|
||||
t.identifier(specifier.local.name),
|
||||
t.identifier(specifier.imported.name)
|
||||
),
|
||||
],
|
||||
t.stringLiteral(
|
||||
loaderMap[ext][specifier.imported.name]
|
||||
? loaderMap[ext][specifier.imported.name].replace(
|
||||
/\[path\]/,
|
||||
sourcePath
|
||||
)
|
||||
: sourcePath
|
||||
)
|
||||
)
|
||||
|
||||
visited.add(newImport)
|
||||
return newImport
|
||||
})
|
||||
)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = namedAssetImportPlugin
|
||||
9
packages/babel-plugin-named-asset-import/package.json
Normal file
9
packages/babel-plugin-named-asset-import/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "babel-plugin-named-asset-import",
|
||||
"version": "0.11.1",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"peerDependencies": {
|
||||
"@babel/core": "^7.1.0"
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,10 @@
|
||||
"@mdx-js/mdx": "^0.15.2",
|
||||
"@mdx-js/mdxast": "^0.15.0",
|
||||
"@sindresorhus/slugify": "^0.3.0",
|
||||
"@svgr/webpack": "^2.4.1",
|
||||
"art-template": "^4.13.0",
|
||||
"babel-loader": "^8.0.2",
|
||||
"babel-plugin-named-asset-import": "^0.11.1",
|
||||
"babel-preset-docz": "^0.11.1",
|
||||
"babylon": "^6.18.0",
|
||||
"cache-loader": "^1.2.2",
|
||||
@@ -74,9 +76,9 @@
|
||||
"remark-slug": "^5.1.0",
|
||||
"resolve": "^1.8.1",
|
||||
"signale": "^1.3.0",
|
||||
"terser-webpack-plugin": "^1.1.0",
|
||||
"titleize": "^1.0.1",
|
||||
"to-vfile": "^5.0.1",
|
||||
"terser-webpack-plugin": "^1.1.0",
|
||||
"unified": "^7.0.0",
|
||||
"unist-util-find": "^1.0.1",
|
||||
"unist-util-is": "^2.1.2",
|
||||
|
||||
@@ -42,12 +42,7 @@ export const setupHappypack = (config: Config, args: Args, babelrc: any) => {
|
||||
},
|
||||
{
|
||||
loader: require.resolve('babel-loader'),
|
||||
options: {
|
||||
...babelrc,
|
||||
plugins: babelrc.plugins
|
||||
.filter((p: string) => /react\-hot\-loader\/babel/.test(p))
|
||||
.filter((p: string) => /babel\-plugin\-react\-docgen/.test(p)),
|
||||
},
|
||||
options: babelrc,
|
||||
},
|
||||
].filter(Boolean),
|
||||
}
|
||||
|
||||
@@ -28,15 +28,26 @@ export const getBabelConfig = async (
|
||||
],
|
||||
]
|
||||
|
||||
const plugins: any[] = !isProd
|
||||
? [require.resolve('react-hot-loader/babel')]
|
||||
: []
|
||||
const defaultPlugins: any[] = [
|
||||
[
|
||||
require.resolve('babel-plugin-named-asset-import'),
|
||||
{
|
||||
loaderMap: {
|
||||
svg: {
|
||||
ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
]
|
||||
|
||||
const config = merge(localBabelRc, {
|
||||
presets,
|
||||
plugins,
|
||||
cacheDirectory: !args.debug,
|
||||
babelrc: false,
|
||||
plugins: defaultPlugins.concat(
|
||||
!isProd ? [require.resolve('react-hot-loader/babel')] : []
|
||||
),
|
||||
})
|
||||
|
||||
const reduce = Plugin.reduceFromPlugins<BabelRC>(args.plugins)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
# docz-plugin-svgr [MOVED]
|
||||
# docz-plugin-svgr [DEPRECATED]
|
||||
|
||||
In order to improve monorepo performance, we moved it to [another repo](https://github.com/pedronauck/docz-plugin-svgr)!
|
||||
This plugin is deprecated. Now svgr loader is built in docz.
|
||||
|
||||
Reference in New Issue
Block a user