Skip to content

Commit 3826654

Browse files
committed
perf: Use of optimised utils
1 parent b1f4e82 commit 3826654

File tree

9 files changed

+29
-111
lines changed

9 files changed

+29
-111
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@
3131
},
3232
"dependencies": {
3333
"@nuxt/kit": "^3.13.2",
34-
"csso": "^5.0.5",
3534
"ohash": "^1.1.4",
36-
"pathe": "^1.1.2",
37-
"purgecss": "^6.0.0",
38-
"purgecss-from-html": "^6.0.0"
35+
"pathe": "^1.1.2"
3936
},
4037
"devDependencies": {
4138
"@nuxt/devtools": "^1.6.0",

pnpm-lock.yaml

Lines changed: 5 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ export default defineNuxtModule<ModuleOptions>({
105105
nuxt.options.nitro.virtual['#style-extractor/nuxt-style-extractor-transform.js'] = () => {
106106
return fs.readFile(transformFile, 'utf-8')
107107
}
108+
nuxt.options.nitro.virtual['#style-extractor/remove-unused.js'] = () => {
109+
return fs.readFile(resolver.resolve('./runtime/utils/remove-unused.js'), 'utf-8')
110+
}
111+
nuxt.options.nitro.virtual['#style-extractor/minify.js'] = () => {
112+
return fs.readFile(resolver.resolve('./runtime/utils/minify.js'), 'utf-8')
113+
}
108114

109115
nuxt.options.nitro.virtual['#style-extractor/nuxt-style-extractor-cache-control.js'] = () => {
110116
return `const cacheControl = "${_options.cacheControl === null ? '' : _options.cacheControl}";
@@ -123,13 +129,6 @@ export default defineNuxtModule<ModuleOptions>({
123129
},
124130
})
125131

126-
addTemplate({
127-
filename: 'nuxt-style-extractor-transform.js',
128-
getContents() {
129-
return fs.readFile(transformFile, 'utf-8')
130-
},
131-
})
132-
133132
addTypeTemplate({
134133
filename: 'nuxt-style-extractor.d.ts',
135134
getContents() {

src/runtime/nuxt-style-extractor.d.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,23 @@ declare module '#build/nuxt-style-extractor-transform.js' {
1313
export default optimiseCss
1414
}
1515

16-
declare module '#style-extractor/nuxt-style-extractor-transform.js' {
16+
declare module '#style-extractor/nuxt-style-extractor-cache-control.js' {
17+
const cacheControl: string
18+
export default cacheControl
19+
}
20+
21+
declare module '#style-extractor/remove-unused.js' {
1722
export interface Options {
1823
html: string
1924
css: string
2025
name: string
2126
}
2227
type OptimiseCss = (options: Options) => Promise<string> | string
23-
const optimiseCss: OptimiseCss
24-
export default optimiseCss
28+
const removeUnusedCss: OptimiseCss
29+
export const removeUnusedCss
2530
}
2631

27-
declare module '#style-extractor/nuxt-style-extractor-cache-control.js' {
28-
const cacheControl: string
29-
export default cacheControl
32+
declare module '#style-extractor/minify.js' {
33+
const minify: (css: string) => string
34+
export const minify
3035
}

src/runtime/transforms/best.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
1-
import { minify } from 'csso/dist/csso.esm'
2-
import { PurgeCSS } from 'purgecss'
3-
import purgehtml from 'purgecss-from-html'
4-
5-
let purgeCssCtx
6-
async function removeUnusedCss(options) {
7-
const { html, css } = options
8-
if (!purgeCssCtx) {
9-
purgeCssCtx = new PurgeCSS()
10-
}
11-
const [result] = await purgeCssCtx.purge({
12-
content: [{ raw: html, extension: 'html' }],
13-
css: [{ raw: css }],
14-
extractors: [{
15-
extensions: ['html'],
16-
extractor: purgehtml,
17-
}],
18-
})
19-
return result.css || ''
20-
}
1+
import { minify } from '#style-extractor/minify.js'
2+
import { removeUnusedCss } from '#style-extractor/remove-unused.js'
213

224
async function minifyCss(options) {
235
const { css } = options

src/runtime/transforms/minify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { minify } from 'csso/dist/csso.esm'
1+
import { minify } from '#style-extractor/minify.js'
22

33
export default async (options) => {
44
const { css } = options
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
import { PurgeCSS } from 'purgecss'
2-
import purgehtml from 'purgecss-from-html'
3-
4-
let purgeCssCtx
5-
async function removeUnusedCss(options) {
6-
const { html, css } = options
7-
if (!purgeCssCtx) {
8-
purgeCssCtx = new PurgeCSS()
9-
}
10-
const [result] = await purgeCssCtx.purge({
11-
content: [{ raw: html, extension: 'html' }],
12-
css: [{ raw: css }],
13-
extractors: [{
14-
extensions: ['html'],
15-
extractor: purgehtml,
16-
}],
17-
})
18-
return result.css || ''
19-
}
1+
import { removeUnusedCss } from '#style-extractor/remove-unused.js'
202

213
export default options => removeUnusedCss(options)

src/runtime/utils/minify.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/utils/remove-unused.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)