Automatic Module Installation

Node module dependency

gulp-tron uses various node modules, but most of them are not installed automatically becuase typically only parts of them are used depending on user's project requirements. To minimize the overhead on module dependency, gulp-tron tries to keep it's own dependency as small as possible. So, when you first try to use it, you could see many warnings like this:

[01:14:22] Starting '01-watcher:scss'...
[01:14:22] '01-watcher:scss' errored after 48 ms
[01:14:23] Error: Cannot find module 'gulp-sass'
Require stack:
- D:\dev\pub\gulp-tron\lib\plugins\CSSPlugin.js
...

If you see error messages like 'Cannot find module...', then you have to install the the missing modules required - 'gulp-sass' in this case. When you run gulp-tron for the first time, then you can see this error messages repeatedly for all the modules used by gulp-tron internally. To ease this inconvenience, gulp-tron provides automatic module installation feature.

Automatic module installation

Automatic module installation option is enabled by default for easier user experience. If this is enabled, there could be small performance degradation, but for most project scales it'd be fine. With this feature enabled, all the modules used by gulp-tron will be automatically instsalled and the package.json file will be updated accordingly as per the given installation options (default: '--save-dev'). For the moduels already installed, installation action is skipped.

gulp-tron provides two ways of enabling automatic module installation.

Using Command line

If --npm-auto-install (--npm-auto for short) option is given to gulp in command line, automatic installation option is enabled. Optionally, you can specify package manager and installation options to the package manager.

Here are some examples:

npx gulp task1 --npm-auto # default package manager is npm, install option is '--save-dev'
npx gulp task1 --npm-auto=npm # the same ase above
npx gulp task1 --npm-auto=pnpm # set package to pnpm with default install option '--save-dev'
npx gulp task1 --npm-auto=yarn # set package to yarn with default install option '--dev'
npx gulp task1 --npm-auto=--no-save # default package manager is npm, set install option to '--np-save'
npx gulp task1 --npm-auto="pnpm add --no-save" # custom install command: need full install command

Using API

Using tron.setPackageManager(options), automatic installation options can be turned on or off. In addition, package manager can also be selected with preferred install options.

Usage examples

const tron = require('gulp-tron');
// default package manager is npm, install option is '--save-dev', autoInstall is true
// set package manager to pnpm, install option is set to default, '--save-dev'
tron.setPackageManager({name: 'pnpm'});
// set package manager to yarn, install option is set to default, '--dev'
tron.setPackageManager({name: 'yarn'});
// default package manager is npm, set install option to '--save'
tron.setPackageManager({installOptions='--save'});
// disable auto-installation
tron.setPackageManager({autoInstall: false});
options.name

Package manager name. If known package manager is specified alone, options.installOptions is set to defaults according.

Currently, npm, pnpm, yarn are known package manager names.

options.installOptions

Options to install command. Default is '--save-dev' for npm and pnpm. '--dev' for yarn.

options.autoInstall

Enable or disable automatic module installion. default is false.

Auto-install for users

Modules required in gulpfile.js can be installed automatically using tron.require() function or npmInstall property of BuildConfig.

Usage example:

const scss = {
name: 'scss',
builder: (rtb) => {
const sass = tron.require('gulp-sass');
rtb.src().pipe(sass().on('error', sass.logError)).dest();
},
src: 'assets/scss/**/*.scss',
dest: 'www/css',
clean: 'www/css'
}
const scripts = {
name: 'babel',
builder: (rtb) => rtb.src().pipe(require('gulp-babel')()).dest(),
buildOptions: { babel: true },
src: 'assets/js/**/*.js',
dest: 'www/js',
clean: 'www/js',
npmInstall: ['gulp-babel', '@babel/core'] // packages to be installed before build execution
}

Modules specified in BuildConfig.npmInstall are installed right before the build item is executed. tron.require() will install the required module when it's called. So, if it is called inside the build function, the required module will be installed only when the task is executed, not when the gulpfile is loaded.