-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promotes the experimental loader as the defeault module loading mecha…
…nism
- Loading branch information
Daniel Del Core
committed
Sep 20, 2024
1 parent
68206cd
commit 0b45155
Showing
7 changed files
with
73 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { installPackage } from '@antfu/install-pkg'; | ||
|
||
const ModuleLoader = () => { | ||
const getInfo = async (packageName: string) => { | ||
// @ts-expect-error Experimental loader | ||
const entryPath = await import.meta.resolve(packageName); | ||
const location = (entryPath.split(packageName)[0] + packageName).replace( | ||
'file://', | ||
'', | ||
); | ||
const pkgJsonRaw = fs.readFileSync( | ||
path.join(location.replace('file://', ''), 'package.json'), | ||
'utf8', | ||
); | ||
const pkgJson = JSON.parse(pkgJsonRaw); | ||
|
||
return { location, entryPath, pkgJson }; | ||
}; | ||
|
||
const install = async (packageName: string) => { | ||
const __dirname = path.dirname(new URL(import.meta.url).pathname); | ||
await installPackage(packageName, { | ||
cwd: __dirname, | ||
packageManager: 'npm', | ||
additionalArgs: ['--force'], | ||
}); | ||
|
||
const { pkgJson } = await getInfo(packageName); | ||
|
||
// Install whitelisted devDependencies | ||
if (pkgJson?.hypermod?.dependencies) { | ||
await Promise.all( | ||
pkgJson.hypermod.dependencies.map((dep: string) => { | ||
const version = pkgJson.devDependencies[dep]; | ||
if (!version) return; | ||
return installPackage(`${dep}@${version}`, { | ||
cwd: __dirname, | ||
packageManager: 'npm', | ||
additionalArgs: ['--force'], | ||
}); | ||
}), | ||
); | ||
} | ||
}; | ||
|
||
return { | ||
install, | ||
getInfo, | ||
require: async (packageName: string) => import(packageName), | ||
}; | ||
}; | ||
|
||
export default ModuleLoader; |