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

Fix support for config.base. Fix import.meta.url being expanded. Fix use command instead of mode. #4

Merged
merged 4 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin {
const fileId = _path?.[1].replace(/^\//, '') + '_bg.wasm';

// build wasm file
if (!wasmMap.has(fileId) && config?.mode !== 'development') {
if (!wasmMap.has(fileId) && config?.command !== 'serve') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const source = fs.readFileSync(path.resolve(crateRoot, fileId));
const hash = createHash('md5').update(String(source)).digest('hex').substring(0, 8);
const _name = config?.build?.assetsDir + '/' + path.basename(fileId).replace('.wasm', `.${hash}.wasm`);
Expand All @@ -51,7 +51,7 @@ export function ViteRsw(userOptions: RswPluginOptions): Plugin {
});

// fix: fetch or URL
code = loadWasm(code, path.basename(fileId), _name);
code = loadWasm(code, path.basename(fileId), config.base + _name);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't assume the base path to be /. It could be anything. You can try with vite build --base "/custom/base/path"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those are the changes from the latest tsup release (4.8.21). Not sure how we can fix this on our side except sticking with 4.8.20 for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the build import.meta.url will be replaced with __import_meta_url

// source code
code.replace(`new URL('${oPath}', import.meta.url)`, `new URL('${nPath}', location.origin)`)
// build
e.replace(`new URL('${o}', __import_meta_url)`,`new URL('${n}', location.origin)`)

Screen Shot 2021-04-14 at 12 46 54 AM


solution: RegExp

// utils.ts

// ...
code = code.replace(/import\.meta\.url\.replace\(\/\\\.js\$\/, \'_bg\.wasm\'\);/, `fetch('${nPath}')`);
code = code.replace(`new URL('${oPath}',`, `new URL('${nPath}',`);
code = code.replace(/, import\.meta\.url\)/, `, location.origin)`);

return code;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,4 @@ function clearRswErrorOverlay() {
}

window.createRswErrorOverlay = createRswErrorOverlay;
`;
`;