最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change the entry point to something other than the default 'srcmain.js' using Vite? - Stack

programmeradmin2浏览0评论

I want to point to a different location and file name and deviate from the Vite standard.

Let's say my js file is in out/frontend/../../out.js. I got it working when I rename it to main.js and copy it in the root of my project where index.html and vite.config.js live. But I would prefer to change the entry point somehow...

I tried this:

rollupInputOptions: {
  // or whatever your entry file is
  input: resolve(__dirname, 'out/frontend/my/path/out.js')
}

Did not work. Any suggestions?

I want to point to a different location and file name and deviate from the Vite standard.

Let's say my js file is in out/frontend/../../out.js. I got it working when I rename it to main.js and copy it in the root of my project where index.html and vite.config.js live. But I would prefer to change the entry point somehow...

I tried this:

rollupInputOptions: {
  // or whatever your entry file is
  input: resolve(__dirname, 'out/frontend/my/path/out.js')
}

Did not work. Any suggestions?

Share Improve this question asked Jul 1, 2021 at 20:19 user3350744user3350744 4792 gold badges5 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

As of Vite 2, the entry file is specified in a <script> tag in index.html:

<script type="module" src="/src/main.js"></script>

You can change that file to the one you want:

<script type="module" src="/out/frontend/my/path/out.js"></script>

demo

Use resolve from path module and add an alias in vite.config.js config to resolve paths, check.

import { resolve } from 'path'

module.exports = {
  ...
  resolve: {
    alias: {
      '@': resolve(__dirname, '../out/'), // resolve path
    },
  },
  rollupInputOptions: {
    input: resolve(__dirname, '../out/entrypoint.js') // custom main
  }
}
  • to import a module from resolved path use '@', eg: import { myModule } from '@/mymodule'
  • You will have to adjust the path of the new entrypoint in the index.html file.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论