diff --git a/.gitignore b/.gitignore index 3d5025d..2b0fade 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,4 @@ dist # TernJS port file .tern-port -packages/vite-plugin-commonjs/lib +lib diff --git a/packages/vite-plugin-commonjs/package.json b/packages/vite-plugin-commonjs/package.json index 2cefcd5..f561e94 100644 --- a/packages/vite-plugin-commonjs/package.json +++ b/packages/vite-plugin-commonjs/package.json @@ -7,7 +7,6 @@ }, "main": "lib/index.js", "files": [ - "src", "lib" ], "repository": { @@ -15,7 +14,7 @@ "url": "git+https://github.com/originjs/vite-plugins.git" }, "keywords": [ - "vite", + "vite-plugin", "commonjs" ], "author": "jiawulin@vip.qq.com", diff --git a/packages/vite-plugin-commonjs/src/index.ts b/packages/vite-plugin-commonjs/src/index.ts index 059d94e..3381bb5 100644 --- a/packages/vite-plugin-commonjs/src/index.ts +++ b/packages/vite-plugin-commonjs/src/index.ts @@ -1,80 +1,61 @@ import { transformSync, TransformResult } from "esbuild"; +import { transformRequire, isCommonJS } from "./lib"; +import * as fs from "fs"; import { Plugin } from "vite"; import { createFilter } from "@rollup/pluginutils"; -const commonJSRegex: RegExp = /\b(module\.exports|exports\.\w+|exports\s*=\s*)/; -const requireRegex: RegExp = /require\(([\'|\"].*[\'|\"])\)/g; -const IMPORT_STRING_PREFIX: String = "__require_for_vite"; - type Options = { - include?: string | RegExp | string[] | RegExp[] | undefined; - exclude?: string | RegExp | string[] | RegExp[] | undefined; + include?: string | RegExp | string[] | RegExp[] | undefined; + exclude?: string | RegExp | string[] | RegExp[] | undefined; }; -export default (options: Options = {}): Plugin => { - const filter = createFilter(options.include, options.exclude); - - return { - name: "originjs:commonjs", - apply: "serve", - transform(code: string, id: string): TransformResult { - if (!filter(id)) { - return null; - } - - const requireMatches = code.matchAll(requireRegex); - let importsString = ""; - let packageName = ""; - for (let item of requireMatches) { - if (!isString(item[1])) { - console.warn(`Not supported dynamic import, file:${id}`); - continue; - } - - packageName = `${IMPORT_STRING_PREFIX}_${randomString(6)}`; - importsString += `import * as ${packageName} from ${item[1]};\n`; - code = code.replace(item[0], packageName); - } - - if (importsString) { - code = importsString + code; - } - - if (id.indexOf("/node_modules/.vite/") == -1 && isCommonJS(code)) { - return transformSync(code, { format: "esm" }); - } - - if (importsString) { - return { - code, - map: null, - warnings: null, - }; - } - - return null; - }, - }; -}; - -function isCommonJS(code: string): Boolean { - return commonJSRegex.test(code); -} - -function randomString(length: number): string { - const code: string = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; - let result: string = ""; - for (let index = 0; index < length; index++) { - result += code[Math.floor(Math.random() * code.length)]; +export function viteCommonjs(options: Options = {}): Plugin { + const filter = createFilter(options.include, options.exclude); + return { + name: "originjs:commonjs", + apply: "serve", + transform(code: string, id: string): TransformResult { + if (!filter(id)) { + return null; + } + let result = transformRequire(code, id); + if (id.indexOf("/node_modules/.vite/") == -1 && isCommonJS(code)) { + return transformSync(result.code, { format: "esm" }); + } + + if (result.replaced) { + return { + code: result.code, + map: null, + warnings: null, + } + } + return null; } - return result; -} + } +}; -function isString(text: string) { - try { - return typeof eval(text) === "string"; - } catch (err) { - return false; +export function esbuildCommonjs(includes: string[] = []) { + return { + name: "originjs:commonjs", + setup(build) { + build.onLoad( + { + filter: new RegExp('(' + includes.join('|') + ').*\.js'), + namespace: 'file' + }, + async ({ path: id }) => { + const code = fs.readFileSync(id).toString(); + let result = transformRequire(code, id); + if (result.replaced) { + return { + contents: result.code, + loader: 'js' + } + } + return null + } + ) } -} + } +}; diff --git a/packages/vite-plugin-commonjs/src/lib.ts b/packages/vite-plugin-commonjs/src/lib.ts new file mode 100644 index 0000000..b3a7dbd --- /dev/null +++ b/packages/vite-plugin-commonjs/src/lib.ts @@ -0,0 +1,55 @@ +const commonJSRegex: RegExp = /\b(module\.exports|exports\.\w+|exports\s*=\s*)/; +const requireRegex: RegExp = /require\s*\((["'].*["'])\)/g; +const IMPORT_STRING_PREFIX: String = "__require_for_vite"; + +export interface TransformRequireResult { + code: string; + replaced: boolean; +} + +export function transformRequire(code: string, id: string):TransformRequireResult { + const requireMatches = code.matchAll(requireRegex); + let importsString = ""; + let packageName = ""; + let replaced = false; + for (let item of requireMatches) { + if (!isString(item[1])) { + console.warn(`Not supported dynamic import, file:${id}`); + continue; + } + replaced = true; + packageName = `${IMPORT_STRING_PREFIX}_${randomString(6)}`; + importsString += `import * as ${packageName} from ${item[1]};\n`; + code = code.replace(item[0], packageName); + } + + if (replaced) { + code = importsString + code; + } + return { + replaced, + code + } +} + +export function isCommonJS(code: string): boolean { + return commonJSRegex.test(code); +} + +function randomString(length: number): string { + const code: string = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + let result: string = ""; + for (let index = 0; index < length; index++) { + result += code[Math.floor(Math.random() * code.length)]; + } + return result; +} + +function isString(text: string) { + try { + return typeof eval(text) === "string"; + } catch (err) { + return false; + } +} diff --git a/packages/vite-plugin-require-context/package.json b/packages/vite-plugin-require-context/package.json index 9d923ee..aa25a8e 100644 --- a/packages/vite-plugin-require-context/package.json +++ b/packages/vite-plugin-require-context/package.json @@ -15,7 +15,7 @@ "url": "git+https://github.com/originjs/vite-plugins.git" }, "keywords": [ - "vite", + "vite-plugin", "require.context" ], "author": "Peter Lee", diff --git a/tsconfig.json b/tsconfig.json index 691cd07..05f1b7e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "declaration": true, "outDir": "lib", "target": "esnext", - "module": "esnext", + "module": "commonjs", "esModuleInterop": true, "skipLibCheck": true, "moduleResolution": "node",