Parcel: Run a process on parcel watch rebuild

Created on 4 Apr 2018  路  10Comments  路  Source: parcel-bundler/parcel

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.

Most helpful comment

@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

All 10 comments

See https://github.com/parcel-bundler/parcel/issues/800. There are also examples there on how to do this using the javascript api (also here in the docs).

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

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.

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 https://github.com/parcel-bundler/parcel/issues/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.

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!

@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

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

@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

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...

Why close this issue ?

Was this page helpful?
0 / 5 - 0 ratings