Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): build watch mode #1449

Merged
merged 12 commits into from
Apr 5, 2021
Merged

Conversation

stafyniaksacha
Copy link
Contributor

@stafyniaksacha stafyniaksacha commented Jan 9, 2021

Add build watch mode (through rollup)

See #1434

Enable watch mode

cli

vite build --watch

vite.config.ts

import { defineConfig } from 'vite'
import { WatcherOptions } from 'rollup'

const watcherOptions: WatcherOptions = {}
export default defineConfig({
  build: {
    watch: watcherOptions, // null to disable (default)
  },
})

Todo

  • vite.config.ts reload
  • Tests
  • Update documentation

@xueqingxiao xueqingxiao mentioned this pull request Feb 24, 2021
@aminya
Copy link
Contributor

aminya commented Mar 8, 2021

Could we get this faster? Maybe defer reloading the config file to a separate follow-up pull request?

@antfu antfu added the enhancement New feature or request label Mar 15, 2021
@stafyniaksacha
Copy link
Contributor Author

stafyniaksacha commented Mar 18, 2021

Maybe defer reloading the config file to a separate follow-up pull request?

I think this will be the way I will follow, introducing a watcher for vite.config.ts/vite.config.js file seem to be harder than I expected: What I ended is to create a custom chokidar instance to watch them.

The caveat is that updated plugins will not trigger rebuild ... and that one of the main goal of this functionality

Maybe we can create a custom vite watch command instead to keep things clear

@yyx990803 where I can add related tests for that functionality ?

packages/vite/src/node/cli.ts Outdated Show resolved Hide resolved
packages/vite/src/node/build.ts Outdated Show resolved Hide resolved
packages/vite/src/node/build.ts Outdated Show resolved Hide resolved
Shinigami92
Shinigami92 previously approved these changes Mar 19, 2021
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Havn't tested it, but code looks okay to me 🙂

We may await additional reviews from e.g. core members

@Shinigami92 Shinigami92 added p2-nice-to-have Not breaking anything but nice to have (priority) 🔍 review needed labels Mar 22, 2021
@patak-dev patak-dev linked an issue Mar 22, 2021 that may be closed by this pull request
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great!

As @Shinigami92 suggested, we should add the -w option as webpack does.

Also, it would be nice to add line breaks so the output looks like

vite v2.1.2 building for production...

watching for file changes...

 18 modules transformed.

And it is easier to see that we are in watch mode

There should also be an extra break line between build outputs so it is easy to identify each one.

Current output:

λ yarn build --watch
yarn run v1.22.10
$ vite build --watch
vite v2.1.2 building for production...
watching for file changes...
 18 modules transformed.
dist/index.html                  0.16kb
dist/assets/index.cef4026c.js    2.49kb / brotli: 0.75kb
dist/assets/vendor.ebcc162a.js   196.17kb / brotli: 41.34kb
built in 1650ms.

Note: Using vite preview I was not able to reload the page after the re-build. Do you know why this is happening? I tried with serve and it works correctly.

@KieranP
Copy link

KieranP commented Mar 31, 2021

Would be great if this was able to push the updated files to the browser also, in the same way that Snowpack does. Besides watching files and building them, it also spins up a small node web server through which it pushes updates to the browser via websockets. In development builds, it injects a small bit of JS into the build which connects to the local node server to receive those updates. Works really well. Just a thought...

@stafyniaksacha
Copy link
Contributor Author

stafyniaksacha commented Apr 2, 2021

Also, it would be nice to add line breaks so the output looks like

I improved the output and added the -w shorthand !

$ vite build -w
vite v2.1.5 building for production...

watching for file changes...

build started...
✓ 4 modules transformed.
dist/index.html                  1.63kb
dist/assets/index.3e06ead4.css   0.23kb / brotli: 0.13kb
dist/assets/index.6c81d39f.js    0.63kb / brotli: 0.32kb
built in 343ms.

build started...
✓ 5 modules transformed.
dist/index.html                  1.63kb
dist/assets/index.3e06ead4.css   0.23kb / brotli: 0.13kb
dist/assets/index.6c81d39f.js    0.63kb / brotli: 0.32kb
built in 231ms.

Note: Using vite preview I was not able to reload the page after the re-build. Do you know why this is happening? I tried with serve and it works correctly.

The main goal of the two commands are different: vite serve is an alias of vite command, so it will not pass through rollup. It uses its own watcher and inject HMR functionalities. Unlike vite preview which just run a http server with sirv package on the outDir keeping the served files untouched.

@patak-dev
Copy link
Member

Great! Thanks for taking care of that details 👍🏼

I meant that reloads works using https://www.npmjs.com/package/serve, but it doesn't reload correctly using vite preview that as you said, uses sirv internally. I think this is unrelated to this PR though.

patak-dev
patak-dev previously approved these changes Apr 2, 2021
@patak-dev patak-dev merged commit 0dc6e37 into vitejs:main Apr 5, 2021
@patak-dev
Copy link
Member

Thanks a lot for implementing this feature @stafyniaksacha 🙌🏼

@stafyniaksacha stafyniaksacha deleted the feat-cli-build-watch branch April 5, 2021 16:47
@cawa-93
Copy link
Contributor

cawa-93 commented Apr 5, 2021

@stafyniaksacha How to use it programmatically? In my project I run

const {build} = require('vite')

customFileWatcher(() => build({mode, configFile: 'path/to/vite.config.js'}) )

It's not very obvious to me how to programmatically run a build and file watcher after this PR.

@stafyniaksacha
Copy link
Contributor Author

stafyniaksacha commented Apr 5, 2021

hi @cawa-93 this should do the tricks:

const { build } = require('vite')
const config = { 
  mode,  
  configFile: 'path/to/vite.config.js',
  build: {
  /**
   * Rollup watch options
   * https://rollupjs.org/guide/en/#watchoptions
   */
    watch: {}
  }
}
customFileWatcher(() => build(config) )

})

// stop watching
watcher.close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is close called immediately?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build watch mode
8 participants