Skip to content

add grunt replace package

kuang edited this page Mar 26, 2015 · 2 revisions

增加包 grunt-replace

背景

过程重点记录

第一次运行grunt replace:development的时候报错

Running "replace:development" (replace) task
Warning: Destination is not defined! If you want to overwrite files, then make sure to set overwrite: true. If you don't wish to overwrite, then make sure to set a destination Use --force to continue.

Aborted due to warnings.

把错误google,发现几乎是在描述一个叫grunt-text-replace的包,意识到应该是名字冲突了 修改Gruntfile.js

  grunt.loadNpmTasks('grunt-replace');
  grunt.renameTask('replace','gruntReplace');

就没事了

体会

  1. generator-env-config 这个包只是用来生成development.json、config.js这些文件,也可以手动去添加这些文件的
  2. 但也说明,有些命令可以帮你生成好项目的种子框架
  3. 参考文章中的yo也是要安装的 npm install yo -g
  4. 执行npm install grunt-replace --save-dev,会自动修改package.json

最后结果

到最后,还是决定把开发变量__DEV__加到window下就好了,不用angular的constant

目录结构如下

│  const.js
│
└─environments
        development.json
        production.json

const.js

!function(){
    window.__DEV__ = !!'@@dev';
}();

development.json

{
  "dev": 1
}

production.json

{
  "dev": ""
}

以后 if(__DEV__) 就说明是开发环境