I've edited a simple Gulp pipeline (that i've tested and works outside the IDE) and i'm now trying to include it into PHPStorm Project in order to achieve a better css workflow in a real project.
This is the script, (it's a simple css optimization pipeline):
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
del = require('del');
// Styles
gulp.task('styles', function() {
return sass('../scss/base.scss', { style: 'expanded' })
.pipe(autoprefixer('> 1%, last 2 versions, Firefox ESR, Opera 12.1'))
.pipe(minifycss())
.pipe(gulp.dest('../css'))
.pipe(notify({ message: 'Styles task complete' }));
});
// Clean
gulp.task('clean', function(cb) {
del(['css/base.css'], cb)
});
// Default task
gulp.task('default', ['clean'], function() {
gulp.start('styles');
});
The annoying problem i cannot solve is related to the module sass that is the missing a proper ruby environment initialization, needed in order to run the command.
sass = require('gulp-ruby-sass') ->
'sass' not recognized as an internal or external command.
As i said before, I managed to run this gulp script using an external shell link from the ruby installer:
Start Command Prompt with Ruby
C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby21\bin\setrbvars.bat
Do you have any clue on how to run gulp scripts that needs ruby inside PhpStorm?
Thanks in advance,
Please make sure that Ruby is in your system PATH - in Control Panel, go to System properties, Advanced, press Environment variables, in System variables section select PATH and append C:\Ruby21\bin to it. Make sure to restart PHPStorm after changing PATH
Related
I'm using this plugin for years now, and it's the first time I get this error.
I'm working on an old project which i recently upgraded to the latests versions of node & npm, so I'm working with node v16.15.0 and npm v8.5.5. I also upgraded all of my npm packages to their latests versions (yes, I like to live dangerously).
Gulp v4.0.2 with gulpfile.js as such (there is some commented code in it because I'm still in the process of making it work with these versions of node & npm. Originally, this project was running fine with node v12.6.0 and npm v7.20.03 .
After upgrading, I encountered many errors, as for example the need to switch from node-sass to dart-sass, because it's deprecated. ) :
// MODULES
// ----------------------------------------------------------------------------
const gulp = require('gulp'),
//sass = require('gulp-sass')(require('dart-sass'), require('node-sass')),
sass = require('gulp-sass')(require('sass'), require('dart-sass')),
globImporter = require('node-sass-glob-importer'),
postcss = require('gulp-postcss'),
stylelint = require('gulp-stylelint'),
svgstore = require('gulp-svgstore'),
rename = require('gulp-rename'),
cssnano = require('cssnano'),
uglifyjs = require('uglify-js'),
del = require('del'),
autoprefixer = require('autoprefixer'),
// This is based on node-sass, which is deprecated
// find a replacement which works with something else...
//inliner = require('sass-inline-svg'),
connect = require('gulp-connect'),
openBrowser = require('open');
// Basic config for paths (cfg = config)
// ----------------------------------------------------------------------------
const cfg = {
scssDir: 'src/scss/',
builtCssDir: 'dist/css/',
scssPattern: '**/*.scss',
svgDir: 'src/assets/svg/',
compiledSvgDir: 'dist/svg/',
compiledSvgFileName: 'symbols.twig',
svgPattern: '**/*.svg',
jsLibsDir: 'src/js/libs/',
jsDir: 'src/js/',
compiledJsLibsDir: 'dist/js/libs/',
compiledJsDir: 'dist/js/',
jsPattern: '*.js'
}
// Launch a server
// ----------------------------------------------------------------------------
function serve() {
connect.server({
port: 8080,
livereload: true,
root: ['src']
});
}
// Open application in browser
// ----------------------------------------------------------------------------
async function open() {
await openBrowser('http://localhost:8080/');
}
// Construct style.css (Combine all scss files into one css final file)
// ----------------------------------------------------------------------------
function style() {
return gulp
.src(cfg.scssDir+cfg.scssPattern)
// Test files with `gulp-stylelint` to ckeck the coding style
/*.pipe(stylelint({
reporters: [
{formatter: 'compact', console: true}
]
}))*/
/*.pipe(sass({
importer: globImporter(),
functions: {
svg: inliner(cfg.svgDir, {encodingFormat: 'uri'})
}
}))*/
.on('error', sass.logError)
// Use postcss with autoprefixer and compress the compiled file using cssnano
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(gulp.dest(cfg.builtCssDir))
}
// Construct svg symbol file (Combine svg files into one with <symbol> elements)
// ----------------------------------------------------------------------------
/*function svg() {
return gulp
.src(cfg.svgDir+cfg.svgPattern, {base: cfg.svgDir})
.pipe(rename((filePath) => {
const name = filePath.dirname !== '.' ? filePath.dirname.split(filePath.sep) : []
name.push(filePath.basename)
filePath.basename = `symbol-${name.join('-')}`
}))
.pipe(svgstore({ inlineSvg: true }))
.pipe(rename(cfg.compiledSvgFileName))
.pipe(gulp.dest(cfg.compiledSvgDir));
}*/
// Catch JS libs and transfer it to dist folder
// ----------------------------------------------------------------------------
async function jslibs() {
return gulp
.src(cfg.jsLibsDir+cfg.jsPattern)
.pipe(gulp.dest(cfg.compiledJsLibsDir));
}
// Compile JS
// ----------------------------------------------------------------------------
async function js() {
return gulp
.src(cfg.jsDir+cfg.jsPattern)
.pipe(gulp.dest(cfg.compiledJsDir));
}
// Watcher
// ----------------------------------------------------------------------------
function watch() {
gulp.watch('../'+cfg.scssDir+cfg.scssPattern, style)
gulp.watch('../'+cfg.scssDir+cfg.scssPattern)
gulp.watch(cfg.scssDir+cfg.scssPattern, style)
gulp.watch(cfg.svgDir+cfg.svgPattern, svg)
gulp.watch(cfg.scssDir+cfg.scssPattern);
}
// Empty the build folder of its front asset like css & svg (We're not emptying it totally, because there is other assets in it)
function clean() {
return del([
cfg.builtCssDir,
cfg.compiledSvgDir+'/'+cfg.compiledSvgFileName,
cfg.compiledJsDir
], {force: true});
}
clean.description = 'Delete the content of the build folder.';
// Serve
// ----------------------------------------------------------------------------
// Launch server
const launch = gulp.series(open, serve);
// Builder
// ----------------------------------------------------------------------------
// Regenerate the build folder.
const build = gulp.series(clean, style, jslibs, js);
// Export default task
// ----------------------------------------------------------------------------
// Regenerate the build folder & launch watcher
const defaultTask = gulp.series(clean, style, jslibs, js, watch);
// Export Tasks
// ----------------------------------------------------------------------------
module.exports = {
open,
serve,
launch,
clean,
style,
jslibs,
js,
build,
watch,
default: defaultTask
};
I get the following error when I try to throw my gulp default task in my terminal :
error
[11:42:55] CssSyntaxError in plugin "gulp-postcss"
Message:
/Users/emma/Desktop/www/photosbroth/src/scss/styles.scss:3:1: Unknown word
You tried to parse SCSS with the standard CSS parser; try again with the postcss-scss parser
1 | #charset "UTF-8";
2 |
> 3 | // Import EXTERNAL libs
| ^
4 | // - `sass-mq` for breakpoints management
5 | #import '../../node_modules/sass-mq/mq';
Apparently, it doesn't like the // scss comments, which were perfectly fine before I tried to upgrade my stack.
Could anyone help please ? I wasn't able to find a solution to this.
I tried this in the gulpfile :
postcss = require('gulp-postcss')(require('postcss'))
this
postcss = require('gulp-postcss')(require('postcss-scss'))
and this
postcss = require('gulp-postcss')(require('postcss'), require('postcss-scss'))
None of those expressions worked.
Is this a bug or anything else ? Am I missing something ?
Thank you.
I'm using gulp to compile my scss files. Everything worked until I got a new laptop, and now the nested variables aren't compiling.
For example, none of these colors compile:
$theme-colors: (
primary: #002d72,
secondary: #53565a,
purple: #4e2984,
light-purple: rgba(78, 41, 132, .8),
light-blue: #69b3e7,
error: #d72f09
);
Example of how a "theme color" would be called:
h3 {
color: theme-color("primary");
}
I had to re-install npm and gulp, so I am obviously using different versions of these two packages then I was previously.
Here are the versions:
npm: 8.5.0
gulp: CLI version: 2.3.0
Local version: 4.0.2
node: v16.14.2
My gulpfile looks like this (it handles scss and js files):
/* file: gulpfile.js */
var gulp = require('gulp');
var sass = require('gulp-sass')(require('node-sass'));
const minify = require('gulp-minify');
var concat = require('gulp-concat');
gulp.task('sass', function(){
return gulp.src('resources/assets/styles/**/*.scss')
.pipe(sass()) // Using gulp-sass
.pipe(gulp.dest('dist/styles'))
.pipe(minify({
ext: '.css',
mangle: false,
noSource: true
}))
});
gulp.task('compress', function() {
return gulp.src('resources/assets/scripts/*.js')
.pipe(concat('main.js'))
.pipe(minify({
ext: '.js',
mangle: false,
noSource: true
}))
.pipe(gulp.dest('dist/scripts'));
});
gulp.task('watch', function() {
gulp.watch('resources/assets/styles/**/*.scss', gulp.series('sass'));
gulp.watch('resources/assets/scripts/**/*.js', gulp.series('compress'));
});
Note: I added require('node-sass') to get gulp to work on the new computer.
If anyone has an idea of what's wrong here, it would be a huge help!
thanks!
Jill
The issue was caused by a discrepancy in the bootstrap node module versions. When the site was first built, it installed bootstrap version 4.3.1. After getting a new laptop and re-installing all the node modules, bootstrap 5.1 was installed.
Not exactly sure why bootstrap would affect scss variables, so if YOU know feel free to enlighten me.
To update the bootstrap version number, navigate to the directory containing the node_modules and run, npm install bootstrap#<version-number> --save
I know this has been asked many times before, but none of the answers helped me to solve my problem migrating gulp 3 to 4. We didn't necessarily have to upgrade to version 4 of gulp, but updating Node.js from 10 to 12 forced us to do so, since Node.js 12 doesn't support gulp 3 anymore. Here are just 2 of files in our build process, I think that it should be enough to understand what the problem is from these files alone, but I can add the other files if need be. And I have also removed the contents of most functions for brevity.
// gulpfile.js
'use strict';
var gulp = require('gulp');
var wrench = require('wrench');
/**
* This will load all js or coffee files in the gulp directory
* in order to load all gulp tasks
*/
wrench.readdirSyncRecursive('./gulp').filter(function (file) {
return (/\.(js|coffee)$/i).test(file);
}).map(function (file) {
require('./gulp/' + file);
});
/**
* Default task clean temporaries directories and launch the
* main optimization build task
*/
//gulp.task('default', ['clean'], function () { <-- Original line, worked in gulp 3.9
function main(done)
{
gulp.start(build);
done();
}
exports.default = gulp.series(clean, main);
And another file:
// build-dev.js
'use strict';
var path = require('path');
var gulp = require('gulp');
var conf = require('./conf');
var $ = require('gulp-load-plugins')({
pattern: ['gulp-*', 'main-bower-files', 'uglify-save-license', 'del']
});
//gulp.task('html-dev', ['inject'], function () <-- Original line, worked in gulp 3.9
function htmlDev()
{
// Removed for brevity...
}
exports.htmlDev = gulp.series(exports.inject, htmlDev); <-- this is the line that fails
//gulp.task('fonts-dev', function () <-- Original line, worked in gulp 3.9
function fontsDev()
{
// Removed for brevity...
}
exports.fontsDev = fontsDev;
//gulp.task('other-dev', function () <-- Original line, worked in gulp 3.9
function otherDev()
{
// Removed for brevity...
}
exports.otherDev = otherDev;
//gulp.task('clean', function () <-- Original line, worked in gulp 3.9
function clean()
{
// Removed for brevity...
}
exports.clean = clean;
//gulp.task('build:dev', ['html-dev', 'fonts-dev', 'other-dev']); <-- Original line, worked in gulp 3.9
exports.buildDev = gulp.series(exports.htmlDev, fontsDev, otherDev);
And when I run gulp I get the following error:
AssertionError [ERR_ASSERTION]: Task never defined: undefined
at getFunction (F:\Dev\DigitalRural\Main\Mchp.DigitalRural.Portal\node_modules\undertaker\lib\helpers\normalizeArgs.js:15:5)
at map (F:\Dev\DigitalRural\Main\Mchp.DigitalRural.Portal\node_modules\arr-map\index.js:20:14)
at normalizeArgs (F:\Dev\DigitalRural\Main\Mchp.DigitalRural.Portal\node_modules\undertaker\lib\helpers\normalizeArgs.js:22:10)
at Gulp.series (F:\Dev\DigitalRural\Main\Mchp.DigitalRural.Portal\node_modules\undertaker\lib\series.js:13:14)
at Object.<anonymous> (F:\Dev\DigitalRural\Main\Mchp.DigitalRural.Portal\gulp\build-dev.js:61:24)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Module.require (internal/modules/cjs/loader.js:830:19) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '=='
}
The error is in the second file, build-dev.js, and I indicate it in the code I provided. I have been trying to follow tutorials and SO questions, but to no avail. What gives?
OK, I apparently got it all wrong (yup, makes sense, from a guy that doesn't know either gulp 3 nor gulp 4 :)).
Since gulp 4 has some quite substantial changes, I had to actually rewrite the whole process (not the tasks themselves, they are more or less fine, except some here and there).
So basically, I changed the tasks to functions, used exports for some tasks to make them well known, and used series/parallel for the tasks' flow.
But I have another problem, related to the destination path, but that's a topic for another post.
Thanks everyone.
When TFS "get latest" pulls in a package.json change that adds a dev dependency, Visual Studio 2017 doesn't automatically run "npm install". This breaks any gulp tasks in "watch" mode that depend on the new package.
The only way I can see to cause an npm install is to manually touch package.json or restart VS.
Is there a way to trigger "npm install" on "get latest"?
Here is a wireframe of the solution I settled on. tl;dr - implement my own package.json watcher in Gulp to stop Webpack, run npm install, and restart Webpack.
const gulp = require('gulp');
const install = require('gulp-install');
const watch = require('glob-watcher');
const webpack = require('webpack');
gulp.task('default', ['webpack']);
gulp.task('webpack', function () {
// Start the initial Webpack build.
let webpackCompiler = webpack({
// ...
watch: true
// ...
});
// Set up a watcher to kill Webpack, run 'npm install', and restart Webpack when package.json changes.
let packageWatcher = watch('./package.json', { events: ['change'] }, () => {
packageWatcher.close();
console.log('Stopped package.json watcher.');
webpackWatcher.close(() => {
console.log('Stopped Webpack watcher.');
gulp.start('npm-install-then-webpack');
});
});
console.log('Started package.json watcher.');
});
gulp.task('npm-install', function (callback) {
gulp.src('./package.json')
.pipe(install(callback));
});
gulp.task('npm-install-then-webpack', ['npm-install'], function () {
gulp.start('webpack');
});
We are using gulp to compile all our LESS files into the target/ of a Maven project. This task alone takes ~51secs, so we would like to speed it up and skip unchanged LESS files. We need a file cache because gulp is called from Maven and the build runs inside an IDE, so the gulp process cannot stay in memory.
At best, the cached CSS files should be copied to target/ even if /target was deleted by a Clean & Build.
Here's my code:
var cache = require('gulp-cache');
var fileCache = new cache.Cache({ cacheDirName: 'gulp-cache' });
gulp.task('less', function () {
return gulp.src([webappPath + '/**/*.less'])
.pipe(fileCache('less'))
.pipe(sourcemaps.init())
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')],
plugins: [cleancss],
relativeUrls: true
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(target));
;
});
The line .pipe(fileCache('less')) runs into an error:
TypeError: fileCache is not a function.
(Documentation at https://www.npmjs.com/package/gulp-cache )
(1) The gulp-cache plugin needs to wrap your less plugin. That way only files that have changed will be passed through to less.
(2) You don't necessarily need to instantiate your own cache.Cache object. gulp-cache will create one for you if you don't. You only need to do it yourself if you want to have multiple caches. In that case you can pass the cache.Cache object using the fileCache option.
gulp.task('less', function () {
return gulp.src([webappPath + '/**/*.less'])
.pipe(sourcemaps.init())
.pipe(cache(less({
paths: [path.join(__dirname, 'less', 'includes')],
plugins: [cleancss],
relativeUrls: true
}), {
fileCache: new cache.Cache({ cacheDirName: 'gulp-cache' })
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(target));
});