6

私にはいくつかのうなり声のタスクがあり、それらのタスク間でグローバル変数を共有しようとしていますが、問題が発生しています。

ビルドタイプに応じて適切な出力パスを設定するカスタムタスクをいくつか作成しました。これは正しく設定されているようです。

// Set Mode (local or build)
grunt.registerTask("setBuildType", "Set the build type. Either build or local", function (val) {
  // grunt.log.writeln(val + " :setBuildType val");
  global.buildType = val;
});

// SetOutput location
grunt.registerTask("setOutput", "Set the output folder for the build.", function () {
  if (global.buildType === "tfs") {
    global.outputPath = MACHINE_PATH;
  }
  if (global.buildType === "local") {
    global.outputPath = LOCAL_PATH;
  }
  if (global.buildType === "release") {
    global.outputPath = RELEASE_PATH;
  }
  if (grunt.option("target")) {
    global.outputPath = grunt.option("target");
  }
  grunt.log.writeln("Output folder: " + global.outputPath);
});

grunt.registerTask("globalReadout", function () {
  grunt.log.writeln(global.outputPath);
});

そのため、後続のタスクでglobal.outputPathを参照しようとすると、エラーが発生します。

grunt testコマンドラインから呼び出すと、問題なく正しいパスが出力されます。

ただし、次のようなタスクがある場合:clean:{release:{src:global.outputPath}}

次のエラーがスローされます。 Warning: Cannot call method 'indexOf' of undefined Use --force to continue.

また、setOutputタスクの定数は、Gruntfile.jsの先頭に設定されています。

何かご意見は?私はここで何か間違ったことをしていますか?

4

3 に答える 3

13

だから、私は正しい道を進んでいました。問題は、これらのグローバル変数が設定される前にモジュールがエクスポートされるため、initConfig()タスク内で定義された後続のタスクではすべて未定義になることです。

私が思いついた解決策は、もっと良いかもしれませんが、grunt.option値を上書きすることです。

タスクにオプションのオプションがあります--target

実用的なソリューションは次のようになります。

grunt.registerTask("setOutput", "Set the output folder for the build.", function () {
  if (global.buildType === "tfs") {
    global.outputPath = MACHINE_PATH;
  }
  if (global.buildType === "local") {
    global.outputPath = LOCAL_PATH;
  }
  if (global.buildType === "release") {
    global.outputPath = RELEASE_PATH;
  }
  if (grunt.option("target")) {
    global.outputPath = grunt.option("target");
  }

  grunt.option("target", global.outputPath);
  grunt.log.writeln("Output path: " + grunt.option("target"));
});

そして、initConfig()で定義されたタスクは次のようになりました。

clean: {
  build: {
    src: ["<%= grunt.option(\"target\") %>"]
  }
}

より良い解決策があれば、遠慮なくチャイムを鳴らしてください。そうでなければ、おそらくこれは他の誰かを助けるかもしれません。

于 2013-03-27T15:54:39.793 に答える
4

これを行う方法があり、のような値を使用して出力パスを指定できます--dev。これまでのところ、それは非常にうまく機能しています、私はそれがとても好きです。他の誰かもそれを好きかもしれないので、私はそれを共有したいと思いました。

    # Enum for target switching behavior
    TARGETS =
      dev: 'dev'
      dist: 'dist'

    # Configurable paths and globs
    buildConfig =
      dist: "dist"
      dev: '.devServer'
      timestamp: grunt.template.today('mm-dd_HHMM')

    grunt.initConfig
        cfg: buildConfig
        cssmin:
            crunch:
                options: report: 'min'
                files: "<%= grunt.option('target') %>/all-min.css": "/**/*.css"

    # Set the output path for built files.
    # Most tasks will key off this so it is a prerequisite
    setPath = ->
      if grunt.option 'dev'
        grunt.option 'target', buildConfig.dev
      else if grunt.option 'dist'
        grunt.option 'target', "#{buildConfig.dist}/#{buildConfig.timestamp}"
      else # Default path
        grunt.option 'target', buildConfig.dev
      grunt.log.writeln "Output path set to: `#{grunt.option 'target'}`"
      grunt.log.writeln "Possible targets:"
      grunt.log.writeln target for target of TARGETS

    setPath()

この設定では、次のようなコマンドを実行できます。

grunt cssmin --dist #sent to dist target
grunt cssmin --dev #sent to dev target
grunt cssmin --dev #sent to default target (dev)
于 2013-08-23T17:33:03.923 に答える
0

これは古い質問です、私はちょうど私の5セントを投入しようと思いました。

任意のタスクから構成変数にアクセスできるようにする必要がある場合は、メイン(常にロードするもの)の構成ファイルで次のように定義するだけです。

module.exports = function(grunt)
{
    //
    // Common project configuration
    //
    var config = 
    {
        pkg: grunt.file.readJSON('package.json'),

        options: // for 'project'
        {
            dist:
            {
                outputPath: '<%= process.cwd() %>/lib',
            },
            dev:
            {
                outputPath: '<%= process.cwd() %>/build',
            },
        },
    }

    grunt.config.merge( config )
}

次に、次のように値にアクセスできます。

  • 設定ファイル内

... my_thingie: [ ends_up_here: '<%= options.dev.outputPath %>/bundle', ], ...

  • タスクで

// as raw value grunt.config.data.options.dist.outputPath // after (eventual) templates have been processed grunt.config('options.dist.outputPath')

ここでは慣例に沿ってキーを使用しましたoptionsが、名前付きのタスクやキーに使用したものを登録しないことを覚えている限り、何でも使用でき'options'ます:)

于 2017-06-01T04:21:05.480 に答える