Craig  Hicks
Craig Hicks

Reputation: 2548

gulp-preprocess not working as expected on a javascript file

I'm running a very simple preprocess task in gulp and its not working as expected.

Here's the task:

import preprocess from 'gulp-preprocess'


function testpp() {
    return gulp.src('./test')
        .pipe(preprocess())
        .pipe(gulp.dest('./tmp'))
}

The input file

 something

// @ifdef SOMEVAR

should be gone

// @endif

comes out unchanged. I expected the part 'should be gone' to get removed.

The gulp-preprocess documentation has this example:

// @ifdef DEBUG
someDebuggingCall();
// @endif

Any ideas what I am doing wrong?

$ npm -v
6.10.1
$ gulp -v
CLI version: 2.2.0
Local version: 4.0.2
$ node -v
v10.16.0

From package.json

  "devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "babel-core": "^6.26.3",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
    "eslint": "^6.1.0",
    "gulp": "^4.0.2",
    "gulp-babel": "^8.0.0-beta.2",
    "gulp-cli": "^2.2.0",
    "gulp-preprocess": "^3.0.2",
    "through2": "^3.0.1",
    "transfob": "^1.0.0"
  }

Upvotes: 0

Views: 259

Answers (1)

dgo
dgo

Reputation: 3937

I assume you've solved this by now, but on the off chance you haven't, or someone else has the same problem, I'll tell you what I can see.

Using your example:

function testpp() {
    return gulp.src('./test')
        .pipe(preprocess())
        .pipe(gulp.dest('./tmp'))
}

and

// @ifdef SOMEVAR

should be gone

// @endif

SOMEVAR will only be defined if it is defined in the context preprocess is working with. By default, I think it inherits the process object context, which includes your local machine environment. However, if you want to pass additional data to preprocess, you need to pass it a context object. For example:

 return gulp.src('./test')
        .pipe(preprocess({
          SOMEVAR:42
        })
       .pipe(gulp.dest('./tmp'))
 ));

Then SOMEVAR is defined within the scope of the current task, and should be gone won't show.

Upvotes: 0

Related Questions