@ideasonpurpose/gulp-task-styles v0.1.2
Gulp Task: Styles
Create pre-configured Gulp 4 tasks using Sass and PostCSS to generate CSS stylesheets.
Installation
$ yarn add @ideasonpurpose/gulp-task-styles
or
$ npm install @ideasonpurpose/gulp-task-stylesUsage
Basic
Call the create method directly on the import. In most cases, just trust the defaults and go:
// Call `create` on the require to initialize a new task
const sass = require("@ideasonpurpose/gulp-task-styles").create();
// Export to make the task publicly callable
exports.sass = sass;Custom options
The create method accepts one configuration object. This module accepts four properties:
src
A glob string or array of glob-strings. Defaults tosrc/sass/**/*Passed directly togulp.srcsrcOptions
An object containing any options recognized by gulp.src string or array of glob-strings. All options are passed directly togulp-srcexcept forsince.- srcOptions.since
To use incremental builds, set the value ofsincetotrue. This will be replaced withgulp.lastRunin the generated function. (Thegulpinstance will also need to be included, see gulp below)
- srcOptions.since
dest
The output path to be passed togulp.destDefaults todist/cssPassed directly togulp.destsassConfig
A Sass configuration object of settings to pass to gulp-sass (and then to node-sass). See Sass Config below for default settings. Passed directly togulp-sasspostcssPlugins
An array of plugins to pass to gulp-postcss. This array completely replaces the default set of plugins. See PostCSS Options below for defaults.
Passed directly togulp-postcssgulp
The current gulp instance. Required whensrcOptions.sinceistrue
Sass Config
The generated task uses a well-tested set of default Sass settings:
{
sassConfig: {
includePaths: ["node_modules"],
sourceComments: true,
outputStyle: "expanded"
}
};These settings can be overridden by adding a sassOptions key to the configuration object passed to create. Custom settings will be merged with the Sass defaults.
PostCSS Options
The generated task adds a well-tested default set of PostCSS transformations: autoprefixer and postcss-import and, if NODE_ENV is set to production, cssnano for minification and optimization.
{
postCssPlugins: [
autoprefixer({ grid: "no-autoplace" }),
postCssImport(),
cssnano() // if (NODE_ENV === "production")
];
}As with sassOptions, these settings can be overidden by setting postcssOptions to a new array of plugins. Specifying custom plugins overwrites all defaults postcss plugins including the NODE_ENV environment check.
What about Less?
We don't use it. Pull requests are welcome.
Browserslist
The CSS pipeline uses browserslist definiitons when available. We recommend including a .browserslistrc file in the project root. The task optimizes and minifies the resulting CSS when NODE_ENV is set to production.
Watch helper
Very frequently, gulp watch globs are identical to the source globs for a given task. To reduce repetition, the generated task includes a helper method which calls gulp.watch with default arguments. This makes writing watch tasks very concise and easy to maintain.
For Gulp logging to work correctly withe the watch helper method, the gulp instance must be passed to the create call.
const styles = require("@ideasonpurpose/gulp-task-styles").create({ gulp });
const watch = () => {
styles.watch(); // calls `gulp.watch(src, {cwd: srcOptions.cwd}, styles)`
};