|
| 1 | +# gulp-core-build [](https://badge.fury.io/js/%40microsoft%2Fgulp-core-build) |
| 2 | + |
| 3 | +[](https://travis-ci.org/Microsoft/gulp-core-build) [](https://david-dm.org/Microsoft/gulp-core-build) |
| 4 | + |
| 5 | +The gulp build system, along with its rich plugin ecosystem, is a very powerful tool for web development projects. |
| 6 | +However, project gulp build setups become difficult to manage over time, as gulpfiles grow in complexity. This project |
| 7 | +simplifies a number of aspects of getting a build setup going for a majority of scenarios. |
| 8 | + |
| 9 | +Core build defines a contract for tasks to implement, such that they can share opinions about where things end up. Tasks are modular but they are designed to work well together. |
| 10 | + |
| 11 | +With gulp core build, your gulpfile translates into a list of task definitions, each which define what to run: |
| 12 | + |
| 13 | +```typescript |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// Import core build and the tasks the project needs. |
| 17 | +let build = require('gulp-core-build'); |
| 18 | +let lint = require('gulp-core-build-typescript').tslint; |
| 19 | +let typescript = require('gulp-core-build-typescript').typescript; |
| 20 | +let sass = require('gulp-core-build-sass').default; |
| 21 | +let karma = require('gulp-core-build-karma').default; |
| 22 | +let webpack = require('gulp-core-build-webpack').default; |
| 23 | +let serve = require('gulp-core-build-serve').default; |
| 24 | + |
| 25 | +// Define gulp tasks. |
| 26 | +let buildTasks = build.task('build', build.parallel(lint, typescript, sass)); |
| 27 | +let testTasks = build.task('test', build.serial(buildTasks, karma)); |
| 28 | +let bundleTasks = build.task('bundle', build.serial(buildTasks, webpack)); |
| 29 | +let serveTasks = build.task('serve', build.serial(bundleTasks, serve)); |
| 30 | +let defaultTasks = build.task('default', testTasks); |
| 31 | + |
| 32 | +// Initialize! |
| 33 | +build.initialize(require('gulp')); |
| 34 | +``` |
| 35 | + |
| 36 | +# Usage |
| 37 | + |
| 38 | +Within your project, install gulp, gulp-core-build, and the tasks you need: |
| 39 | + |
| 40 | +``` |
| 41 | +npm install --save-dev gulp gulp-core-build |
| 42 | +``` |
| 43 | + |
| 44 | +Then install the tasks you need: |
| 45 | + |
| 46 | +``` |
| 47 | +npm install --save-dev gulp-core-build-typescript gulp-core-build-karma gulp-core-build-webpack gulp-core-build-serve |
| 48 | +
|
| 49 | +``` |
| 50 | + |
| 51 | +Create a gulpfile.js that sets up the tasks in the way you want them to run: |
| 52 | + |
| 53 | +```javascript |
| 54 | +'use strict'; |
| 55 | + |
| 56 | +// Import core build. |
| 57 | +let build = require('gulp-core-build'); |
| 58 | + |
| 59 | +// Import the tasks. |
| 60 | +let lint = require('gulp-core-build-typescript').tslint; |
| 61 | +let typescript = require('gulp-core-build-typescript').typescript; |
| 62 | +let sass = require('gulp-core-build-sass').default; |
| 63 | +let karma = require('gulp-core-build-karma').default; |
| 64 | +let webpack = require('gulp-core-build-webpack').default; |
| 65 | +let serve = require('gulp-core-build-serve').default; |
| 66 | + |
| 67 | +// Shorthand for defining custom subtasks |
| 68 | +// The proper method for this is to introduce a new package which exports a class that extends GulpTask |
| 69 | +// However, this shorthand allows an easy way to introduce one-off subtasks directly in the gulpfile |
| 70 | +let helloWorldSubtask = build.subTask('do-hello-world-subtask', function(gulp, buildOptions, done) { |
| 71 | + this.log('Hello, World!'); // use functions from GulpTask |
| 72 | +}); |
| 73 | + |
| 74 | +// Define gulp tasks. |
| 75 | +let buildTasks = build.task('build', build.parallel(helloWorldSubtask, lint, typescript, sass)); |
| 76 | +let testTasks = build.task('test', build.serial(buildTasks, karma)); |
| 77 | +let bundleTasks = build.task('bundle', build.serial(buildTasks, webpack)); |
| 78 | +let serveTasks = build.task('serve', build.serial(bundleTasks, serve)); |
| 79 | +let helloWorldTasks = build.task('hello-world', helloWorldSubtask); |
| 80 | +let defaultTasks = build.task('default', testTasks); |
| 81 | + |
| 82 | +// Tell the build to set up gulp tasks with the given gulp instance. |
| 83 | +build.initialize(require('gulp')); |
| 84 | +``` |
| 85 | + |
| 86 | +Once this is set up, you should be able to execute the gulp tasks and they should run in the order you defined. |
| 87 | + |
| 88 | +# Available tasks |
| 89 | + |
| 90 | +| Task name | Description | |
| 91 | +| --------- | ----------- | |
| 92 | +| [gulp-core-build-typescript](https://www.npmjs.com/package/@microsoft/gulp-core-build-typescript) | Builds and lints typescript. | |
| 93 | +| [gulp-core-build-sass](https://www.npmjs.com/package/@microsoft/gulp-core-build) | Compiles sass into css, into js modules, that are theme friendly. | |
| 94 | +| [gulp-core-build-webpack](https://www.npmjs.com/package/@microsoft/gulp-core-build-webpack) | Runs webpack given a config, and outputs libraries plus the stats and logging. | |
| 95 | +| [gulp-core-build-serve](https://www.npmjs.com/package/@microsoft/gulp-core-build-serve) | Sets up a server and live reload for a quick dev loop. | |
| 96 | +| [gulp-core-build-karma](https://www.npmjs.com/package/@microsoft/gulp-core-build-karma) | Runs unit tests in a browser using [Karma](https://www.npmjs.com/package/karma) | |
| 97 | +| [gulp-core-build-mocha](https://www.npmjs.com/package/@microsoft/gulp-core-build-mocha) | Runs unit tests in a NodeJS environment with [Mocha](https://www.npmjs.com/package/mocha) | |
| 98 | +| [sp-build-core-tasks](https://www.npmjs.com/package/@microsoft/sp-build-core-tasks) | Special tasks for developing with the SharePoint Framework (SPFx) | |
| 99 | + |
| 100 | +# API |
| 101 | + |
| 102 | +## task(name, task) |
| 103 | + |
| 104 | +Defines a named task to be registered with gulp as a primary gulp task, which will run the provided task when execution. |
| 105 | + |
| 106 | +## parallel(tasks) |
| 107 | + |
| 108 | +Runs a given list of tasks in parallel execution order. |
| 109 | + |
| 110 | +## serial(tasks) |
| 111 | + |
| 112 | +Runs a given list of tasks in serial execution order. |
| 113 | + |
| 114 | +## subtask(name, fn) |
| 115 | + |
| 116 | +Creates a subtask (which is not registered directly with gulp, use `task()` for that) which can be |
| 117 | +used with parallel and serial. The `this` variable in the callback function will be an instance of a `GulpTask`. |
| 118 | + |
| 119 | +`fn` should be a function which accepts 3 parameters: a `gulp` instance, the build configuration |
| 120 | +object, and an optional callback function to signify subtask completion. |
| 121 | + |
| 122 | +## initialize(gulpInstance, [buildOtions]) |
| 123 | + |
| 124 | +Registers the gulp tasks. |
| 125 | + |
| 126 | +The options are broken down into task-specific sections, and all are optional, so only provide the ones |
| 127 | +that require deviating from defaults: |
| 128 | + |
| 129 | +```typescript |
| 130 | +build.initializeTasks( |
| 131 | + require('gulp'), |
| 132 | + { |
| 133 | + build: { /* build options */ }, |
| 134 | + bundle: { /* bundle options */ }, |
| 135 | + test: { /* test options */ }, |
| 136 | + serve: { /* serve options */ }, |
| 137 | + nuke: { /* nuke options */ } |
| 138 | + }); |
| 139 | +``` |
| 140 | +# Building gulp-core-build |
| 141 | +1. ```npm install --force``` |
| 142 | +2. ```gulp``` |
| 143 | + |
| 144 | +# Defining a custom task |
| 145 | + |
| 146 | +The `subtask()` function is used to define a custom task. For example, |
| 147 | +you could create the following subtask, which is registered to the command |
| 148 | +`gulp hello-world`: |
| 149 | + |
| 150 | +```javascript |
| 151 | +let helloWorldSubtask = build.subTask('do-hello-world-subtask', function(gulp, buildOptions, done) { |
| 152 | + this.log('Hello, World!'); // use functions from GulpTask |
| 153 | +}); |
| 154 | + |
| 155 | +// Register the task with gulp command line |
| 156 | +let helloWorldTask = build.task('hello-world', helloWorldSubtask); |
| 157 | +``` |
| 158 | + |
| 159 | +Note that the command `gulp do-hello-world-subtask` would error. |
| 160 | + |
| 161 | + |
| 162 | +# License |
| 163 | + |
| 164 | +MIT |
0 commit comments