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

Run a process on parcel watch rebuild #1131

Closed
NicolaiSchmid opened this issue Apr 4, 2018 · 11 comments
Closed

Run a process on parcel watch rebuild #1131

NicolaiSchmid opened this issue Apr 4, 2018 · 11 comments

Comments

@NicolaiSchmid
Copy link

Choose one: is this a 🐛 bug report or 🙋 feature request?
Feature request

I'm using parcel to build my backend nodejs code and it would be nice to integrate a nodemon-like feature into parcel.
Currently, I'm watching my files with parcel and it would very helpful to just specify a process that should be run after each build. Eg my freshly compiled script.

💁 Possible Solution

Maybe add an --exec flag to the watch command that runs the specified value as a process and restarts it after the build finishes.

@mischnic
Copy link
Member

mischnic commented Apr 4, 2018

See #800. There are also examples there on how to do this using the javascript api (also here in the docs).

@DeMoorJasper
Copy link
Member

If it's a supershort command we could RFC this flag, but like mentioned in #800 this is a very niche use-case and is probably better served by the api

@NicolaiSchmid
Copy link
Author

Implementing this in code is fine. But for users who like to use parcel for node projects, like me, it would be nice to be 'configuration-free' and just be able to execute the node process as well:
parcel watch src/index.js --target node --out-dir build/ --exec node build/
This enables me to run my project directly with hot reloading.

@mischnic
Copy link
Member

mischnic commented Apr 4, 2018

Yes, that makes sense, #800 is more about an additional build tool.
You want "run my entrypoint (in this case src/index.js) when using target=node".

See #935 (Parcel should run script differently with different targets):

When user runs something like parcel src/scripts/server.js -t node -d dist/server -o index.js, parcel should run node dist/server/index.js or nodemon dist/server/index.js.

@NicolaiSchmid
Copy link
Author

Ah, then I will just subscribe to that issue and see what happens. For now, my implementation using the api is working fine, so thanks for your help!

@sebbean
Copy link

sebbean commented Oct 4, 2018

@NicolaiSchmid would love to see how you're running this.

Have a usecase for a run on bundle finished.

do you just run a js file that runs runBundle() ? eg: node run.js

@NicolaiSchmid
Copy link
Author

I'm not completely able to recall my specific requirements from April, but since then I have switched to a webpack solution using webpack-nodemon, which covers all my problems.
https://github.com/wasc-io/tools/blob/master/config/webpack.config.backend.prod.js

@felipemullen
Copy link

@sebbean I just implemented a quick watcher using the parcel-bundler api. It was pretty quick, maybe this will help you.

  • add an npm script command to your package file:
...
"scripts": {
    "watch": "node my-bundler.js"
}
const Bundler = require('parcel-bundler');
const Path = require('path');
const { execSync } = require('child_process');

const entryFiles = [
    Path.join(__dirname, './src/index.html')
];

const options = {
    outDir: './public',
    publicUrl: '/my-host/',
    watch: true,
    minify: false
};

(async () => {
    const bundler = new Bundler(entryFiles, options);
    
    bundler.on('buildEnd', () => {
        const postBuildFile = Path.join(__dirname, './some-script.sh');
        console.log(`running: ${postBuildFile}`);
        const stdout = execSync(`${postBuildFile}`);
        // Do things with stdout
    });

    const bundle = await bundler.bundle();
})();
  • Run your bundler with npm run watch

@vertcitron
Copy link

vertcitron commented Mar 1, 2020

In the case of node development, it would very usefull to launch something after each build completes, but not necessarily the node script, we often need to launch jest tests too... so an --exec would be nice...

@xialvjun
Copy link

Why close this issue ?

@scolastico
Copy link

I know this is old, but i do not understand why parcel does not have a simple argument, accepting a command which is run after the watcher did its build. Anyway this is my solution to the problem:

	"scripts": {
		"build": "parcel build",
		"dev": "concurrently npm:dev:build npm:dev:run",
		"dev:build": "parcel watch",
		"dev:run": "sleep 2 && nodemon --delay 2 --watch dist --exec npm run dev:start",
		"dev:start": "node dist/index.cjs",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants