-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
2,904 additions
and
2,476 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
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 |
---|---|---|
@@ -1,35 +1,77 @@ | ||
import { IOptions } from './type' | ||
|
||
function replace(options: IOptions) { | ||
const sourceLibrary = options.sourceLibrary || [] | ||
interface ReplaceOptions extends Omit<IOptions, 'exclude'> { | ||
exclude: { [key: string]: boolean } | ||
} | ||
|
||
const skip = new Set() | ||
|
||
function replace(options: ReplaceOptions) { | ||
const sourceLibraries = options.sourceLibrary || [] | ||
const targetLibrary = options.targetLibrary | ||
return { | ||
return ({ types: t }) => ({ | ||
visitor: { | ||
ImportDeclaration(path) { | ||
if (sourceLibrary.indexOf(path.node.source.value) > -1) { | ||
// 替换包名称 | ||
path.node.source.value = targetLibrary | ||
path.node.specifiers.forEach((specifier) => { | ||
// 根据 iconMappings 进行替换 | ||
const iconMappings = options.iconMappings | ||
if (iconMappings && iconMappings[specifier.imported.name]) { | ||
specifier.imported.name = iconMappings[specifier.imported.name] | ||
} | ||
}) | ||
if ( | ||
sourceLibraries.indexOf(path.node.source.value) > -1 && | ||
!skip.has(path.node) | ||
) { | ||
try { | ||
const updatedImports: Array<any> = [] | ||
path.node.specifiers.forEach((specifier) => { | ||
const localIdentifier = | ||
specifier.local.name || specifier.local.value | ||
const importedIdentifier = | ||
specifier.imported.name || specifier.imported.value | ||
const iconMappings = options.iconMappings | ||
// delete this and build new segmentent | ||
const newLibraryName = options.exclude[importedIdentifier] | ||
? path.node.source.value | ||
: targetLibrary | ||
const finalImportedName = | ||
iconMappings && iconMappings[importedIdentifier] | ||
? iconMappings[importedIdentifier] | ||
: importedIdentifier | ||
const newImport = t.ImportDeclaration( | ||
[ | ||
t.ImportSpecifier( | ||
t.identifier(localIdentifier), | ||
t.identifier(finalImportedName) | ||
), | ||
], | ||
t.StringLiteral(newLibraryName) | ||
) | ||
updatedImports.push(newImport) | ||
skip.add(newImport) | ||
}) | ||
const programPath = path.findParent((path) => path.isProgram()) | ||
updatedImports.forEach((imported) => { | ||
programPath.unshiftContainer('body', imported) | ||
}) | ||
path.remove() | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
}) | ||
} | ||
|
||
export function replaceIcons( | ||
options: IOptions = { | ||
sourceLibrary: ['@nutui/icons-react', '@nutui/icons-react-taro'], | ||
targetLibrary: '', | ||
exclude: [], | ||
} | ||
) { | ||
if (!options.targetLibrary) { | ||
return {} | ||
} | ||
return replace(options) | ||
// options exclude covert to object | ||
const excludeHashMap = {} | ||
options.exclude?.forEach((exc) => { | ||
excludeHashMap[exc] = true | ||
}) | ||
return replace({ ...options, exclude: excludeHashMap }) | ||
} |
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
10 changes: 10 additions & 0 deletions
10
packages/nutui-replace-icons/test/__snapshots__/exclude-case-inline.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,10 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`replace Loading icons with Star 1`] = ` | ||
"import { Loading1 } from "@nutui/icons-react"; | ||
import { Star as Loading } from "@test/aa"; | ||
import { ArrowSize6 as Arrow } from "@nutui/icons-react"; | ||
const ReplaceOne = () => { | ||
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Loading, null), " ", /*#__PURE__*/React.createElement(Arrow, null)); | ||
};" | ||
`; |
10 changes: 10 additions & 0 deletions
10
packages/nutui-replace-icons/test/__snapshots__/exclude-case.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,10 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`replace Loading icons with Star 1`] = ` | ||
"import { ArrowSize6 as Arrow } from "@nutui/icons-react"; | ||
import { Loading1 } from "@nutui/icons-react"; | ||
import { Star as Loading } from "@test/aa"; | ||
const ReplaceOne = () => { | ||
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Loading, null), " ", /*#__PURE__*/React.createElement(Arrow, null)); | ||
};" | ||
`; |
4 changes: 2 additions & 2 deletions
4
packages/nutui-replace-icons/test/__snapshots__/replace-case.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
30 changes: 30 additions & 0 deletions
30
packages/nutui-replace-icons/test/exclude-case-inline.test.ts
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 babel from '@babel/core' | ||
import { describe, expect, it } from 'vitest' | ||
import { replaceIcons } from '../src/replace-icons' | ||
|
||
const plugin = replaceIcons({ | ||
targetLibrary: '@test/aa', | ||
sourceLibrary: ['@nutui/icons-react'], | ||
iconMappings: { | ||
Loading: 'Star', | ||
}, | ||
exclude: ['Loading1', 'ArrowSize6'], | ||
}) | ||
|
||
const babelOptions = { | ||
presets: ['@babel/preset-react'], | ||
plugins: [plugin], | ||
} | ||
const caseIns = ` | ||
import { ArrowSize6 as Arrow, Loading, Loading1 } from '@nutui/icons-react' | ||
const ReplaceOne = () => { | ||
return <><Loading /> <Arrow /></> | ||
} | ||
` | ||
describe('', () => { | ||
it('replace Loading icons with Star', () => { | ||
const ast = babel.transformSync(caseIns, babelOptions) | ||
// @ts-ignore | ||
expect(ast.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,32 @@ | ||
import babel from '@babel/core' | ||
import { describe, expect, it } from 'vitest' | ||
import { replaceIcons } from '../src/replace-icons' | ||
|
||
const plugin = replaceIcons({ | ||
targetLibrary: '@test/aa', | ||
sourceLibrary: ['@nutui/icons-react'], | ||
iconMappings: { | ||
Loading: 'Star', | ||
}, | ||
exclude: ['Loading1', 'ArrowSize6'], | ||
}) | ||
|
||
const babelOptions = { | ||
presets: ['@babel/preset-react'], | ||
plugins: [plugin], | ||
} | ||
const caseIns = ` | ||
import { Loading } from '@nutui/icons-react' | ||
import { Loading1 } from '@nutui/icons-react' | ||
import { ArrowSize6 as Arrow } from '@nutui/icons-react' | ||
const ReplaceOne = () => { | ||
return <><Loading /> <Arrow /></> | ||
} | ||
` | ||
describe('', () => { | ||
it('replace Loading icons with Star', () => { | ||
const ast = babel.transformSync(caseIns, babelOptions) | ||
// @ts-ignore | ||
expect(ast.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
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,7 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
// ... Specify options here. | ||
}, | ||
}) |
Oops, something went wrong.