Skip to content

Commit 0dbcb20

Browse files
committed
chore: update deps
1 parent a4e9163 commit 0dbcb20

File tree

6 files changed

+323
-292
lines changed

6 files changed

+323
-292
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@
2323
},
2424
"homepage": "https://github.com/vbenjs/vite-plugin-html/tree/master/#readme",
2525
"devDependencies": {
26-
"@commitlint/cli": "^16.1.0",
27-
"@commitlint/config-conventional": "^16.0.0",
26+
"@commitlint/cli": "^16.2.1",
27+
"@commitlint/config-conventional": "^16.2.1",
2828
"@types/html-minifier-terser": "^6.1.0",
2929
"@types/jsdom": "^16.2.14",
30-
"@types/node": "^17.0.16",
31-
"@typescript-eslint/eslint-plugin": "^5.11.0",
32-
"@typescript-eslint/parser": "^5.11.0",
30+
"@types/node": "^17.0.19",
31+
"@typescript-eslint/eslint-plugin": "^5.12.1",
32+
"@typescript-eslint/parser": "^5.12.1",
3333
"commitizen": "^4.2.4",
3434
"conventional-changelog-cli": "^2.2.2",
3535
"cross-env": "^7.0.3",
36-
"eslint": "^8.8.0",
37-
"eslint-config-prettier": "^8.3.0",
36+
"eslint": "^8.9.0",
37+
"eslint-config-prettier": "^8.4.0",
3838
"eslint-plugin-html": "^6.2.0",
3939
"husky": "^7.0.4",
40-
"lint-staged": "^12.3.3",
40+
"lint-staged": "^12.3.4",
4141
"prettier": "^2.5.1",
4242
"rimraf": "^3.0.2",
4343
"tsup": "^5.11.13",
4444
"typescript": "^4.5.5",
4545
"unbuild": "^0.6.9",
46-
"vite": "^2.8.0",
47-
"vitest": "^0.3.0"
46+
"vite": "^2.8.4",
47+
"vitest": "^0.5.1"
4848
},
4949
"lint-staged": {
5050
"*": [

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
"@types/ejs": "^3.1.0",
6363
"@types/fs-extra": "^9.0.13",
6464
"@types/html-minifier-terser": "^6.1.0",
65-
"@types/node": "^17.0.16",
65+
"@types/node": "^17.0.19",
6666
"typescript": "^4.5.5",
67-
"vite": "^2.8.0"
67+
"vite": "^2.8.4"
6868
}
6969
}

packages/core/src/htmlPlugin.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { InjectOptions, PageOption, Pages, UserOptions } from './typing'
33
import { render } from 'ejs'
44
import { cleanUrl, isDirEmpty, loadEnv } from './utils'
55
import { htmlFilter } from './utils/createHtmlFilter'
6-
import { mergeConfig, normalizePath } from 'vite'
6+
import { normalizePath } from 'vite'
77
import { parse } from 'node-html-parser'
8-
import fse from 'fs-extra'
9-
import { resolve, dirname, basename } from 'pathe'
8+
import fs from 'fs-extra'
9+
import path from 'pathe'
1010
import fg from 'fast-glob'
1111
import consola from 'consola'
1212
import { dim } from 'colorette'
@@ -37,6 +37,7 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
3737
},
3838
config(conf) {
3939
const input = createInput(userOptions, conf as unknown as ResolvedConfig)
40+
4041
if (input) {
4142
return {
4243
build: {
@@ -69,16 +70,20 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
6970
excludeBaseUrl === '/' ? DEFAULT_TEMPLATE : url.replace('/', '')
7071

7172
const page = getPage(userOptions, htmlName, viteConfig)
73+
7274
const { injectOptions = {} } = page
7375
let html = await getHtmlInPages(page, viteConfig.root)
76+
7477
html = await renderHtml(html, {
7578
injectOptions,
7679
viteConfig,
7780
env,
7881
entry: page.entry || entry,
7982
verbose,
8083
})
81-
html = await server.transformIndexHtml?.(url, html, req.originalUrl)
84+
if (server.transformIndexHtml) {
85+
html = await server.transformIndexHtml(url, html, req.originalUrl)
86+
}
8287
res.end(html)
8388
} catch (e) {
8489
consola.log(e)
@@ -87,9 +92,12 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
8792
},
8893
transform(code, id): Promise<TransformResult> | TransformResult {
8994
if (viteConfig.command === 'build' && htmlFilter(id)) {
90-
const htmlName = id.match('[^/]+(?!.*/)')?.[0] ?? DEFAULT_TEMPLATE
95+
const htmlName = id
96+
.replace(path.join(process.cwd(), viteConfig.base), '/')
97+
.replace(/\/public/, '')
9198

9299
const page = getPage(userOptions, htmlName, viteConfig)
100+
93101
const { injectOptions = {} } = page
94102
return getHtmlInPages(page, viteConfig.root).then((html) => {
95103
return renderHtml(html, {
@@ -117,38 +125,40 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
117125

118126
if (isMpa(viteConfig) || pages.length) {
119127
for (const page of pages) {
120-
const dir = dirname(page.template)
128+
const dir = path.dirname(page.template)
121129
if (!ignoreDirs.includes(dir)) {
122130
outputDirs.push(dir)
123131
}
124132
}
125133
} else {
126-
const dir = dirname(template)
134+
const dir = path.dirname(template)
127135
if (!ignoreDirs.includes(dir)) {
128136
outputDirs.push(dir)
129137
}
130138
}
131-
const cwd = resolve(viteConfig.root, viteConfig.build.outDir)
139+
const cwd = path.resolve(viteConfig.root, viteConfig.build.outDir)
132140
const htmlFiles = await fg(
133141
outputDirs.map((dir) => `${dir}/*.html`),
134-
{ cwd: resolve(cwd), absolute: true },
142+
{ cwd: path.resolve(cwd), absolute: true },
135143
)
136144

137145
await Promise.all(
138146
htmlFiles.map((file) =>
139-
fse.move(file, resolve(cwd, basename(file)), { overwrite: true }),
147+
fs.move(file, path.resolve(cwd, path.basename(file)), {
148+
overwrite: true,
149+
}),
140150
),
141151
)
142152

143153
const htmlDirs = await fg(
144154
outputDirs.map((dir) => dir),
145-
{ cwd: resolve(cwd), onlyDirectories: true, absolute: true },
155+
{ cwd: path.resolve(cwd), onlyDirectories: true, absolute: true },
146156
)
147157
await Promise.all(
148158
htmlDirs.map(async (item) => {
149159
const isEmpty = await isDirEmpty(item)
150160
if (isEmpty) {
151-
return fse.remove(item)
161+
return fs.remove(item)
152162
}
153163
}),
154164
)
@@ -164,20 +174,28 @@ export function createInput(
164174
if (isMpa(viteConfig) || pages?.length) {
165175
const templates = pages.map((page) => page.template)
166176
templates.forEach((temp) => {
167-
const file = basename(temp)
168-
const key = file.replace(/\.html/, '')
169-
input[key] = resolve(viteConfig.root, temp)
177+
let dirName = path.dirname(temp)
178+
const file = path.basename(temp)
179+
180+
dirName = dirName.replace(/\s+/g, '').replace(/\//g, '-')
181+
182+
const key =
183+
dirName === '.' || dirName === 'public' || !dirName
184+
? file.replace(/\.html/, '')
185+
: dirName
186+
input[key] = path.resolve(viteConfig.root, temp)
170187
})
188+
171189
return input
172190
} else {
173-
const dir = dirname(template)
191+
const dir = path.dirname(template)
174192
if (ignoreDirs.includes(dir)) {
175193
return undefined
176194
} else {
177-
const file = basename(template)
195+
const file = path.basename(template)
178196
const key = file.replace(/\.html/, '')
179197
return {
180-
[key]: resolve(viteConfig.root, template),
198+
[key]: path.resolve(viteConfig.root, template),
181199
}
182200
}
183201
}
@@ -278,24 +296,28 @@ export function getPageConfig(
278296
filename: defaultPage,
279297
template: `./${defaultPage}`,
280298
}
281-
const page = pages.filter((page) => page.filename === htmlName)?.[0]
299+
300+
const page = pages.filter((page) => {
301+
return path.resolve('/' + page.filename) === path.resolve('/' + htmlName)
302+
})?.[0]
282303
return page ?? defaultPageOption ?? undefined
283304
}
284305

285306
export function getHtmlInPages(page: PageOption, root: string) {
286307
const htmlPath = getHtmlPath(page, root)
308+
287309
return readHtml(htmlPath)
288310
}
289311

290312
export function getHtmlPath(page: PageOption, root: string) {
291313
const { template } = page
292314
const templatePath = template.startsWith('.') ? template : `./${template}`
293-
return resolve(root, templatePath)
315+
return path.resolve(root, templatePath)
294316
}
295317

296318
export async function readHtml(path: string) {
297-
if (!fse.pathExistsSync(path)) {
319+
if (!fs.pathExistsSync(path)) {
298320
throw new Error(`html is not exist in ${path}`)
299321
}
300-
return await fse.readFile(path).then((buffer) => buffer.toString())
322+
return await fs.readFile(path).then((buffer) => buffer.toString())
301323
}

packages/playground/basic/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
},
88
"dependencies": {
99
"vite-plugin-html": "workspace:*",
10-
"vue": "^3.2.30",
10+
"vue": "^3.2.31",
1111
"vue-router": "^4.0.12"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^2.2.0",
15-
"@vue/compiler-sfc": "^3.2.30",
16-
"vite": "^2.8.0"
14+
"@vitejs/plugin-vue": "^2.2.2",
15+
"@vue/compiler-sfc": "^3.2.31",
16+
"vite": "^2.8.4"
1717
}
1818
}

packages/playground/mpa/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
},
99
"dependencies": {
1010
"vite-plugin-html": "workspace:*",
11-
"vue": "^3.2.30"
11+
"vue": "^3.2.31"
1212
},
1313
"devDependencies": {
14-
"@vitejs/plugin-vue": "^2.2.0",
15-
"@vue/compiler-sfc": "^3.2.30",
16-
"vite": "^2.8.0"
14+
"@vitejs/plugin-vue": "^2.2.2",
15+
"@vue/compiler-sfc": "^3.2.31",
16+
"vite": "^2.8.4"
1717
}
1818
}

0 commit comments

Comments
 (0)