Skip to content

Commit

Permalink
fix: rewrite CJS specific funcs/vars in plugins (#8227)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 19, 2022
1 parent e57af2e commit da0b6de
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ module.exports = defineConfig({
'node/no-extraneous-import': 'off'
}
},
{
files: ['packages/plugin-*/**/*'],
rules: {
'no-restricted-globals': ['error', 'require', '__dirname', '__filename']
}
},
{
files: ['playground/**'],
rules: {
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-vue-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function vueJsxPlugin(options: Options = {}): Plugin {
}
},

transform(code, id, opt) {
async transform(code, id, opt) {
const ssr = typeof opt === 'boolean' ? opt : (opt && opt.ssr) === true
const {
include,
Expand All @@ -91,7 +91,10 @@ function vueJsxPlugin(options: Options = {}): Plugin {
const plugins = [importMeta, [jsx, babelPluginOptions], ...babelPlugins]
if (id.endsWith('.tsx') || filepath.endsWith('.tsx')) {
plugins.push([
require('@babel/plugin-transform-typescript'),
// @ts-ignore missing type
await import('@babel/plugin-transform-typescript').then(
(r) => r.default
),
// @ts-ignore
{ isTSX: true, allowExtensions: true }
])
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue-jsx/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"compilerOptions": {
"outDir": "dist",
"target": "ES2020",
"module": "CommonJS",
"module": "ES2020",
"moduleResolution": "Node",
"strict": true,
"declaration": true,
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-vue/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare module 'vue/compiler-sfc' {
}
}

import { createRequire } from 'module'
import type * as _compiler from 'vue/compiler-sfc'

export function resolveCompiler(root: string): typeof _compiler {
Expand All @@ -23,8 +24,11 @@ export function resolveCompiler(root: string): typeof _compiler {
return compiler
}

const _require = createRequire(import.meta.url)
function tryRequire(id: string, from?: string) {
try {
return from ? require(require.resolve(id, { paths: [from] })) : require(id)
return from
? _require(_require.resolve(id, { paths: [from] }))
: _require(id)
} catch (e) {}
}
2 changes: 1 addition & 1 deletion packages/plugin-vue/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"compilerOptions": {
"outDir": "dist",
"target": "ES2020",
"module": "commonjs",
"module": "ES2020",
"moduleResolution": "node",
"strict": true,
"declaration": true,
Expand Down

0 comments on commit da0b6de

Please sign in to comment.