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

Conversation

kabouzeid
Copy link
Contributor

No description provided.

@@ -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.

@@ -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)`);

src/utils.ts Outdated Show resolved Hide resolved
@kabouzeid
Copy link
Contributor Author

kabouzeid commented Apr 13, 2021

@lencx I finally fixed it now, everything working fine. I've applied the RegEx just as you suggested which fixed all problems with rollup and tsup trying to change import.meta.url 👍 .

Could you merge this?

As a side note: Apparently this is a known problem:
https://github.com/jiangfengming/rollup-plugin-import-meta-url-to-module
vitejs/vite#894

@lencx lencx merged commit f44199b into rwasm:main Apr 13, 2021
@lencx lencx added the bug Something isn't working label Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants