【问题标题】:Read package.json in Ember application在 Ember 应用程序中读取 package.json
【发布时间】:2013-04-23 09:16:02
【问题描述】:

对于基于yeoman (grunt) 的Ember.js 项目,我们具有以下结构(仅显示了一些相关组件):

myapp/
├── app
├── component.json
├── package.json
├── Gruntfile.js
├── README.md
│   ├── adapters
│   ├── application.js
│   ├── components
│   ├── controllers
│   ├── generated
│   ├── images
│   ├── models
│   ├── routes
│   ├── scripts
│   ├── styles
│   ├── templates
│   └── views
├── dist
├── node_modules
└── test

我们需要在application.js 中访问package.jsoncomponent.json 中的一些值。我们想在部署过程中访问它(作为grunt 任务?)。这可能吗?如何实现?

【问题讨论】:

    标签: ember.js gruntjs yeoman


    【解决方案1】:

    您也可以使用像 https://github.com/outaTiME/grunt-replace 这样的即用型 grunt 包来完成此类任务,最常见的用例是例如在 grunt 运行期间提取版本号并用它标记您的 javascript 文件

    示例实现(尚未测试)

    在您的 Gruntfile.js 中:

    ...
    replace: {
        dist: {
            options: {
                variables: {
                    version: '<%= pkg.version %>'
                }
            },
            prefix: '@@',
            files: [
                {expand: true, flatten: true, src: ['app/application.js', 'app/README.md'], dest: 'dist/'}
            ]
        }
    }
    ...
    

    application.js

    ...
    var App = Framework.Application.create({
        VERSION: @@version,
    ...
    

    【讨论】:

      【解决方案2】:

      使用 grunt.file.readJSON('package.json') 从文件中读取 json 以执行 grunt 任务。

      【讨论】:

      • 这看起来很有趣。如何在 grunt 任务中将刚刚解析的json hash 传递给我的application.js
      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2018-02-06
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      相关资源
      最近更新 更多