From 549bc29060f82ec6d689a29e485a8eb59039f0d7 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 13 Jan 2023 19:31:09 +0900 Subject: [PATCH] Chore: Fix order-in-components rule doc page demo not working (#2078) --- .eslintignore | 1 + .gitignore | 1 + docs/.vitepress/vite-plugin.ts | 72 +++++++++++++++++++++++++--------- package.json | 1 - 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/.eslintignore b/.eslintignore index a0c203ada..2353241cd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -9,3 +9,4 @@ /docs/.vitepress/build-system/shim/eslint.mjs /docs/.vitepress/build-system/shim/assert.mjs /docs/.vitepress/.temp +/docs/.vitepress/cache diff --git a/.gitignore b/.gitignore index 06fb2db3b..797d0cbcd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ yarn-error.log /docs/.vitepress/build-system/shim/eslint.mjs /docs/.vitepress/build-system/shim/assert.mjs /docs/.vitepress/.temp +/docs/.vitepress/cache typings/eslint/lib/rules diff --git a/docs/.vitepress/vite-plugin.ts b/docs/.vitepress/vite-plugin.ts index 0df6a54fe..2a044ced1 100644 --- a/docs/.vitepress/vite-plugin.ts +++ b/docs/.vitepress/vite-plugin.ts @@ -1,7 +1,7 @@ import type { UserConfig } from 'vitepress' import path from 'path' import { fileURLToPath } from 'url' -import { viteCommonjs as baseViteCommonjs } from '@originjs/vite-plugin-commonjs' +import esbuild from 'esbuild' type Plugin = Extract< NonNullable['plugins']>[number], { name: string } @@ -27,27 +27,63 @@ export function vitePluginRequireResolve(): Plugin { } export function viteCommonjs(): Plugin { - const base = baseViteCommonjs({ - include: [libRoot], - skipPreBuild: true - }) as Plugin return { - ...base, - // The `@originjs/vite-plugin-commonjs` is 'serve' only, but use it in 'build' as well. + name: 'vite-plugin-cjs-to-esm', apply: () => true, - async transform(code, id, options) { - if (typeof base.transform !== 'function') { - return null + async transform(code, id) { + if (!id.startsWith(libRoot)) { + return undefined } - const result = await base.transform.call(this, code, id, options) - if (result && typeof result === 'object' && result.code) { - return { - ...result, - // Replace it with null, because blanks can be given to the sourcemap and cause an error. - map: undefined - } + const base = transformRequire(code) + try { + const transformed = esbuild.transformSync(base, { + format: 'esm' + }) + return transformed.code + } catch (e) { + console.error('Transform error. base code:\n' + base, e) } - return result + return undefined } } } + +/** + * Transform `require()` to `import` + */ +function transformRequire(code: string) { + if (!code.includes('require')) { + return code + } + const modules = new Map() + const replaced = code.replace( + /(\/\/[^\n\r]*|\/\*[\s\S]*?\*\/)|\brequire\s*\(\s*(["'].*?["'])\s*\)/gu, + (match, comment, moduleString) => { + if (comment) { + return match + } + + let id = + '__' + + moduleString.replace(/[^a-zA-Z0-9_$]+/gu, '_') + + Math.random().toString(32).substring(2) + while (code.includes(id) || modules.has(id)) { + id += Math.random().toString(32).substring(2) + } + modules.set(id, moduleString) + return id + } + ) + + return ( + [...modules] + .map(([id, moduleString]) => { + return `import * as __temp_${id} from ${moduleString}; +const ${id} = __temp_${id}.default || __temp_${id}; +` + }) + .join('') + + ';\n' + + replaced + ) +} diff --git a/package.json b/package.json index dc5dc2430..54cef3a87 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "xml-name-validator": "^4.0.0" }, "devDependencies": { - "@originjs/vite-plugin-commonjs": "^1.0.3", "@ota-meshi/site-kit-eslint-editor-vue": "^0.1.0", "@types/eslint": "^8.4.2", "@types/eslint-visitor-keys": "^1.0.0",