From c8785597c4cda205aa3caa04c89c9b833146fd30 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Thu, 9 Dec 2021 15:52:19 +0800 Subject: [PATCH] fix: do not polyfill `import.meta.url` --- docs/vite.config.ts | 6 +++--- package.json | 1 + pnpm-lock.yaml | 4 ++-- scripts/utils.ts | 24 +++++++++++++++++++++++- src/markdown-component.ts | 1 + types.d.ts | 2 ++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/docs/vite.config.ts b/docs/vite.config.ts index 8ff30c2..5b67895 100644 --- a/docs/vite.config.ts +++ b/docs/vite.config.ts @@ -2,8 +2,8 @@ import { defineConfig } from 'vite' import prefresh from '@prefresh/vite' import { createConfig } from '../scripts/utils' +const config = createConfig('preact', false, true) export default defineConfig({ - ...createConfig('preact', false, true), - // @ts-expect-error - plugins: [prefresh()], + ...config, + plugins: [...(config.plugins || []), prefresh()], }) diff --git a/package.json b/package.json index 951cff9..a12c8be 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dependencies": { "element-in-view": "^0.1.0", "htm": "^3.0.4", + "magic-string": "^0.25.7", "marked": "^2.0.0", "preact": "^10.5.12", "prismjs": "^1.23.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 043a7db..42f5bbd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,7 @@ specifiers: esbuild-register: ^3.2.0 fre: ^2.0.4 htm: ^3.0.4 + magic-string: ^0.25.7 marked: ^2.0.0 preact: ^10.5.12 prettier: ^2.2.1 @@ -30,6 +31,7 @@ specifiers: dependencies: element-in-view: 0.1.0 htm: 3.0.4 + magic-string: 0.25.7 marked: 2.0.1 preact: 10.5.13 prismjs: 1.23.0 @@ -1379,7 +1381,6 @@ packages: resolution: {integrity: sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==} dependencies: sourcemap-codec: 1.4.8 - dev: true /marked/2.0.1: resolution: {integrity: sha512-5+/fKgMv2hARmMW7DOpykr2iLhl0NgjyELk5yn92iE7z8Se1IS9n3UsFm86hFXIkvMBmVxki8+ckcpjBeyo/hw==} @@ -1725,7 +1726,6 @@ packages: /sourcemap-codec/1.4.8: resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==} - dev: true /supports-color/5.5.0: resolution: {integrity: sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==} diff --git a/scripts/utils.ts b/scripts/utils.ts index 2bf6b87..11d52ff 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -4,6 +4,7 @@ import { rollup } from 'rollup' import dts from 'rollup-plugin-dts' import tsResolve from '@egoist/rollup-plugin-ts-resolve' import { UserConfig } from 'vite' +import MagicString from 'magic-string' const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8')) @@ -33,11 +34,32 @@ export const createConfig = ( PRISM_VERSION: JSON.stringify(require('prismjs/package').version), PRISM_LANGUAGES: JSON.stringify(getPrismLanguages()), }, + // plugins: [ + // { + // name: 'import_meta_url', + // enforce: 'post', + // renderChunk(code) { + // const index = code.indexOf('import_meta_url') + + // if (index === -1) return + // const s = new MagicString(code) + // s.overwrite( + // index, + // index + 'import_meta_url'.length, + // 'import.meta.url' + // ) + // return { + // code: s.toString(), + // map: s.generateMap({ hires: true }), + // } + // }, + // }, + // ], build: { emptyOutDir: false, minify, sourcemap: true, - + target: 'esnext', rollupOptions: { preserveEntrySignatures: 'strict', input: isWebsite diff --git a/src/markdown-component.ts b/src/markdown-component.ts index 4598ac2..cd808f2 100644 --- a/src/markdown-component.ts +++ b/src/markdown-component.ts @@ -1,5 +1,6 @@ export const MD_PROPS_KEY = `MD_C_PROPS` +// prevent vite from shimming import.meta.url let docupUrl = import.meta.url if (import.meta.env.DEV) { // During dev it's not bundled diff --git a/types.d.ts b/types.d.ts index 20bf00b..79d3640 100644 --- a/types.d.ts +++ b/types.d.ts @@ -20,3 +20,5 @@ interface Window { MD_C_PROPS: any _MD_COMPONENTS: Record } + +declare const import_meta_url: string