admin管理员组文章数量:1022795
Background: I am new to Vite and recently I am trying to build a Markdown render project which receives Markdown format string from outside and renders GFM HTML. I choosed md4w as the converter and the npm package contains two wasm in md4w/js path.
Here is the code I used originally.
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
and it worked fine using pnpm dev
. However, If i use pnpm build
and pnpm preview
, it won't work because Vite didn't bundle the wasm into the dist and the browser console would show this error.
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
Then I followed the md4w
project README guide and change the setup script part to
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import wasmUrl from 'md4w/js/wasm-small.wasm?url';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
However, this time is even worse. Even pnpm dev
would failed and the linter would also show this error
[plugin:vite:import-analysis] Missing "./js/wasm-small.wasm" specifier in "md4w" package
But the wasm-small.wasm is there in node_modules and the md4w.d.ts is also there. The directory structure is
-rw-r--r-- 2 staff staff 1.7K 11 19 16:02 index.js
-rwxr-xr-x 2 staff staff 863.9K 11 19 16:02 md4w-fast.wasm*
-rwxr-xr-x 2 staff staff 59.1K 11 19 16:02 md4w-small.wasm*
-rw-r--r-- 2 staff staff 5.6K 11 19 16:02 md4w.d.ts
-rw-r--r-- 2 staff staff 7.4K 11 19 16:02 md4w.js
-rw-r--r-- 2 staff staff 155 11 19 16:02 unwasm.js
The code to reproduce is available in this stackblitz
Is there other ways that could import the wasm from this md4w package without import from the external url and could be bundled into the dist? Thanks a lot!
Background: I am new to Vite and recently I am trying to build a Markdown render project which receives Markdown format string from outside and renders GFM HTML. I choosed md4w as the converter and the npm package contains two wasm in md4w/js path.
Here is the code I used originally.
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
and it worked fine using pnpm dev
. However, If i use pnpm build
and pnpm preview
, it won't work because Vite didn't bundle the wasm into the dist and the browser console would show this error.
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
Then I followed the md4w
project README guide and change the setup script part to
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import wasmUrl from 'md4w/js/wasm-small.wasm?url';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
However, this time is even worse. Even pnpm dev
would failed and the linter would also show this error
[plugin:vite:import-analysis] Missing "./js/wasm-small.wasm" specifier in "md4w" package
But the wasm-small.wasm is there in node_modules and the md4w.d.ts is also there. The directory structure is
-rw-r--r-- 2 staff staff 1.7K 11 19 16:02 index.js
-rwxr-xr-x 2 staff staff 863.9K 11 19 16:02 md4w-fast.wasm*
-rwxr-xr-x 2 staff staff 59.1K 11 19 16:02 md4w-small.wasm*
-rw-r--r-- 2 staff staff 5.6K 11 19 16:02 md4w.d.ts
-rw-r--r-- 2 staff staff 7.4K 11 19 16:02 md4w.js
-rw-r--r-- 2 staff staff 155 11 19 16:02 unwasm.js
The code to reproduce is available in this stackblitz
Is there other ways that could import the wasm from this md4w package without import from the external url and could be bundled into the dist? Thanks a lot!
Share Improve this question edited Nov 19, 2024 at 20:05 Eli 618 bronze badges asked Nov 19, 2024 at 10:37 Isaiah ChanIsaiah Chan 11 bronze badge1 Answer
Reset to default 0md4w uses the exports field, which prevents accessing internal files, so the docs are wrong, you can't import under "md4w/js/*"
.
It does have unwasm support, so I tried installing that, but it didn't seem to help.
Here's some "hacky" workarounds instead:
- Copy the wasm file into your project (works for me): example stackblitz.
- Use a CDN version (haven't tried, should also work, but would have worse perf).
Background: I am new to Vite and recently I am trying to build a Markdown render project which receives Markdown format string from outside and renders GFM HTML. I choosed md4w as the converter and the npm package contains two wasm in md4w/js path.
Here is the code I used originally.
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
and it worked fine using pnpm dev
. However, If i use pnpm build
and pnpm preview
, it won't work because Vite didn't bundle the wasm into the dist and the browser console would show this error.
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
Then I followed the md4w
project README guide and change the setup script part to
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import wasmUrl from 'md4w/js/wasm-small.wasm?url';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
However, this time is even worse. Even pnpm dev
would failed and the linter would also show this error
[plugin:vite:import-analysis] Missing "./js/wasm-small.wasm" specifier in "md4w" package
But the wasm-small.wasm is there in node_modules and the md4w.d.ts is also there. The directory structure is
-rw-r--r-- 2 staff staff 1.7K 11 19 16:02 index.js
-rwxr-xr-x 2 staff staff 863.9K 11 19 16:02 md4w-fast.wasm*
-rwxr-xr-x 2 staff staff 59.1K 11 19 16:02 md4w-small.wasm*
-rw-r--r-- 2 staff staff 5.6K 11 19 16:02 md4w.d.ts
-rw-r--r-- 2 staff staff 7.4K 11 19 16:02 md4w.js
-rw-r--r-- 2 staff staff 155 11 19 16:02 unwasm.js
The code to reproduce is available in this stackblitz
Is there other ways that could import the wasm from this md4w package without import from the external url and could be bundled into the dist? Thanks a lot!
Background: I am new to Vite and recently I am trying to build a Markdown render project which receives Markdown format string from outside and renders GFM HTML. I choosed md4w as the converter and the npm package contains two wasm in md4w/js path.
Here is the code I used originally.
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
and it worked fine using pnpm dev
. However, If i use pnpm build
and pnpm preview
, it won't work because Vite didn't bundle the wasm into the dist and the browser console would show this error.
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
Then I followed the md4w
project README guide and change the setup script part to
<script setup lang="ts">
import { init, mdToHtml } from 'md4w';
import wasmUrl from 'md4w/js/wasm-small.wasm?url';
import { onMounted, ref } from 'vue';
import 'github-markdown-css/github-markdown.css';
const props = defineProps<{ msg: string }>();
const html = ref('');
onMounted(async () => {
await init(wasmUrl);
html.value = mdToHtml(props.msg);
});
</script>
However, this time is even worse. Even pnpm dev
would failed and the linter would also show this error
[plugin:vite:import-analysis] Missing "./js/wasm-small.wasm" specifier in "md4w" package
But the wasm-small.wasm is there in node_modules and the md4w.d.ts is also there. The directory structure is
-rw-r--r-- 2 staff staff 1.7K 11 19 16:02 index.js
-rwxr-xr-x 2 staff staff 863.9K 11 19 16:02 md4w-fast.wasm*
-rwxr-xr-x 2 staff staff 59.1K 11 19 16:02 md4w-small.wasm*
-rw-r--r-- 2 staff staff 5.6K 11 19 16:02 md4w.d.ts
-rw-r--r-- 2 staff staff 7.4K 11 19 16:02 md4w.js
-rw-r--r-- 2 staff staff 155 11 19 16:02 unwasm.js
The code to reproduce is available in this stackblitz
Is there other ways that could import the wasm from this md4w package without import from the external url and could be bundled into the dist? Thanks a lot!
Share Improve this question edited Nov 19, 2024 at 20:05 Eli 618 bronze badges asked Nov 19, 2024 at 10:37 Isaiah ChanIsaiah Chan 11 bronze badge1 Answer
Reset to default 0md4w uses the exports field, which prevents accessing internal files, so the docs are wrong, you can't import under "md4w/js/*"
.
It does have unwasm support, so I tried installing that, but it didn't seem to help.
Here's some "hacky" workarounds instead:
- Copy the wasm file into your project (works for me): example stackblitz.
- Use a CDN version (haven't tried, should also work, but would have worse perf).
本文标签: typescriptFailed to import WASM using url in Vite ProjectStack Overflow
版权声明:本文标题:typescript - Failed to import WASM using ?url in Vite Project - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745568001a2156565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论