This project provides several common used tasks for Gulp.
It has built-in support for: Babel, React, Webpack 2 (HMR, Tree Shaking), Mocha and more.
- create project
yarn init
- add Yarn / NPM packages
yarn add -D cross-env gulp babel-register
yarn add -D gulp-predefined-tasks
Please install [email protected]
if you want to use HMR with React.
- add
gulpfile.babel.js
import registerTasks from 'gulp-predefined-tasks';
let options = {
// several options
};
registerTasks(gulp, options);
- edit
package.json
to add scripts
"scripts": {
"build": "gulp",
"build:dev": "cross-env NODE_ENV=development gulp build:dev",
"build:prod": "cross-env NODE_ENV=production gulp build:prod",
"build:watch": "cross-env NODE_ENV=watch gulp build:watch",
"clean": "gulp clean",
"test": "gulp test",
"test:watch": "gulp test:watch",
"webpack:dev": "cross-env NODE_ENV=development gulp webpack:dev",
"webpack:prod": "cross-env NODE_ENV=production gulp webpack:prod",
"start": "cross-env NODE_ENV=watch gulp webpack:watch"
},
- add
.babelrc
To support Tree Shaking feature, we should add two .babelrc
files.
In root folder:
{
"presets": ["es2015", "react", "stage-0"]
}
In srcDir
folder:
{
"presets": [
["es2015", { "modules": false }],
"stage-0",
"react"
],
"env": {
"watch": {
"plugins": [
"react-hot-loader/babel",
],
},
"test": {
"presets": ["es2015", "react", "stage-0"]
}
}
}
{
type: 'web',
srcDir: './src/',
testDir: './test/',
libDir: './lib/',
distDir: './dist/',
babel: { ... },
webpack: { ... }
}
Project type.
web
(default)- Standalone web app
lib
- NPM package
The folder to store source code files.
Default: ./src/
The folder to store test code files.
Default: ./test/
The folder to store published artifacts.
Default: ./lib/
The folder to store published distribution artifacts.
Default: ./dist/
The default Babel configuration.
Default: { presets: ["es2015", "react", "stage-0"] }
Webpack configuration. Or set to false
to disable webpack.
Default: false
{
entry: String or Array or Object,
config: String or Object, // default config file
devConfig: String or Object, // merge with `config` for `webpack:dev`
prodConfig: String or Object, // merge with `config` for `webpack:prod`
watchConfig: String or Object, // merge with `config` for `webpack:watch`
babel: Object,
devServer: {
host: String, // default: 0.0.0.0
port: bool, // default: 3000
hot: bool, // default: true
contentBase: bool or Object,
proxy: Object,
...
},
onMerge: function, // merge all config objects
onEntry: function, // rewrite entry item
onConfig: function, // handle merged config object
}
It could be webpack config file path, or webpack config object.
Babel options object.
Default: { presets: [["es2015", { modules: false }], "react", "stage-0"] }
The options object of WebpackDevServer.
- mode (String): The runtime mode:
dev
,prod
orwatch
. - object (Object): The destination object.
- sources (...Object): The source objects.
As default, it will concat any array values.
- mode (String): The runtime mode:
dev
,prod
orwatch
. - entry (String or Array): The value of entry.
- mode (String): The runtime mode:
dev
,prod
orwatch
. - config (Object): The merged config object.
To support gulp-imagemin
, we should add gulp-imagemin
to devDependencies
.
To support image-webpack-loader
, we should add image-webpack-loader
to devDependencies
.
The following argument could be set via CLI.
--libDir <path>
- Overwrite the value of
options.srcDir
- Overwrite the value of
--distDir <path>
- Overwrite the value of
options.distDir
- Overwrite the value of
--outputFilename <filename>
- Overwrite the value of
options.webpack.config.output.filename
- Overwrite the value of
--port <port>
- Overwrite the value of
options.webpack.devServer.port
- Overwrite the value of
--proxy <target>
- Overwrite the value of
options.webpack.devServer.proxy.'/'.target
- Overwrite the value of
--recursive
or-R
- [Incubating]
- Check all dependencies (with
file:
andlink:
prefix) fromdependencies
andlinkDependencies
, and execute same tasks in them.
For example:
# Customize the libDir in runtime
yarn run build:dev -- --libDir <NEW_LIB_DIR>
# Run build:dev for all projects
cd ./example/app
yarn run build:dev -- --recursive
Developer may overwrite the following tasks to fit their needs.
Show all tasks.
Build project in dev
mode.
- For
lib
project, it will runbuild:lib
andwebpack:dev
. - For
web
project, it will runwebpack:dev
.
Build project in prod
mode.
- For
lib
project, it will runwebpack:prod
. - For
web
project, it will runwebpack:prod
.
Build project as library
.
It will generate several artifacts in dev
mode.
Build project as distribution
.
It will generate several artifacts in prod
mode.
Watch project, and build as library
.
It depends on build:lib
.
Run webpack
to bundle artifacts in dev
mode.
Run webpack
to bundle artifacts in prod
mode.
Run webpack
to bundle artifacts in watch
mode.
Lint source codes using ESLint.
Run tests by using Mocha.
- Yarn
- Yarn ignore
.npmignore
when publishing artifacts
- Yarn ignore
- Webpack UglifyJsPlugin
- Unable to drop unused React component
- UglifyJS warning:
Side effects in initialization of unused variable XxxReactComponent
- UglifyJS warning:
- Unable to drop unused React component
- Webpack less-loader
- Unable to resolve module error when working with css-loader?modules=true