Nodemon 破坏了汇总的热重载插件



我正在研究如何开发web应用程序。我使用Svelte作为前端,Express.js作为后端。我已经配置了一切,以便Express应用程序为客户机提供构建的应用程序。这是我的index.ts:

import express, { Router } from "express"
const app = express()
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "public", "index.html"))
})
app.listen(3000, () => {})

我的rollup.config.js(我已经删除了serve函数,因为我不需要它。否则,它与Svelte的默认rollup配置相同):

import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
const production = !process.env.ROLLUP_WATCH;
// function serve() {
//  let server;
//  function toExit() {
//      if (server) server.kill(0);
//  }
//  return {
//      writeBundle() {
//          if (server) return;
//          server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
//              stdio: ['ignore', 'inherit', 'inherit'],
//              shell: true
//          });
//          process.on('SIGTERM', toExit);
//          process.on('exit', toExit);
//      }
//  };
// }
export default {
input: 'src/main.ts',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
// enable run-time checks when not in production
dev: !production
}
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: 'bundle.css' }),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
// !production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};

我的package.json的脚本部分:

"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w & tsc -w -p tsconfig.backend.json & nodemon dist/index.js",
"start": "sirv public --no-clear",
"check": "svelte-check --tsconfig ./tsconfig.json"
},

dev中,我在监视模式下启动rollup的构建过程,tsctsconfig.backend.json进程也是如此,最后启动nodemon的后端服务器的编译版本。

所以一切都工作(虽然我承认这很有说服力),但是我的方法有一个恼人的问题。每当我更改任何.svelte文件时,所有内容都会重新加载,没有任何问题,但如果我更改index.ts中的一些代码,服务器会重新加载,并且浏览器的页面向我发出信号,表示它已经失去了与服务器的连接。然后我必须手动重新启动页面才能连接到它。我怎样才能使我的浏览器在重新加载时不会失去与服务器的连接?

编辑:无论在何处进行更改,服务器都无法热加载。

似乎在我添加了一点延迟到nodemon之后,一切都被修复了。package.json的脚本部分:

"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w & tsc -w -p tsconfig.backend.json & nodemon -d 1 dist/index.js",
"start": "sirv public --no-clear",
"check": "svelte-check --tsconfig ./tsconfig.json"
},

注意devnodemon旁边的-d 1

最新更新