grunt-ng-constant 不生成配置脚本
grunt-ng-constant is not producing config script
我正尝试在 AngularJS 中使用环境变量来进行特定于环境的配置。我正在使用使用 G运行t 的 Yeoman 工作流程,并且 grunt-ng-constant
插件据称可以帮助进行特定于环境的配置。按照这个 tutorial,我相应地设置了我的 G运行tfile,但是当我在控制台中 运行 grunt serve
时, config.js
没有写入 /app/scripts/
。没有 config.js
,我无法将环境变量注入 angular 应用程序。
这是我的 Gruntfile
的片段:
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt, {
useminPrepare: 'grunt-usemin',
ngtemplates: 'grunt-angular-templates',
cdnify: 'grunt-google-cdn'
});
// Configurable paths for the application
var appConfig = {
app: require('./bower.json').appPath || 'app',
dist: '../server/dist'
};
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-ng-constant');
// Define the configuration for all the tasks
grunt.initConfig({
// Project settings
yeoman: appConfig,
ngconstant: {
// options for all environments
options: {
space: ' ',
wrap: '"use strict";\n\n {%= __ngModule %}',
name: 'config'
},
// Development/Testing environment
development: {
options: {
dest: '<%= yeoman.app %>/scripts/config.js'
},
constants: {
ENV: {
name: 'development',
apiEndpoint: 'http://localhost:3000'
}
}
},
// Production environment
production: {
options: {
dest: '<%= yeoman.dist %>/scripts/config.js'
},
constants: {
ENV: {
name: 'production',
apiEndpoint: 'http://productionUrl'
}
}
}
},
...
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'wiredep',
'concurrent:server',
'autoprefixer:server',
'connect:livereload',
'watch',
'ngconstant:development'
]);
});
...
生成的是 /.sass-cache
和 /.tmp
在与 G运行t 文件相同的目录 (client
) 中。
我的应用文件结构:
ngconstant
任务未被调用,因为它在 watch
任务之后。修改 运行 块,使行 'ngconstant:development'
位于 'autoprefixer:server',
旁边,因此位于连接和监视任务之前。不要忘记添加逗号!
grunt.task.run([
'clean:server',
'wiredep',
'concurrent:server',
'autoprefixer:server',
'ngconstant:development',
'connect:livereload',
'watch'
]);
此外,bower.json 文件中的应用程序路径可能有误。可以肯定的是,通过更改 appConfig
修改应用程序的路径,使其看起来像这样:
var appConfig = {
app: 'app',
dist: '../server/dist'
}
我正尝试在 AngularJS 中使用环境变量来进行特定于环境的配置。我正在使用使用 G运行t 的 Yeoman 工作流程,并且 grunt-ng-constant
插件据称可以帮助进行特定于环境的配置。按照这个 tutorial,我相应地设置了我的 G运行tfile,但是当我在控制台中 运行 grunt serve
时, config.js
没有写入 /app/scripts/
。没有 config.js
,我无法将环境变量注入 angular 应用程序。
这是我的 Gruntfile
的片段:
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt, {
useminPrepare: 'grunt-usemin',
ngtemplates: 'grunt-angular-templates',
cdnify: 'grunt-google-cdn'
});
// Configurable paths for the application
var appConfig = {
app: require('./bower.json').appPath || 'app',
dist: '../server/dist'
};
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-ng-constant');
// Define the configuration for all the tasks
grunt.initConfig({
// Project settings
yeoman: appConfig,
ngconstant: {
// options for all environments
options: {
space: ' ',
wrap: '"use strict";\n\n {%= __ngModule %}',
name: 'config'
},
// Development/Testing environment
development: {
options: {
dest: '<%= yeoman.app %>/scripts/config.js'
},
constants: {
ENV: {
name: 'development',
apiEndpoint: 'http://localhost:3000'
}
}
},
// Production environment
production: {
options: {
dest: '<%= yeoman.dist %>/scripts/config.js'
},
constants: {
ENV: {
name: 'production',
apiEndpoint: 'http://productionUrl'
}
}
}
},
...
grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'wiredep',
'concurrent:server',
'autoprefixer:server',
'connect:livereload',
'watch',
'ngconstant:development'
]);
});
...
生成的是 /.sass-cache
和 /.tmp
在与 G运行t 文件相同的目录 (client
) 中。
我的应用文件结构:
ngconstant
任务未被调用,因为它在 watch
任务之后。修改 运行 块,使行 'ngconstant:development'
位于 'autoprefixer:server',
旁边,因此位于连接和监视任务之前。不要忘记添加逗号!
grunt.task.run([
'clean:server',
'wiredep',
'concurrent:server',
'autoprefixer:server',
'ngconstant:development',
'connect:livereload',
'watch'
]);
此外,bower.json 文件中的应用程序路径可能有误。可以肯定的是,通过更改 appConfig
修改应用程序的路径,使其看起来像这样:
var appConfig = {
app: 'app',
dist: '../server/dist'
}