Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Newer generated files use module_or_path rather than input #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 28 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ function vitePluginWasmPack(
: isString(moduleCrates)
? [moduleCrates]
: moduleCrates;

// from ../../my-crate -> my_crate_bg.wasm
const wasmFilename = (cratePath: string) => {
return path.basename(cratePath).replace(/\-/g, '_') + '_bg.wasm';
};

type CrateType = { path: string; isNodeModule: boolean };

// wasmfileName : CrateType
const wasmMap = new Map<string, CrateType>();

// 'my_crate_bg.wasm': {path:'../../my_crate/pkg/my_crate_bg.wasm', isNodeModule: false}
cratePaths.forEach((cratePath) => {
const wasmFile = wasmFilename(cratePath);
Expand All @@ -53,6 +57,7 @@ function vitePluginWasmPack(
isNodeModule: false
});
});

// 'my_crate_bg.wasm': { path: 'node_modules/my_crate/my_crate_bg.wasm', isNodeModule: true }
modulePaths.forEach((cratePath) => {
const wasmFile = wasmFilename(cratePath);
Expand All @@ -66,6 +71,7 @@ function vitePluginWasmPack(
return {
name: 'vite-plugin-wasm-pack',
enforce: 'pre',

configResolved(resolvedConfig) {
config_base = resolvedConfig.base;
config_assetsDir = resolvedConfig.build.assetsDir;
Expand Down Expand Up @@ -98,7 +104,9 @@ function vitePluginWasmPack(
const pkgPath = isNodeModule
? path.dirname(require.resolve(cratePath))
: path.join(cratePath, pkg);

const crateName = path.basename(cratePath);

if (!fs.existsSync(pkgPath)) {
if (isNodeModule) {
console.error(
Expand All @@ -116,6 +124,7 @@ function vitePluginWasmPack(
);
}
}

if (!isNodeModule) {
// copy pkg generated by wasm-pack to node_modules
try {
Expand All @@ -124,6 +133,7 @@ function vitePluginWasmPack(
this.error(`copy crates failed: ${error}`);
}
}

// replace default load path with '/assets/xxx.wasm'
const jsName = crateName.replace(/\-/g, '_') + '.js';

Expand All @@ -134,18 +144,25 @@ function vitePluginWasmPack(
*/

let jsPath = path.join('./node_modules', crateName, jsName);

if (isNodeModule) {
jsPath = path.join(pkgPath, jsName);
}
const regex = /input = new URL\('(.+)'.+;/g;

// newer versions of wasmpack seem to use a different variable name here
// (i.e., 'module_or_path' rather than 'input').
const regex = /(module_or_path|input) = new URL\('(.+)'.+;/g;

let code = fs.readFileSync(path.resolve(jsPath), { encoding: 'utf-8' });
code = code.replace(regex, (_match, group1) => {
return `input = "${path.posix.join(

code = code.replace(regex, (_fullMatch, variableName, urlPath) => {
return `${variableName} = "${path.posix.join(
config_base,
config_assetsDir,
group1
urlPath
)}"`;
});

fs.writeFileSync(jsPath, code);
};

Expand All @@ -161,13 +178,14 @@ function vitePluginWasmPack(
configureServer({ middlewares }) {
// send 'root/pkg/xxx.wasm' file to user
middlewares.use((req, res, next) => {
if (isString(req.url)) {
const basename = path.basename(req.url);
res.setHeader(
'Cache-Control',
'no-cache, no-store, must-revalidate'
);
const incomingUrl = req.originalUrl ?? req.url;

if (isString(incomingUrl)) {
const basename = path.basename(incomingUrl);

res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
const entry = wasmMap.get(basename);

if (basename.endsWith('.wasm') && entry) {
res.writeHead(200, { 'Content-Type': 'application/wasm' });
fs.createReadStream(entry.path).pipe(res);
Expand Down