Using Gulp, is it possible to add environment variables as a comment into CSS? (for example the package.json version)

喜夏-厌秋 提交于 2020-06-28 04:45:23

问题


Is it possible to pipe through specific environment variables in an SCSS file using a gulp setup?

Could you, for example, use something like:

/*! %version% */

Where version would be the version number as used in package.json.


回答1:


From gulp-header example:

// using data from package.json

var pkg = require('./package.json');

//var banner = ['/**',
//  ' * <%= pkg.name %> - <%= pkg.description %>',
//  ' * @version v<%= pkg.version %>',
//  ' * @link <%= pkg.homepage %>',
//  ' * @license <%= pkg.license %>',
//  ' */',
//  ''].join('\n');

// in your case all you need is:
var banner = ['/*! <%= pkg.version %> */\n'];

// in your task 
gulp.src('./foo/*.scss')
  .pipe(header(banner, { pkg : pkg } ))
  .pipe(sourcemaps.init())
  // if you are using sourcemaps, init them after injecting the header commment

  .pipe(sass().on("error", sass.logError))
  .pipe(sourcemaps.write("sourcemaps"))
  .pipe(gulp.dest('./dist/'))


来源:https://stackoverflow.com/questions/51722816/using-gulp-is-it-possible-to-add-environment-variables-as-a-comment-into-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!