Skip to content

Commit

Permalink
fix: es module can't work
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 6, 2023
1 parent 93553b8 commit d2d102a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "vite-plugin-cdn2-example",
"type": "module",
"scripts": {
"dev": "set DEBUG=vite-plugin-cdn2 & vite",
"build": "vite build",
"build": "set DEBUG=vite-plugin-cdn2 & vite build",
"preview": "vite preview"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion src/scanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fsp from 'fs/promises'
import url from 'url'
import module from 'module'
import worker_threads from 'worker_threads'
import type { MessagePort } from 'worker_threads'
import { MAX_CONCURRENT, createConcurrentQueue, createVM } from './vm'
Expand All @@ -22,6 +23,9 @@ import type { IIFEModuleInfo, IModule, ModuleInfo, ResolverFunction, TrackModule
// import.meta.url will be transform as
// const __meta = { url: require('url').pathToFileURL(__filename).href }


const _require = module.createRequire(import.meta.url)

const ___filename = url.fileURLToPath(import.meta.url)

interface WorkerData {
Expand Down Expand Up @@ -79,7 +83,7 @@ async function tryResolveModule(
) {
const { name: moduleName, relativeModule, ...rest } = module
try {
const modulePath = require.resolve(moduleName)
const modulePath = _require.resolve(moduleName)
const packageJsonPath = lookup(modulePath, 'package.json')
const str = await fsp.readFile(packageJsonPath, 'utf8')
const packageJSON: IIFEModuleInfo = JSON.parse(str)
Expand Down

0 comments on commit d2d102a

Please sign in to comment.