chore(docz-utils): add named asset import as utils

This commit is contained in:
Pedro Nauck
2018-09-28 13:18:44 -03:00
parent a86a3e164d
commit 6cb93eaa60
6 changed files with 8 additions and 18 deletions

View File

@@ -1,9 +0,0 @@
{
"name": "babel-plugin-named-asset-import",
"version": "0.11.1",
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"@babel/core": "^7.1.0"
}
}

View File

@@ -32,7 +32,6 @@
"@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",

View File

@@ -30,7 +30,7 @@ export const getBabelConfig = async (
const defaultPlugins: any[] = [
[
require.resolve('babel-plugin-named-asset-import'),
require.resolve('docz-utils/lib/named-asset-import'),
{
loaderMap: {
svg: {

View File

@@ -13,7 +13,8 @@
"src/codesandbox.ts",
"src/format.ts",
"src/imports.ts",
"src/jsx.ts"
"src/jsx.ts",
"src/named-asset-import.ts"
],
"files": [
"lib/",

View File

@@ -5,4 +5,5 @@ import * as imports from './imports'
import * as codesandbox from './codesandbox'
import { format } from './format'
export { default as namedAssetImport } from './named-asset-import'
export { ast, mdast, format, imports, jsx, codesandbox }

View File

@@ -1,11 +1,11 @@
const { extname } = require('path')
import { extname } from 'path'
function namedAssetImportPlugin({ types: t }) {
export default function namedAssetImport({ types: t }: any): any {
const visited = new WeakSet()
return {
visitor: {
ImportDeclaration(path, { opts: { loaderMap } }) {
ImportDeclaration(path: any, { opts: { loaderMap } }: any): any {
const sourcePath = path.node.source.value
const ext = extname(sourcePath).substr(1)
@@ -15,7 +15,7 @@ function namedAssetImportPlugin({ types: t }) {
if (loaderMap[ext]) {
path.replaceWithMultiple(
path.node.specifiers.map(specifier => {
path.node.specifiers.map((specifier: any) => {
if (t.isImportDefaultSpecifier(specifier)) {
const newDefaultImport = t.importDeclaration(
[
@@ -56,5 +56,3 @@ function namedAssetImportPlugin({ types: t }) {
},
}
}
module.exports = namedAssetImportPlugin