Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 6dfd10e

Browse files
committed
fix: esm sync require support, close #108
1 parent d58623c commit 6dfd10e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Diff for: src/core/parseSFC.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable one-var */
22
/* eslint-disable @typescript-eslint/no-namespace */
3+
import { createRequire } from 'module'
34
import { notNullish, partition } from '@antfu/utils'
45
import type { Program } from '@babel/types'
56
import type { ParserPlugin } from '@babel/parser'
@@ -82,6 +83,14 @@ const BUILD_IN_DIRECTIVES = new Set([
8283
// 'ref',
8384
])
8485

86+
function getRequire() {
87+
return (
88+
(typeof require === 'function')
89+
? require
90+
: createRequire(import.meta.url)
91+
)
92+
}
93+
8594
function getComponents(node: TemplateChildNode): string[] {
8695
const current
8796
= node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.COMPONENT
@@ -367,8 +376,7 @@ export function parseSFC(
367376
&& p.value.content === 'pug',
368377
)
369378
? baseParse(
370-
// eslint-disable-next-line @typescript-eslint/no-var-requires
371-
require('pug').compile(
379+
getRequire()('pug').compile(
372380
templateNode.children.map(x => x.loc.source).join(''),
373381
{
374382
filename: id,

0 commit comments

Comments
 (0)