Skip to content

Commit

Permalink
feat(dev): 新增 getBabelPluginImportList
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Feb 22, 2021
1 parent 6192d56 commit 603843e
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 18 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"./x": {
"require": "./_cjs/x/index.js",
"import": "./x/index.js"
},
"./dev": {
"require": "./_cjs/dev/index.js",
"import": "./dev/index.js"
}
},
"main": "_cjs/utils/index.js",
Expand Down Expand Up @@ -95,8 +99,11 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@types/benchmark": "2.1.0",
"@types/ioredis": "^4.17.9",
"@types/tough-cookie": "^4.0.0",
"benchmark": "2.1.4",
"cli-table3": "0.6.0",
"cuid": "^2.1.8",
"date-fns": "^2.16.1",
"fast-xml-parser": "^3.17.5",
Expand All @@ -120,15 +127,12 @@
"@tarojs/taro": "3.0.27",
"@testing-library/react": "11.2.5",
"@testing-library/react-hooks": "3.7.0",
"@types/benchmark": "2.1.0",
"@types/fs-extra": "9.0.7",
"@types/prompts": "2.0.9",
"@types/react": "17.0.2",
"@types/react-dom": "17.0.1",
"@types/standard-version": "7.0.0",
"@types/uuid": "8.3.0",
"benchmark": "2.1.4",
"cli-table3": "0.6.0",
"codecov": "3.8.1",
"cross-env": "7.0.3",
"eslint": "7.20.0",
Expand Down
22 changes: 8 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/date/numeralDayToChineseDay.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { numeralDayToChineseDay } from './numeralDayToChineseDay'

describe(numeralDayToChineseDay.name, () => {
describe('numeralDayToChineseDay', () => {
test('转换正常', () => {
expect(numeralDayToChineseDay(0)).toBe('日')
expect(numeralDayToChineseDay(1)).toBe('一')
Expand Down
29 changes: 29 additions & 0 deletions src/dev/__snapshots__/getBabelPluginImportList.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getBabelPluginImportList 转换正常 1`] = `
"\\"use strict\\";
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
var _de2 = _interopRequireDefault(require(\\"date-fns/esm/locale/de\\"));
var _getDay2 = _interopRequireDefault(require(\\"date-fns/esm/getDay\\"));
var _zhCN2 = _interopRequireDefault(require(\\"date-fns/esm/locale/zhCN\\"));
var _getDate2 = _interopRequireDefault(require(\\"date-fns/esm/getDate\\"));
var _anyToDate2 = _interopRequireDefault(require(\\"vtils/date/anyToDate\\"));
var _getTopBarInfo2 = _interopRequireDefault(require(\\"vtils/mp/getTopBarInfo\\"));
var _utils = _interopRequireDefault(require(\\"../utils\\"));
var _uniq2 = _interopRequireDefault(require(\\"lodash-es/uniq\\"));
var _sampleIndex2 = _interopRequireDefault(require(\\"vtils/utils/sampleIndex\\"));
var _sample2 = _interopRequireDefault(require(\\"lodash-es/sample\\"));
console.log(_sample2.default, _sampleIndex2.default, _utils.default, _utils.default, _anyToDate2.default, _getDate2.default, _zhCN2.default, _getDay2.default, _de2.default, _uniq2.default, _getTopBarInfo2.default);"
`;
30 changes: 30 additions & 0 deletions src/dev/getBabelPluginImportList.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as babel from '@babel/core'
import { dedent } from '../utils'
import { getBabelConfig } from 'haoma'
import { getBabelPluginImportList } from './getBabelPluginImportList'

describe('getBabelPluginImportList', () => {
test('转换正常', async () => {
const code = dedent`
import { sample, sampleIndex } from 'vtils'
import { wait, sampleBy } from '../utils'
import { uniq } from 'lodash-uni'
import { anyToDate, getDate, zhCN } from 'vtils/date'
import { getDay } from 'date-fns/esm'
import { de } from 'date-fns/esm/locale'
import { getTopBarInfo } from 'vtils/mp'
console.log(sample, sampleIndex, wait, sampleBy, anyToDate, getDate, zhCN, getDay, de, uniq, getTopBarInfo)
`
const res = await babel.transformAsync(
code,
getBabelConfig({
filename: __filename,
target: 'browser',
module: 'cjs',
plugins: getBabelPluginImportList(),
}),
)
expect(res!.code!).toMatchSnapshot()
})
})
127 changes: 127 additions & 0 deletions src/dev/getBabelPluginImportList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as dateFns from 'date-fns'
import * as dateFnsLocale from 'date-fns/locale'
import * as lodash from 'lodash-uni'
import * as path from 'path'

export function getBabelPluginImportList() {
// @ts-ignore
const resolve = require.resolve
const vtilsPath = path.dirname(resolve('vtils/package.json'))

return [
// utils
[
resolve('babel-plugin-import'),
{
libraryName: 'vtils',
camel2DashComponentName: false,
customName: (name: string) => {
if (name in lodash) {
return `lodash-es/${name}`
}
return `vtils/utils/${name}`
},
},
'vtils.utils.0',
],
[
resolve('babel-plugin-import'),
{
libraryName: 'vtils/utils',
camel2DashComponentName: false,
customName: (name: string) => {
if (name in lodash) {
return `lodash-es/${name}`
}
return `vtils/utils/${name}`
},
},
'vtils.utils.1',
],
[
resolve('babel-plugin-import'),
{
libraryName: 'lodash-uni',
camel2DashComponentName: false,
customName: (name: string) => `lodash-es/${name}`,
},
'vtils.utils.2',
],
[
resolve('babel-plugin-import'),
{
libraryName: '../utils',
camel2DashComponentName: false,
customName: (name: string, file: { opts: { filename: string } }) => {
if (!file.opts.filename.startsWith(vtilsPath)) {
return '../utils'
}
if (name in lodash) {
return `lodash-es/${name}`
}
return `../utils/${name}`
},
},
'vtils.utils.3',
],

// mp
[
resolve('babel-plugin-import'),
{
libraryName: 'vtils/mp',
camel2DashComponentName: false,
customName: (name: string) => `vtils/mp/${name}`,
},
'vtils.mp.0',
],

// react
[
resolve('babel-plugin-import'),
{
libraryName: 'vtils/react',
camel2DashComponentName: false,
customName: (name: string) => `vtils/react/${name}`,
},
'vtils.react.0',
],

// date
[
resolve('babel-plugin-import'),
{
libraryName: 'vtils/date',
camel2DashComponentName: false,
customName: (name: string) => {
if (name in dateFns) {
return `date-fns/esm/${name}`
}
if (name in dateFnsLocale) {
return `date-fns/esm/locale/${name}`
}
return `vtils/date/${name}`
},
},
'vtils.date.0',
],
[
resolve('babel-plugin-import'),
{
libraryName: 'date-fns/esm',
camel2DashComponentName: false,
customName: (name: string) => `date-fns/esm/${name}`,
},
'vtils.date.1',
],
[
resolve('babel-plugin-import'),
{
libraryName: 'date-fns/esm/locale',
camel2DashComponentName: false,
customName: (name: string) => `date-fns/esm/locale/${name}`,
},
'vtils.date.2',
],
]
}
1 change: 1 addition & 0 deletions src/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './expectType'
export * from './getBabelPluginImportList'
export * from './runBenchmark'
// @endindex

0 comments on commit 603843e

Please sign in to comment.