简体   繁体   English

使用 Vite 作为另一个构建的副作用生成独立的 js 工件

[英]Generate stand alone js artifacts using Vite as side effect of another build

I'm using Vite (vite@3.1.8) to build Typescript artifacts for an SPA "site" using SolidJS (solid-js@1.6.0).我正在使用 Vite (vite@3.1.8) 使用 SolidJS (solid-js@1.6.0) 为 SPA“站点”构建 Typescript 个工件。

here's my vite.config.ts这是我的 vite.config.ts

import { defineConfig, resolveBaseUrl } from 'vite'
import solidPlugin from 'vite-plugin-solid'

export default defineConfig({
  plugins: [solidPlugin()],
  server: {
    port: 3000,
  },
  build: {
    target: 'esnext',
    outDir: '../htdocs',
    rollupOptions: {
      input: {
        index: "./index.html",
        dev: "./dev.html",
        test: "./test.ts",
      },
      output: {
        entryFileNames: `assets/[name].js`,
        chunkFileNames: `assets/[name].js`,
        assetFileNames: `assets/[name].[ext]`
      }
    },
  },
});

Currently, it actually builds 2 html files (index.html and dev.html) and the artifacts needed to run those files.目前,它实际上构建了 2 个 html 文件(index.html 和 dev.html)以及运行这些文件所需的工件。 Its great.这很棒。 Couldn't be happier.不能更快乐。

I would like to have the transpiler to also kick out test.js so that I can run it to do some sanity checking before deploying to production.我想让转译器也踢出 test.js,这样我就可以在部署到生产环境之前运行它来做一些健全性检查。

I'm hoping to do vite build , and then run node../htdocs/assets/test.js (or something similar), and have it block the final deployment if any my sanity tests fail.我希望做vite build ,然后运行node../htdocs/assets/test.js (或类似的东西),如果我的健全性测试失败,让它阻止最终部署。

however, when I attempt to do this, I get an error when I run test.js, complaining about my use of import statements.但是,当我尝试这样做时,我在运行 test.js 时遇到错误,抱怨我使用了 import 语句。

Warning: To load an ES module, set "type": "module" in the package.json or use the.mjs extension.

setting my package type to module in package.json doesn't fix it.将我的 package 类型设置为 package.json 中的模块并不能修复它。 changing the test file to test.mjs doesnt fix it.将测试文件更改为 test.mjs 并不能解决问题。 I'm not really sure what to try next.我不太确定接下来要尝试什么。

What I really wish it would do is do the whole "import" as part of transpiling, and make one self-contained test.js that just runs.我真正希望它做的是将整个“导入”作为转译的一部分,并制作一个可以运行的独立 test.js。 It seems like that is what it does when it builds index.html and dev.html, why wont it do that for my ts file?看起来这就是它在构建 index.html 和 dev.html 时所做的,为什么它不会为我的 ts 文件这样做?

That should work.那应该工作。 I just tried making a new repo with your vite.config.ts , one-line index.html , dev.html , and test.ts files, and vite , vite-plugin-solid , solid-js installed.我只是尝试用你的vite.config.ts ,一行index.htmldev.htmltest.ts文件制作一个新的回购协议,并安装vitevite-plugin-solidsolid-js In the end I got a ../htdocs/assets/test.js file.最后我得到了一个../htdocs/assets/test.js文件。

You might also be interested in checking out Vitest which makes testing like this easier to do, and won't accidentally end up in your deployed htdocs.您可能也有兴趣查看Vitest ,它使这样的测试更容易进行,并且不会意外地出现在您部署的 htdocs 中。

The best solution I could find was to make a separate config file for building the tests.我能找到的最佳解决方案是制作一个单独的配置文件来构建测试。

 import { defineConfig } from 'vite' import solidPlugin from 'vite-plugin-solid' export default defineConfig({ plugins: [solidPlugin()], server: { port: 3000, }, build: { target: 'esnext', outDir: '../htdocs', lib: { entry: "./test-runner.ts", name: "test-runner", fileName: "test-runner" }, rollupOptions: { }, }, });

and then, update my package.json to make my test script compile and run the output from that alternative vite config.然后,更新我的 package.json 以使我的测试脚本编译并从该替代 vite 配置运行 output。

 "scripts": { "start": "vite", "dev": "vite", "build": "vite build", "serve": "vite preview", "test": "vite build --config vite.config-tests.ts && node../htdocs/test-runner.umd.js" },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM