-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev): 新增 getBabelPluginImportList
- Loading branch information
Showing
7 changed files
with
203 additions
and
18 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
src/dev/__snapshots__/getBabelPluginImportList.test.ts.snap
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,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);" | ||
`; |
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,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() | ||
}) | ||
}) |
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,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', | ||
], | ||
] | ||
} |
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