using valnilla js why vite did'n include automaticly js file that imported to index.html for build ? while its work fine in dev

huangapple go评论65阅读模式
英文:

using valnilla js why vite did'n include automaticly js file that imported to index.html for build ? while its work fine in dev

问题

index.html

    <html lang="en">
    <head>
    <title>Document</title>
    </head>
    <body>
        <p id="num"></p>
    </body>
    <script type="module">
        import {sum} from './counter.js'
        const number = sum(2,2)
        document.getElementById('num').innerHTML = number
    </script>
    </html>
counter.js

    export function sum(a,b){
        return a+b
    }
after build
dist/assets/index-74746f.js counter.js already bundle inside it

dist/index.html

    <html lang="en">
    <head>
    <title>Document</title>
    </head>
    <body>
        <p id="num"></p>
    </body>
    </html>
there is nothing tag script that point to ./assets/index--74746f.js 

i know simply i can write <script src="./assets/index--74746f.js . but look not best practice
英文:

index.html

<html lang="en">
<head>
<title>Document</title>
</head>
<body>
    <p id="num"></p>
</body>
<script type="module">
    import {sum} from './counter.js'
    const number = sum(2,2)
    document.getElementById('num').innerHTML = number
</script>
</html>

counter.js

export function sum(a,b){
    return a+b
}

after build
dist/assets/index-74746f.js counter.js already bundle inside it

dist/index.html

<html lang="en">
<head>
<title>Document</title>
</head>
<body>
    <p id="num"></p>
</body>
</html>

there is nothing tag script that point to ./assets/index--74746f.js

i know simply i can write <script src="./assets/index--74746f.js . but look not best practice

答案1

得分: 0

实际上,在构建包含正确脚本标签的index.html之后。

<html lang="en">
<head>
<title>Document</title>
  <script type="module" crossorigin src="/assets/index-a1d6a87d.js"></script>
</head>
<body>
    <p id="num"></p>
</body>
</html>

我找不到任何选项来阻止内联模块脚本的分块,但另一方面,当所有的JavaScript都包括内联脚本时,这实际上并不是坏事。
如果您不喜欢捆绑脚本中的哈希值,您可以使用:

vite.config.js

import { defineConfig } from 'vite';

export default defineConfig(() => {
    return {
        build: {
            rollupOptions: {
                output: {
                    entryFileNames: 'index.js'
                }
            },
        },
    };
});
英文:

Actually after building the index.html contains a proper script tag.

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;title&gt;Document&lt;/title&gt;
  &lt;script type=&quot;module&quot; crossorigin src=&quot;/assets/index-a1d6a87d.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;p id=&quot;num&quot;&gt;&lt;/p&gt;
&lt;/body&gt;

&lt;/html&gt;

I couldn't find any option to prevent chunking the inline module script, but on the other hand it's actually not bad when all of your JS including inline one is bundled.
If you don't like a hash in the bundle script you could use:

vite.config.js

import { defineConfig } from &#39;vite&#39;;

export default defineConfig(() =&gt; {
    return {
        build: {
            rollupOptions: {
                output: {
                    entryFileNames: &#39;index.js&#39;
                }
            },
        },
    };
});

huangapple
  • 本文由 发表于 2023年7月10日 12:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定