Skip to content

Commit

Permalink
Merge pull request #15 from greym0uth/jg/use-resolver
Browse files Browse the repository at this point in the history
Use require.resolve for modules
  • Loading branch information
nshen authored May 20, 2022
2 parents 8ce9d1b + 902cf4f commit e8016a3
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ function vitePluginWasmPack(
// 'my_crate_bg.wasm': { path: 'node_modules/my_crate/my_crate_bg.wasm', isNodeModule: true }
modulePaths.forEach((cratePath) => {
const wasmFile = wasmFilename(cratePath);
const wasmDirectory = path.dirname(require.resolve(cratePath));
wasmMap.set(wasmFile, {
path: path.join('node_modules', cratePath, wasmFile),
path: path.join(wasmDirectory, wasmFile),
isNodeModule: true
});
});
Expand Down Expand Up @@ -95,7 +96,7 @@ function vitePluginWasmPack(
async buildStart(_inputOptions) {
const prepareBuild = async (cratePath: string, isNodeModule: boolean) => {
const pkgPath = isNodeModule
? path.join('node_modules', cratePath)
? path.dirname(require.resolve(cratePath))
: path.join(cratePath, pkg);
const crateName = path.basename(cratePath);
if (!fs.existsSync(pkgPath)) {
Expand Down Expand Up @@ -132,7 +133,10 @@ function vitePluginWasmPack(
* crateName === 'test'
*/

const jsPath = path.join('./node_modules', isNodeModule ? cratePath : crateName, jsName);
let jsPath = path.join('./node_modules', crateName, jsName);
if (isNodeModule) {
jsPath = path.join(pkgPath, jsName);
}
const regex = /input = new URL\('(.+)'.+;/g;
let code = fs.readFileSync(path.resolve(jsPath), { encoding: 'utf-8' });
code = code.replace(regex, (_match, group1) => {
Expand Down

0 comments on commit e8016a3

Please sign in to comment.