Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore empty style when compiler #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function transformMain(

// feature information
const hasScoped = descriptor.styles.some(s => s.scoped)
const hasCssModules = descriptor.styles.some(s => s.module)
const hasCssModules = descriptor.styles.some(s => s.module && !!s.content && !!s.content.trim())
Copy link

@thebanjomatic thebanjomatic Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a trimmed copy of the string to see if it is empty isn't a very fast (or memory efficient) way to check to see if the string is only whitespace. The current approach you are using appears to be about 40% slower than:

const hasNonWhitespaceContent = (content: string | undefined) => !!content && /\S/.test(content)

const hasFunctional =
descriptor.template && descriptor.template.attrs.functional

Expand Down Expand Up @@ -279,6 +279,7 @@ async function genStyleCode(
if (descriptor.styles.length) {
for (let i = 0; i < descriptor.styles.length; i++) {
const style = descriptor.styles[i]
if (!style.src && (!style.content || !style.content.trim())) continue
if (style.src) {
await linkSrcToDescriptor(
style.src,
Expand Down
11 changes: 11 additions & 0 deletions test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export function declareTests(isBuild: boolean) {
}
})

test('SFC empty style', async() => {
const el = await page.$$eval('style', (elements) => {
return elements.map((e) => {
return e.innerHTML?.trim()
}).filter((e) => {
return !e
})
})
expect(el).toHaveLength(0)
})

test('SFC <custom>', async () => {
expect(await getText('.custom-block')).toMatch('Custom Block')
expect(await getText('.custom-block-lang')).toMatch('Custom Block')
Expand Down