Skip to content

Commit

Permalink
fix: esm sync require support, close #108
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Feb 23, 2022
1 parent d58623c commit 6dfd10e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/parseSFC.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable one-var */
/* eslint-disable @typescript-eslint/no-namespace */
import { createRequire } from 'module'
import { notNullish, partition } from '@antfu/utils'
import type { Program } from '@babel/types'
import type { ParserPlugin } from '@babel/parser'
Expand Down Expand Up @@ -82,6 +83,14 @@ const BUILD_IN_DIRECTIVES = new Set([
// 'ref',
])

function getRequire() {
return (
(typeof require === 'function')
? require
: createRequire(import.meta.url)
)
}

function getComponents(node: TemplateChildNode): string[] {
const current
= node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.COMPONENT
Expand Down Expand Up @@ -367,8 +376,7 @@ export function parseSFC(
&& p.value.content === 'pug',
)
? baseParse(
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('pug').compile(
getRequire()('pug').compile(
templateNode.children.map(x => x.loc.source).join(''),
{
filename: id,
Expand Down

0 comments on commit 6dfd10e

Please sign in to comment.