Skip to content

Commit d1a3c26

Browse files
develarantfu
andauthored
feat: auto import of style files (#28)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 52fb96d commit d1a3c26

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

src/context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ export class Context {
133133

134134
this._componentNameMap[name] = {
135135
name,
136-
absolute: path,
137136
path: `/${this.relative(path)}`,
138137
}
139138
})
@@ -158,8 +157,7 @@ export class Context {
158157
else {
159158
return {
160159
name,
161-
path: result.path,
162-
importName: result.importName,
160+
...result,
163161
}
164162
}
165163
}

src/types.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
export type ComponentResolveResult = string | { path: string; importName?: string }
1+
export interface ImportInfo {
2+
name?: string
3+
importName?: string
4+
path: string
5+
}
6+
7+
export interface ComponentInfo extends ImportInfo {
8+
sideEffects?: (ImportInfo | string)[] | ImportInfo | string
9+
}
10+
11+
export type ComponentResolveResult = string | ComponentInfo
212

313
export type ComponentResolver = (name: string) => ComponentResolveResult | null | undefined | void
414

@@ -91,11 +101,4 @@ Required<Options>,
91101
globs: string[]
92102
}
93103

94-
export interface ComponentInfo {
95-
name: string
96-
path: string
97-
absolute?: string
98-
importName?: string
99-
}
100-
101104
export type ComponentsImportMap = Record<string, string[] | undefined>

src/utils.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { join, parse, resolve } from 'path'
22
import minimatch from 'minimatch'
33
import { ResolvedConfig } from 'vite'
4-
import { ComponentInfo, ResolvedOptions, Options } from './types'
4+
import { ComponentInfo, ResolvedOptions, Options, ImportInfo } from './types'
55
import { LibraryResolver } from './helpers/libraryResolver'
66
import { defaultOptions } from './constants'
77
import { Context } from './context'
@@ -69,17 +69,32 @@ export function matchGlobs(filepath: string, globs: string[]) {
6969
return false
7070
}
7171

72-
export function stringifyComponentImport({ name, path, importName }: ComponentInfo, ctx: Context) {
72+
export function stringifyImport(info: ImportInfo | string) {
73+
if (typeof info === 'string')
74+
return `import '${info}'`
75+
if (!info.name)
76+
return `import '${info.path}'`
77+
else if (info.importName)
78+
return `import { ${info.importName} as ${info.name} } from '${info.path}'`
79+
else
80+
return `import ${info.name} from '${info.path}'`
81+
}
82+
83+
export function stringifyComponentImport({ name, path, importName, sideEffects }: ComponentInfo, ctx: Context) {
7384
if (ctx.options.importPathTransform) {
7485
const result = ctx.options.importPathTransform(path)
7586
if (result != null)
7687
path = result
7788
}
7889

79-
if (importName)
80-
return `import { ${importName} as ${name} } from '${path}'`
81-
else
82-
return `import ${name} from '${path}'`
90+
const imports = [
91+
stringifyImport({ name, path, importName }),
92+
]
93+
94+
if (sideEffects)
95+
toArray(sideEffects).forEach(i => imports.push(stringifyImport(i)))
96+
97+
return imports.join('\n')
8398
}
8499

85100
export function resolveOptions(options: Options, viteConfig: ResolvedConfig): ResolvedOptions {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`stringifyComponentImport importName 1`] = `"import { a as Test } from 'test'"`;
4+
5+
exports[`stringifyComponentImport multiple sideEffects 1`] = `
6+
"import Test from 'test'
7+
import 'test.css'
8+
import css from 'test2.css'"
9+
`;
10+
11+
exports[`stringifyComponentImport plain css sideEffects 1`] = `
12+
"import Test from 'test'
13+
import 'test.css'"
14+
`;

test/stringifyComponentImport.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Context } from '../src/context'
2+
import { stringifyComponentImport } from '../src/utils'
3+
4+
describe('stringifyComponentImport', () => {
5+
it('importName', async() => {
6+
const ctx = new Context({}, { root: '' } as any)
7+
expect(
8+
stringifyComponentImport({
9+
name: 'Test',
10+
path: 'test',
11+
importName: 'a',
12+
}, ctx),
13+
).toMatchSnapshot()
14+
})
15+
16+
it('plain css sideEffects', async() => {
17+
const ctx = new Context({}, { root: '' } as any)
18+
expect(
19+
stringifyComponentImport({
20+
name: 'Test',
21+
path: 'test',
22+
sideEffects: 'test.css',
23+
}, ctx),
24+
).toMatchSnapshot()
25+
})
26+
27+
it('multiple sideEffects', async() => {
28+
const ctx = new Context({}, { root: '' } as any)
29+
expect(
30+
stringifyComponentImport({
31+
name: 'Test',
32+
path: 'test',
33+
sideEffects: [
34+
'test.css',
35+
{ name: 'css', path: 'test2.css' },
36+
],
37+
}, ctx),
38+
).toMatchSnapshot()
39+
})
40+
})

0 commit comments

Comments
 (0)