Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix bundler looking for index.<chunk_hash>.wasm #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
dist
esm
yarn.lock
example/my-crate/target
example/my-crate/target
.DS_Store
72 changes: 32 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,29 @@ function vitePluginWasmPack(
},

async buildStart(_inputOptions) {
const prepareBuild = async (cratePath: string, isNodeModule: boolean) => {
const pkgPath = isNodeModule
? path.dirname(require.resolve(cratePath))
: path.join(cratePath, pkg);
const prepareBuild = async (cratePath: string) => {
const pkgPath = path.join(cratePath, pkg);
const crateName = path.basename(cratePath);
if (!fs.existsSync(pkgPath)) {
if (isNodeModule) {
console.error(
chalk.bold.red('Error: ') +
`Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red(
`npm install ${cratePath}`
)} first`
);
} else {
console.error(
chalk.bold.red('Error: ') +
`Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red(
`wasm-pack build ${cratePath} --target web`
)} first`
);
}
console.error(
chalk.bold.red('Error: ') +
`Can't find ${chalk.bold(pkgPath)}, run ${chalk.bold.red(
`wasm-pack build ${cratePath} --target web`
)} first`
);
}
if (!isNodeModule) {
// copy pkg generated by wasm-pack to node_modules
try {
await fs.copy(pkgPath, path.join('node_modules', crateName));
} catch (error) {
this.error(`copy crates failed`);
}
// copy pkg generated by wasm-pack to node_modules
try {
await fs.copy(pkgPath, path.join('node_modules', crateName));
} catch (error) {
this.error(`copy crates failed`);
}
// replace default load path with '/assets/xxx.wasm'
const jsName = crateName.replace(/\-/g, '_') + '.js';

/**
* if use node module and name is '@group/test'
* cratePath === '@group/test'
* crateName === 'test'
*/

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' });
const regex = /input = new URL\('(.+)'.+;/g;
code = code.replace(regex, (_match, group1) => {
return `input = "${path.posix.join(
config_base,
Expand All @@ -150,12 +128,26 @@ function vitePluginWasmPack(
};

for await (const cratePath of cratePaths) {
await prepareBuild(cratePath, false);
await prepareBuild(cratePath);
}
},

for await (const cratePath of modulePaths) {
await prepareBuild(cratePath, true);
}
transform(code, id) {
const cratePath = modulePaths.find((modulePath) => require.resolve(modulePath) === id);
if (!cratePath) return code;
const crateName = path.basename(cratePath);

const wasmAssetPath = path.posix.join(
config_base,
config_assetsDir,
crateName
);
const regex = /input = new URL\('(.+)'.+;/g;
code = code.replace(regex, `input = '${wasmAssetPath}'`);
// Newer versions of wasm-pack use 'import.meta.url' to get the js file.
code = code.replace(/input = import\.meta\.url/g, `input = '${wasmAssetPath}.js'`);

return code;
},

configureServer({ middlewares }) {
Expand Down