2017-03-08 6 views
1

ビルド中に環境変数の値にアクセスして、文字列を置き換える方法を教えてください。ビルド中に環境変数にアクセスする方法

例:

aurelia_project /環境/ dev.ts

export default { 
    debug: true, 
    testing: true, 
    pageBaseUrl: 'https://localhost:9000' // <-- This is the info I'd like to fetch 
}; 

aurelia_project /環境/ prod.ts

export default { 
    debug: true, 
    testing: true, 
    pageBaseUrl: 'https://www.foobar.com' // <-- This is the info I'd like to fetch 
}; 

index.htmlを

<!DOCTYPE html> 
<html> 
<head> 
    <!-- ... --> 
    <base href="{pageBasePath}"> 
    <!-- ... --> 
</head> 
<!-- ... --> 
</html> 

aurelia_project /タスク/ processIndex.ts

// ... 
export default function processIndex() { 
    const pageBasePath = CLI.getEnvParamValue('pageBasePath'); 

    return gulp.src('index.html') 
     .pipe(replace('{pageBasePath}', pageBasePath)) 
     .pipe(gulp.dest(project.platform.outputIndex)); 
} 

があり、いくつかのprocessIndex.tsに同等の私の架空のCLI.getEnvParamValue('pageBasePath');に組み込みまたは私は手動でaurelia_project/environments内の適切なファイルからそれらに関する情報を読み取るために持っています(CLIOptions.getEnvironment()を使用して)?

答えて

1

私は手動で適切な環境ファイルから必要な情報を解析することによってこの問題を解決するために管理してきました:これはかなりハック感じているので

let project = require('../aurelia.json'); 
import * as replace from 'gulp-replace'; 
import * as gulp from 'gulp'; 
import {CLIOptions} from 'aurelia-cli'; 
import * as fs from 'fs'; 

export default function processIndex() { 
    const env = CLIOptions.getEnvironment(); 
    const envContent = fs.readFileSync(`aurelia_project/environments/${env}.ts`, 'utf8'); 
    const pageBaseUrl = /pageBaseUrl: '(.*)'/ig.exec(envContent)[1]; 

    return gulp.src('index.html') 
     .pipe(replace('{pageBaseUrl}', pageBaseUrl)) 
     .pipe(gulp.dest(project.platform.outputIndex)); 
} 

が、私はまだ良い代替手段をいただければと思います!

+0

私は、環境をgulpビルドタスクにインポートすることに失敗したことを知っていました。おい、これは私を救った...あなたがコードを考え出してうれしい! – chdev77

関連する問題