Skip to content

Commit

Permalink
feat: add ignoreComponents config option (#176)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
Fix #165
  • Loading branch information
Ragura committed Feb 1, 2023
1 parent ae37dd7 commit 1a98ab5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,24 @@ type Prefix = string
import { AlButton } from 'xx-lib'
```
### `ignoreComponents`
```ts
type IgnoreComponents = string[]
```
Skip style imports for a list of components. Useful for Element Plus components which do not have a style file.
At the time of writing, this is only the `AutoResizer` component.
```javascript
// format: 'cjs'
import { ElAutoResizer } from 'element-plus'
// ↓ ↓ ↓ ↓ ↓ ↓
import { ElAutoResizer } from 'element-plus'
```
### `defaultLocale`
Replace default locale, you can find locale list [here](https://github.com/element-plus/element-plus/tree/dev/packages/locale/lang).
Expand Down
7 changes: 5 additions & 2 deletions src/core/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export const transformImportStyle = (
prefix: string
lib: string
format: FormatType
ignoreComponents: string[]
}
) => {
const { prefix, lib, format } = options
const { prefix, lib, format, ignoreComponents } = options
const statement = stripeComments(source.slice(specifier.ss, specifier.se))
const leftBracket = statement.indexOf('{')
if (leftBracket > -1) {
Expand All @@ -46,6 +47,7 @@ export const transformImportStyle = (
const trimmed = c.replace(/\sas\s.+/, '').trim()
if (trimmed.startsWith(prefix)) {
const component = trimmed.slice(prefix.length)
if (ignoreComponents.includes(component)) return
if (useSource) {
styleImports.push(
`import '${lib}/${formatMap[format]}/components/${hyphenate(
Expand All @@ -66,7 +68,7 @@ export const transformImportStyle = (
}

export const transformStyle = async (source: string, options: Options) => {
const { useSource, lib, prefix, format } = options
const { useSource, lib, prefix, format, ignoreComponents } = options

if (!source) return

Expand All @@ -84,6 +86,7 @@ export const transformStyle = async (source: string, options: Options) => {
lib,
prefix,
format,
ignoreComponents,
})
return ret
})
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultOptions: Options = {
include: ['**/*.vue', '**/*.ts', '**/*.js', '**/*.tsx', '**/*.jsx'],
exclude: [/[/\\]node_modules[/\\]/, /[/\\]\.git[/\\]/, /[/\\]\.nuxt[/\\]/],
lib: 'element-plus',
ignoreComponents: [],
useSource: false,
defaultLocale: '',
format: 'esm',
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export type Options = {
/** replace default locale */
defaultLocale: string

/**
* Array of component names that will not be transformed.
* Can be useful for components that do not have an associated style file.
* Do not include the prefix in the name.
*/
ignoreComponents: string[]

lib: string
prefix: string
format: 'cjs' | 'esm'
Expand Down
16 changes: 16 additions & 0 deletions tests/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ console.log(ElButton, ElCol);
"
`;

exports[`transform > fixtures > tests/fixtures/ignored.ts > useSource = false 1`] = `
"import { ElAutoResizer } from 'element-plus';
import 'element-plus/es/components/button/style/css';
console.log(ElAutoResizer);
"
`;

exports[`transform > fixtures > tests/fixtures/ignored.ts > useSource = true 1`] = `
"import { ElAutoResizer } from 'element-plus';
import 'element-plus/es/components/button/style/index';
console.log(ElAutoResizer);
"
`;

exports[`transform > fixtures > tests/fixtures/import-alias.ts > useSource = false 1`] = `
"import { ElButton, ElCard } from 'element-plus';
import 'element-plus/es/components/button/style/css';
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/ignored.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ElAutoResizer, ElButton } from 'element-plus'

console.log(ElAutoResizer)
1 change: 1 addition & 0 deletions tests/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('transform', () => {
filepath,
plugin({
useSource,
ignoreComponents: ['AutoResizer'],
})
)
expect(code).toMatchSnapshot()
Expand Down

0 comments on commit 1a98ab5

Please sign in to comment.