2015-09-29 25 views
1

我有一個Gulp任務build:env,它爲我的AngularJS應用程序生成一個app.constant.js文件。但是,這些命令的結果與錯誤:模式定義中預期的替換屬性

/home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/src/applause.js:82 
     throw new Error('Replacement attribute expected in pattern definition. 
      ^
Error: Replacement attribute expected in pattern definition. 
    at /home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/src/applause.js:82:15 
    at /home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/node_modules/lodash/dist/lodash.js:2885:18 
    at forEach (/home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/node_modules/lodash/dist/lodash.js:3297:15) 
    at Function.transform (/home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/node_modules/lodash/dist/lodash.js:2884:35) 
    at normalize (/home/myuser/Documents/Projects/myapp/node_modules/gulp-replace-task/node_modules/applause/src/applause.js:43:12) 
    ...etc 

下面被使用,也從gulpfile.js控制檯的輸出,你可以看到的設置是否正確加載的所有文件。

.env.json

{ 
    "active": "dev" 
} 

environments.json

{ 
    "demo": { 
     "frontend": { 
      "host": "app.demo.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": null 
     }, 
     "backend": { 
      "host": "api.demo.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": { 
       "api": "api/" 
      } 
     }, 
     "socket": { 
      "host": "socket.demo.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": null 
     } 
    }, 
    "dev": { 
     "frontend": { 
      "host": "app.dev.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": null 
     }, 
     "backend": { 
      "host": "api.dev.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": { 
       "api": "api/" 
      } 
     }, 
     "socket": { 
      "host": "socket.dev.example.org", 
      "protocol": "https", 
      "port": "443", 
      "endpoint": null 
     } 
    } 
} 

gulpfile.js(任務編譯:ENV)

gulp.task('build:env', function() { 
    var env = JSON.parse(fs.readFileSync('./.env.json', 'utf8')); 
    var environments = JSON.parse(fs.readFileSync('./.environments.json', 'utf8')); 
    var settings = environments[env.active]; 
    console.log(settings); 

    // Replace each placeholder with the correct value for the variable. 
    return gulp.src('./config/app.constant.js') 
    .pipe(replace({ 
     patterns: [ 
      { 
       match: 'api', 
       replacement: settings.backend.host 
      }, 
      { 
       match: 'assets', 
       replacement: settings.frontend.host 
      } 
     ] 
    })) 
    .pipe(gulp.dest('./www/app')); 
}); 

配置/ app.constant.js

(function() { 
    'use strict'; 
    angular.module('myapp').constant('settings', { 
    'API': '@@api', 
    'ASSETS': '@@assets' 
    }); 
}()); 

的console.log(設置)

{ 
    frontend:{ 
     host:'192.168.1.112', 
     protocol:'http', 
     port:'8100', 
     endpoint:null 
    }, 
    backend:{ 
     host:'192.168.1.112', 
     protocol:'http', 
     port:'80', 
     endpoint:{ 
    api:'api/' 
     } 
    }, 
    socket:{ 
     host:'192.168.1.112', 
     protocol:'http', 
     port:'3001', 
     endpoint:null 
    } 
} 

離子/的NodeJS /科爾多瓦/咕嘟咕嘟

[email protected]:~/Documents/Projects/frontend$ ionic info 

Your system information: 

Cordova CLI: 5.3.3 
Gulp version: CLI version 3.8.11 
Gulp local: Local version 3.9.0 
Ionic Version: 1.1.0 
Ionic CLI Version: 1.7.7 
Ionic App Lib Version: 0.6.3 
OS: Distributor ID:  Ubuntu Description:  Ubuntu 15.04 
Node Version: v0.12.7 

列表的依賴關係

[email protected]:~/Documents/Projects/frontend$ npm list --depth 0 
[email protected] /home/myuser/Documents/Projects/frontend 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
├── [email protected] 
└── [email protected] 
+0

遇到同樣的問題,你有沒有找到一個解決方案? –

+0

還沒有不幸 –

回答

1

我通過使用exports.module

實施例解決礦

module.exports = function() { 
var config = { 
    beta: { 
     "CssPath": "dist/css/", 
     "OidcConfig": { 
      "authority": "https://identity/identity/", 
      "persistKey": "token", 
      "client_id": "test", 
      "redirect_uri": "http://beta/#/callback/", 
      "scope": "openid profile email roles WebApiResource" 
     }, 
     "JSNLogSettings": { 
      "url": "http://beta/jsnlog.logger", 
      "consoleLogging": true 

     }, 
     "BaseWebApiUrl": "http://prod/" 

    }, 
    production: { 
     "CssPath": "bower_components/templates/dist/css/", 
     "OidcConfig": { 
      "authority": "https://identity", 
      "persistKey": "token", 
      "client_id": "test", 
      "redirect_uri": "http://test/#/callback/", 
      "scope": "openid profile email roles WebApiResource" 
     }, 
     "JSNLogSettings": { 
      "url": "http://test/jsnlog.logger", 
      "consoleLogging": true 

     }, 
     "BaseWebApiUrl": "http://test" 

    } 
}; 

return config; 
}; 

IN GULP DID

buildconfig = require('./build.config')(); 

gulp.task('replace', function() { 
var build = JSON.stringify(buildconfig['production']); 

return gulp.src('src/app.module.js') 
    .pipe(replacetask({ 
     usePrefix: false, 
     patterns: [ 
      { 
       match: '=constants', 
       replacement: "=" + build 
      } 
     ] 
    })) 
    .pipe(gulp.dest(config.build)); 
}); 

角模塊

(function() { 
'use strict'; 

window.BuildVars =constants; 

angular 
    .module('app', [ 
     'app.views', 
     'app.services', 
     'app.directives', 
     'angular.filter' 
    ]); 

})(); 

我知道我使用窗口而不是常量,但這對我來說稍微容易一些,但是總是可以創建一個常量到引用窗口。{VarName}以配合角度。剛纔我所目前正在

編輯

不使用窗口。

AngularJS

(function() { 
    'use strict'; 
    angular.module('myapp').constant('settings', { 
     'API': @@buildConstants 
    }); 
}()); 

GULP

gulp.task('replace', function() { 
    var build = JSON.stringify(buildconfig['production']); 

    return gulp.src('src/app.module.js') 
     .pipe(replacetask({ 

      patterns: [ 
       { 
        match: 'buildConstants', 
        replacement: build 
       } 
      ] 
     })) 
     .pipe(gulp.dest(config.build)); 
}); 

你咕嘟咕嘟想更像

gulp.task('build:env', function() { 
    var env = JSON.parse(fs.readFileSync('./.env.json', 'utf8')); 
    var environments = JSON.parse(fs.readFileSync('./.environments.json', 'utf8')); 
    var settings = environments[env.active]; 
    console.log(settings); 

    var backend = JSON.stringify(settings.backend.host); 
    var frontend = JSON.stringify(settings.frontend.host); 

    // Replace each placeholder with the correct value for the variable. 
    return gulp.src('./config/app.constant.js') 
     .pipe(replace({ 
      patterns: [ 
       { 
        match: 'api', 
        replacement: backend 
       }, 
       { 
        match: 'assets', 
        replacement: frontend 
       } 
      ] 
     })) 
     .pipe(gulp.dest('./www/app')); 
}); 

通過解析JSON的更換不能插入對象。回到JSON字符串,應該工作..

+0

感謝您的解釋和解決方案。我upvoted它,但我不能接受它作爲一個答案,因爲我仍然在尋找解決方案,而不使用'窗口' –

+0

讓我知道如果這樣做,我注意到我正在做一個完整的對象,你不是,但是,如果這不能解決問題,我會重新創建對象 –

+0

使用'JSON.stringify()'時仍然會出現相同的錯誤。 –