如何使用vue-cli 3创建两个单独的包?

Ale*_*nko 29 vue.js vuejs2 vue-cli

我想构建两个独立的vue应用程序,这些应用程序将在快速应用程序中的两个不同路由上提供:"公共"vue应用程序和"admin"vue应用程序.这两个应用程序有自己的路由器和商店,但它们共享许多自定义组件.如何编辑默认的webpack模板,使其根据我的两个不同的入口点('public'和'admin')输出两个单独的包?目标是最终得到一个或多或少像这样的设置:

my-app/
+- ...
+- dist/
|  +- admin/         Admin bundle and files
|  +- public/        Public bundle and files
+- src/
|  +- components/    Shared components
|  +- admin/         Entry point, router, store... for the admin app
|  +- public/        Entry point, router, store... for the public app
+- ...
Run Code Online (Sandbox Code Playgroud)

必须由可用的2个开发服务器http:// localhost:8080/adminhttp:// localhost:8080/public 每个项目必须位于dist的自己的文件夹中,并且自己的公共

我今天拥有的:在根目录中创建文件vue.config.js使用:

module.exports = {
  // tweak internal webpack configuration.
  // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  chainWebpack: config => {
    // If you wish to remove the standard entry point
    config.entryPoints.delete('app')

    // then add your own
    config.entry('admin')
      .add('./src/admin/index.js')
      .end()
    .entry('public')
      .add('./src/public/index.js')
      .end()
  }
}
Run Code Online (Sandbox Code Playgroud)

Kam*_*han 18

假设您需要完全独立的构建,并以条目为指导使用一些共享脚本,则可以添加独立的构建命令。

在您的package.json“脚本”部分中:

"scripts": {
    "build:admin": "vue-cli-service build --dest dist/admin src/admin/index.js,
    "build:public": "vue-cli-service build --dest dist/public src/public/index.js
}
Run Code Online (Sandbox Code Playgroud)

对于管理员版本,您可以运行:

npm run build:admin
Run Code Online (Sandbox Code Playgroud)

对于公共建筑:

npm run build:public
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看构建目标文档

  • 如果有人感兴趣,这里是@ElijahLofgren 想法的一个例子,但使用节点命令复制+粘贴+重命名文件,如果相应的构建命令一起使用:https://gist.github.com/educkf/97e76da8d8c2b5dad17846a7d576e205 (4认同)
  • 我忘了提及这两个配置文件在我的案例中包含的内容(我有“admin”和“main”而不是“admin”和“public”)。你可以在这里看到:https://gist.github.com/ejlofgren /0f8368b5c0a74ebc94604558303a73e7 作为参考,我还在该要点的“package.json 脚本片段”下包含了我的 package.json 文件的一部分。 (3认同)
  • 当使用`vue-cli-service`时,最好使用`--no-clean`,否则在每个构建中都将删除`dist`目录。 (2认同)
  • @EduardoC.K.Ferreira 我最终创建了一个单独的文件夹并将 vue.config.js 的 2 个版本放在该文件夹中。然后我制作了 2 个 bat 文件,每个文件都执行以下操作: 1. 从该目录中复制一个配置文件(例如 vue.adminConfig.js)。2. 删除原来的 vue.config.js 3. 将 vue.adminConfig.js 文件重命名为 vue.config.js 4. 最后运行特定的构建命令,例如 npm run build:admin (2认同)

Luc*_*vet 14

我对此事也很感兴趣.

也许我们可以用子页面来解决这个问题:

https://cli.vuejs.org/config/#pages:"以多页模式构建应用程序.每个"页面"应该有一个相应的JavaScript条目文件.该值应该是一个对象,其中键是名称条目,值是:"

module.exports = {
  pages: {
    index: {
      // entry for the *public* page
      entry: 'src/index/main.js',
      // the source template
      template: 'public/index.html',
      // output as dist/index.html
      filename: 'index.html'
    },
    // an admin subpage 
    // when using the entry-only string format,
    // template is inferred to be `public/subpage.html`
    // and falls back to `public/index.html` if not found.
    // Output filename is inferred to be `admin.html`.
    admin: 'src/admin/main.js'
  }
}
Run Code Online (Sandbox Code Playgroud)