bluwy/create-vite-extra

deno-vue dependencies vs devDependencies

BoltDoggy opened this issue · 7 comments

目前 deno 仍然是模拟的 node 环境来运行 vite, 而 vite 并没有为 deno 的使用方式重新设计.

deno-vue 模板中移除了 package.json, 只能靠 deno 的自动收集依赖并生成 node_modules 来给 vue 项目使用.

所以当你为 vue 项目添加一些依赖时, 如 lodash, 你需要在 vue.config.js 中 import 一些 deno 运行环境并不需要的包, 导致 dep 和 devDep 混在了一起.

改进

创建 dep.mjs 专门用来管理前端依赖并生成 node_modules.

import 'npm:vue'
// import 'npm:lodash' // 如果需要 lodash 的话, 直接添加本行
                       // 另外支持 import 'npm:lodash@latest' 或 import 'npm:lodash@2' 指定依赖版本号

运行在 deno 环境下的 vite 自身则不需要生成在 node_modules 中.

deno.json 中的 task 配置修改如下:

{
  "tasks": {
    "install": "deno cache --unstable --node-modules-dir ./dep.mjs",
    "dev": "deno task install && deno run -A --unstable npm:vite"
    "build": "deno task install && deno run -A --unstable npm:vite build",
    "preview": "deno run -A --unstable npm:vite preview",
    "serve": "deno run --allow-net --allow-read https://deno.land/std@0.157.0/http/file_server.ts dist/"
  }
}

而对于 parceljs 这种自动收集依赖并安装的 cli 工具, 就不需要 deno 来考虑 node_modules 了, 只不过 parceljs 仍然是依赖了 npm/yarn 来进行的依赖安装.

期待 vite 内置自动收集依赖能力, 并基于 deno 的实现.

bluwy commented

我觉得也可用一件 vite 插件来自动扫描及收集,使用 deno 来安装,但会有点复杂些且也需解决 @latest 问题。dep.mjs 这概念瞒有趣, 但我不太熟悉 deno 现在对 node 环境的发展。@bartlomieju 在这方面可能有些资料。

Translation: I think we could have a Vite plugin to automatically scan and use deno to install the deps, but it may be a bit complicated along with the @latest issue. The dep.mjs idea is quite interesting, but I'm not too familiar with the recent deno-node compat development. Maybe @bartlomieju have some insights to this.


@bartlomieju (sorry for the ping again 😅) but maybe there's a place for suggestions like this too? So we can direct to there as there's not much the template can do here. For context, this issue is talking about separation of deps and devDeps in deno.

Sure, Deno's issue tracker is the best place to do that: https://github.com/denoland/deno/issues

I think it could be solved by using deps.ts and dev_deps.ts that are imported from the config file (I think).

bluwy commented

Closing this one for now. I think the idea can be experimented separately first and tested in userland.

@bluwy @bartlomieju 我觉得 deno 不需要考虑 deps 和 dev_deps.

create-vite-extra 中使用 --node-modules-dir 来生成 node_modules 是为了给前端项目中的 src 来用的, 我称之为 deps. 而 vite 运行在 deno 中, 不属于 src 中所需的 deps, 所以我称之为 dev_deps.

在前端发展早期, dev_deps 使用 npm 来安装, deps 使用 bower 来安装.

Deno's issue 要讨论的问题可能是 --node-modules-dir 参数如何生成类似 'package-lock.json' 文件

我感觉这个问题是由于 deno 本身的依赖设计不是像 node 那样区分 dev 还是 prod,于是在同个项目内出现运行和开发的依赖混杂。
其实只要保证项目运行的时候只会依赖 prod 的依赖就好了?因为在 deno 的视角来看,作为开发的工具和被开发的项目是没有什么区别的。

bluwy commented

看来 deno 文档 也有些管理依赖的建议,但篇写经验不太舒适。我也觉得 deno 不分 deps 和 dev deps 可能是故意的设计策略,所以在这方面留给使用者自行管理。

@bluwy 所以 deno 本身不需要再做什么了, create-vite-extra 生成的 deno-vue 中应该遵守管理依赖的最佳实践, 也就是添加一个 deps 文件给 vue 而不应该将 vite 全部的依赖生成到 node_modules 里

我已经提交了deno run -A --unstable npm:create-vite-deno --template deno-vue, 可以体验一下