From c8156ca57e6eb85e7cf5deadbb2507196040715c Mon Sep 17 00:00:00 2001 From: Tom Gao Date: Mon, 1 Jan 2024 16:19:16 +0800 Subject: [PATCH] feat: remove gen template script --- package.json | 8 ++------ scripts/template.ts | 37 ------------------------------------- src/global.d.ts | 4 ++++ src/loading/index.ts | 2 +- tsup.config.ts | 3 +++ 5 files changed, 10 insertions(+), 44 deletions(-) delete mode 100644 scripts/template.ts create mode 100644 src/global.d.ts diff --git a/package.json b/package.json index abf17e7..49076b1 100644 --- a/package.json +++ b/package.json @@ -40,12 +40,8 @@ } }, "scripts": { - "dev": "run-p dev:template dev:lib", - "dev:lib": "tsup --watch", - "dev:template": "cross-env WATCH=1 tsx watch ./scripts/template.ts", - "build": "run-p build:template build:lib", - "build:lib": "tsup", - "build:template": "tsx ./scripts/template.ts", + "dev": "tsup --watch", + "build": "tsup", "lint": "run-s lint:eslint lint:stylelint lint:prettier", "lint:eslint": "eslint \"{src,examples}/**/*.{js,cjs,ts,tsx,vue}\" *.{js,cjs,ts} --fix --cache", "lint:stylelint": "stylelint \"examples/**/*.{vue,css,less,html}\" --fix --cache", diff --git a/scripts/template.ts b/scripts/template.ts deleted file mode 100644 index 742796c..0000000 --- a/scripts/template.ts +++ /dev/null @@ -1,37 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; -import chokidar from 'chokidar'; -import { minify } from 'html-minifier-terser'; - -const SRC = path.resolve(__dirname, '../src/loading/template.html'); -const DEST = path.resolve(__dirname, '../src/loading/template.ts'); - -async function minifyHtml() { - if (!fs.existsSync(SRC)) { - throw new Error(`${SRC} is not found`); - } - - const html = fs.readFileSync(SRC, 'utf8'); - const minifiedHtml = await minify(html, { - minifyCSS: true, - minifyJS: true, - collapseWhitespace: true, - removeComments: false, - removeRedundantAttributes: true, - removeScriptTypeAttributes: true, - removeStyleLinkTypeAttributes: true, - useShortDoctype: true, - }); - - fs.writeFileSync(DEST, `export default \`${minifiedHtml}\`;\n`, { encoding: 'utf8' }); - - console.log('minified template.html successfully!'); -} - -if (process.env.WATCH) { - chokidar.watch(SRC).on('change', async () => { - await minifyHtml(); - }); -} - -minifyHtml(); diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..448f7d1 --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,4 @@ +declare module '*.html' { + const content: string; + export default content; +} diff --git a/src/loading/index.ts b/src/loading/index.ts index 5f2f226..a9a686c 100644 --- a/src/loading/index.ts +++ b/src/loading/index.ts @@ -1,7 +1,7 @@ import type { PluginOption } from 'vite'; import { parse as htmlParser } from 'node-html-parser'; import { LOADING_PLUGIN_NAME } from '../constants'; -import loadingHtml from './template'; +import loadingHtml from './template.html'; import { type HtmlLoadingOptions } from './types'; export * from './types'; diff --git a/tsup.config.ts b/tsup.config.ts index 6ab8e3b..8a1ea96 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ format: ['esm', 'cjs'], target: ['es2021', 'node16'], external: ['vite'], + loader: { + '.html': 'text', + }, clean: true, dts: true, splitting: true,