Skip to content

Commit

Permalink
Move config into meta.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Sep 23, 2014
1 parent 2211b58 commit ecd38b6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 52 deletions.
1 change: 1 addition & 0 deletions blueprints/app/files/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">

{{BASE_TAG}}
{{CONFIG_META}}

<link rel="stylesheet" href="assets/vendor.css">
<link rel="stylesheet" href="assets/<%= name %>.css">
Expand Down
2 changes: 1 addition & 1 deletion blueprints/app/files/tests/helpers/start-app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import Application from '../../app';
import Router from '../../router';
import config from '../../config/environments/test';
import config from '../../config/environment';

export default function startApp(attrs) {
var App;
Expand Down
1 change: 1 addition & 0 deletions blueprints/app/files/tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">

{{BASE_TAG}}
{{CONFIG_META}}

<link rel="stylesheet" href="assets/vendor.css">
<link rel="stylesheet" href="assets/<%= name %>.css">
Expand Down
8 changes: 8 additions & 0 deletions lib/broccoli/app-suffix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* jshint ignore:start */

define('{{MODULE_PREFIX}}/config/environment', [], function() {
var metaName = '{{MODULE_PREFIX}}/config/environment';
var rawConfig = jQuery('meta[name="' + metaName + '"]').attr('content');
var config = JSON.parse(unescape(rawConfig));

return { 'default': config };
});

if (runningTests) {
require('{{MODULE_PREFIX}}/tests/test-helper');
} else {
Expand Down
13 changes: 0 additions & 13 deletions lib/broccoli/broccoli-config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ ConfigLoader.prototype.updateCache = function(srcDir, destDir) {
var outputDir = path.join(destDir, 'environments');
fs.mkdirSync(outputDir);

var defaultPath = path.join(destDir, 'environment.js');

if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}

var environments = [this.options.env];
if (this.options.tests) {
environments.push('test');
Expand All @@ -46,16 +40,9 @@ ConfigLoader.prototype.updateCache = function(srcDir, destDir) {
environments.forEach(function(env) {
var config = self.options.project.config(env);
var jsonString = JSON.stringify(config);
var moduleString = 'export default ' + jsonString + ';';
var outputPath = path.join(outputDir, env);


fs.writeFileSync(outputPath + '.js', moduleString, { encoding: 'utf8' });
fs.writeFileSync(outputPath + '.json', jsonString, { encoding: 'utf8' });

if (self.options.env === env) {
fs.writeFileSync(defaultPath, moduleString, { encoding: 'utf8' });
}
}, this);
};

Expand Down
61 changes: 33 additions & 28 deletions lib/broccoli/ember-app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global require, module */
/* global require, module, escape */
'use strict';

var fs = require('fs');
Expand Down Expand Up @@ -235,19 +235,29 @@ EmberApp.prototype.index = function() {
return configReplace(index, this._configTree(), {
configPath: path.join(this.name, 'config', 'environments', this.env + '.json'),
files: [ 'index.html' ],
patterns: [{
match: /\{\{EMBER_ENV\}\}/g,
replacement: calculateEmberENV
}, {
match: /\{\{BASE_TAG\}\}/g,
replacement: calculateBaseTag
}, {
match: /\{\{APP_CONFIG\}\}/g,
replacement: calculateAppConfig
}]
patterns: this._configReplacePatterns()
});
};

EmberApp.prototype._configReplacePatterns = function() {
return [{
match: /\{\{EMBER_ENV\}\}/g,
replacement: calculateEmberENV
}, {
match: /\{\{BASE_TAG\}\}/g,
replacement: calculateBaseTag
}, {
match: /\{\{APP_CONFIG\}\}/g,
replacement: calculateAppConfig
}, {
match: /\{\{CONFIG_META\}\}/g,
replacement: calculateConfigMeta
}, {
match: /\{\{MODULE_PREFIX\}\}/g,
replacement: calculateModulePrefix
}];
};

EmberApp.prototype.testIndex = function() {
var index = pickFiles(this.trees.tests, {
srcDir: '/',
Expand All @@ -259,13 +269,7 @@ EmberApp.prototype.testIndex = function() {
configPath: path.join(this.name, 'config', 'environments', 'test.json'),
files: [ 'tests/index.html' ],
env: 'test',
patterns: [{
match: /\{\{EMBER_ENV\}\}/g,
replacement: calculateEmberENV
}, {
match: /\{\{BASE_TAG\}\}/g,
replacement: calculateBaseTag
}]
patterns: this._configReplacePatterns()
});
};

Expand Down Expand Up @@ -444,16 +448,7 @@ EmberApp.prototype._processedEmberCLITree = function() {
configPath: path.join(this.name, 'config', 'environments', this.env + '.json'),
files: files,

patterns: [{
match: /\{\{EMBER_ENV\}\}/g,
replacement: calculateEmberENV
}, {
match: /\{\{APP_CONFIG\}\}/g,
replacement: calculateAppConfig
}, {
match: /\{\{MODULE_PREFIX\}\}/g,
replacement: calculateModulePrefix
}]
patterns: this._configReplacePatterns()
});

return this._cachedEmberCLITree = pickFiles(emberCLITree, {
Expand Down Expand Up @@ -528,6 +523,11 @@ EmberApp.prototype.javascript = function() {
var legacyFilesToAppend = this.legacyFilesToAppend;
var appOutputPath = this.options.outputPaths.app.js;

var modulePrefix = this.project.config(this.env).modulePrefix;
this.importWhitelist[modulePrefix + '/config/environment'] = [
'default'
];

var es6 = compileES6(applicationJs, {
loaderFile: 'vendor/ember-cli/loader.js',
ignoredModules: Object.keys(this.importWhitelist),
Expand Down Expand Up @@ -793,3 +793,8 @@ function calculateAppConfig(config) {
function calculateModulePrefix(config) {
return config.modulePrefix;
}

function calculateConfigMeta(config) {
return '<meta name="' + config.modulePrefix + '/config/environment" ' +
' content="' + escape(JSON.stringify(config)) + '">';
}
12 changes: 2 additions & 10 deletions tests/unit/broccoli/config-loader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ describe('broccoli/broccoli-config-loader', function() {
describe('clearConfigGeneratorCache', function() {
it('resets the cache', function() {
configLoader.updateCache(tmpSrcDir, tmpDestDir);
var originalConfig = fs.readFileSync(path.join(tmpDestDir, 'environment.js'), { encoding: 'utf8' });
var originalConfig = fs.readFileSync(path.join(tmpDestDir, 'environments', 'development.json'), { encoding: 'utf8' });

config.foo = 'blammo';
writeConfig(config);
configLoader.clearConfigGeneratorCache();

configLoader.updateCache(tmpSrcDir, tmpDestDir2);
var updatedConfig = fs.readFileSync(path.join(tmpDestDir2, 'environment.js'), { encoding: 'utf8' });
var updatedConfig = fs.readFileSync(path.join(tmpDestDir2, 'environments', 'development.json'), { encoding: 'utf8' });

assert.notEqual(originalConfig, updatedConfig);
assert(updatedConfig.match(/blammo/));
Expand All @@ -73,23 +73,15 @@ describe('broccoli/broccoli-config-loader', function() {
it('writes the current environments file', function() {
configLoader.updateCache(tmpSrcDir, tmpDestDir);

assert(fs.existsSync(path.join(tmpDestDir, 'environment.js')));
assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'development.js')));
assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'development.json')));

assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'test.js')));
assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'test.json')));
});

it('does not generate test environment files if testing is disabled', function() {
options.tests = false;
configLoader.updateCache(tmpSrcDir, tmpDestDir);

assert(fs.existsSync(path.join(tmpDestDir, 'environment.js')));
assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'development.js')));
assert(fs.existsSync(path.join(tmpDestDir, 'environments', 'development.json')));

assert(!fs.existsSync(path.join(tmpDestDir, 'environments', 'test.js')));
assert(!fs.existsSync(path.join(tmpDestDir, 'environments', 'test.json')));
});
});
Expand Down

0 comments on commit ecd38b6

Please sign in to comment.