简体   繁体   English

如何将变量值从GULP注入到JS文件中

[英]How to inject variable value into JS file from GULP

Hopefully this is a quick question. 希望这是一个快速的问题。

What I'm trying to do is to add a timestamp into a Javascript object in a JS file while the file is being built with GULP. 我正在尝试做的是在使用GULP构建文件时将时间戳添加到JS文件中的Javascript对象中。 Basically, in "file.js", I have an object where I would like to have object.timeStamp that equals the time of the GULP build. 基本上,在“file.js”中,我有一个对象,我希望object.timeStamp等于GULP构建的时间。

I am currently adding a timestamp to the top of the file using gulp-header, but I have been asked to add the timestamp to a property in the object. 我目前正在使用gulp-header向文件顶部添加时间戳,但我被要求将时间戳添加到对象中的属性。

My thought was to inject the value from GULP, but all of the injection plugins I have found so far are for injecting contents of one file into the target file. 我的想法是从GULP注入值,但到目前为止我发现的所有注入插件都是将一个文件的内容注入目标文件。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Are you using any kind of modules? 你在使用任何一种模块吗? ES6 modules, AMD, CommonJS, ...? ES6模块,AMD,CommonJS,......?

If so, you can generate a config module with Gulp where you can inject any variable you want. 如果是这样,您可以使用Gulp 生成 配置模块,您可以在其中注入所需的任何变量。 It would look something like this: 它看起来像这样:

config.tmpl.js config.tmpl.js

module.exports = <%= config %>

config gulp task 配置gulp任务

var gulp = require('gulp');
var template = require('gulp-template');
var rename = require('gulp-rename');

gulp.task('config', function() {
  return gulp.src('path/to/config.tmpl.js')
    .pipe(template({config: JSON.stringify({
      timeStamp: new Date()
    })}))
    .pipe(rename('config.js'))
    .pipe(gulp.dest('path/to/config.js'));
});

and finally, in your JS file 最后, 在你的JS文件中

var config = require('path/to/config.js');

var object = {
  timeStamp: config.timeStamp
}

OK, so I figured out a workaround that gets me where I want to be. 好的,所以我想出了一个解决方法,让我想到的地方。 The first this is using the gulp-header to insert a variable declaration into my Javascript file using this code: 第一个是使用gulp-header使用以下代码将变量声明插入到我的Javascript文件中:

In GULP: 在GULP中:

    var d = new Date();
    .pipe(header("var timeStamp = '" + d +"';"))

Then, in my Javascript file, I set up a property in the object that is a function, and sets the timeStamp property I was looking for by getting the value from the timeStamp variable injected above using the gulp-header, like this: 然后,在我的Javascript文件中,我在作为函数的对象中设置了一个属性,并通过使用gulp-header从上面注入的timeStamp变量获取值来设置我正在寻找的timeStamp属性,如下所示:

In the JS File, the header functionality inserts this: 在JS文件中,标题功能插入:

    var timeStamp = 'Tue Dec 08 2015 15:15:11 GMT-0800 (PST)';

And then my function, in the JS Object is simply this: 然后我的函数,在JS对象中就是这样:

   ts: function(){
    object.timeStamp = timeStamp;
   },

Which, when called, runs and sets the timestamp inside of the object. 在调用时,它会运行并设置对象内部的时间戳。

It seems like there should be an easier way to do this, but for now, this is working. 似乎应该有一种更简单的方法来做到这一点,但是现在,这是有效的。

If you have any ideas on making it better, I would love to hear them! 如果你对改进它有任何想法,我很乐意听到它们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM