vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.21k stars 6.04k forks source link

Better SSR support [watch build] #972

Closed surmon-china closed 3 years ago

surmon-china commented 3 years ago

Feature

I am using vite to build an SSR website, during which I tried to implement a nuxt-like, faster SSR framework, but I need better support.

I wish build and ssrBuild can support watch mode, or there be watchBuild and watchSSRBuild functions

SSR inevitably requires real-time compilation of products (Client & Server)

In my test.

First, a form of fileWatcher(files, () => build().then(() => restart())) is used to track file changes and execute compile, but this will do a full build without cache support, which takes a long time.

const watcher = chokidar.watch(path.resolve(__dirname, 'src'), {
  ignored: [/node_modules/, /\.git/],
  awaitWriteFinish: {
    stabilityThreshold: 100,
    pollInterval: 10
  }
})

watcher.on('change', info => {
  ssrBuild({/* ... */})
})

Second. If you use rollup.watch to compile, you can quickly update the product and achieve a better development experience.

const rullupConfig = {
    input: path.resolve(root, entry),
    preserveEntrySignatures: false,
    ...rollupInputOptions,
    // ...
}

 const watcher = rollup.watch({
    ...rullupConfig,
    output: {
        // ...
        ...rollupOutputOptions
    },
    watch: {
        ...rollupWatchOptions
        chokidar: chokidar.watch(/* watchPath */, {
            ignored: [/node_modules/, /\.git/],
            awaitWriteFinish: {
                stabilityThreshold: 100,
                pollInterval: 10
            }
        }),
    }
})

watcher.on('event', event => {
    if (event.code === 'BUNDLE_END') {
        doRestart()
        // emit something...
        // write files...
        // ...
    }
});

However, the implementation of the build function in the vite src/node/build/index.ts:build file is too complex and deeply coupled, and I have to re-implement it externally, so I hope this can be extracted as a single function.

links

aleclarson commented 3 years ago

Watch support for build would also be super useful when working on legacy bundles (via vite-plugin-legacy).

surmon-china commented 3 years ago

I can submit a pull request to implement this feature.

@yyx990803 @antfu @underfin

TechAkayy commented 3 years ago

vite build --watch (defaulted to dev mode) will be very invaluable...

Similar to.. @surmon-china

vite build --watch --hmr like snowpack (https://github.com/snowpackjs/snowpack/issues/1002) would be the ultimate...

arpowers commented 3 years ago

@surmon-china any updates on the PR?

I've taken a look at Vite source and I agree this would be a pretty simple change. Basically, Rollup should just run rollup.watch instead of rollup.rollup if we are in NODE_ENV=development mode. From there, I think we would be able to add the standard watch options via the existing rollup options.

I would do the PR myself but I can't figure out exactly how I'm supposed to run the playground, testing, and dev server to create an acceptable result... For someone experienced with the codebase, any thoughts?

cawa-93 commented 3 years ago

watch support for build would also be super useful when you develop electron app.

wxs77577 commented 3 years ago

Any update on this feature?

yyx990803 commented 3 years ago

Closing to track in #1290 & #1434