Skip to content

Commit

Permalink
Fixes #318 - Make configuration a singleton object shared by all mark…
Browse files Browse the repository at this point in the history
…o instances
  • Loading branch information
patrick-steele-idem committed Jun 30, 2016
1 parent c26614f commit cd79732
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 28 deletions.
66 changes: 38 additions & 28 deletions compiler/config.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
var NODE_ENV = process.env.NODE_ENV;
var config;

module.exports = {
/**
* If true, then the compiler will check the disk to see if a previously compiled
* template is the same age or newer than the source template. If so, the previously
* compiled template will be loaded. Otherwise, the template will be recompiled
* and saved to disk.
*
* If false, the template will always be recompiled. If `writeToDisk` is false
* then this option will be ignored.
*/
checkUpToDate: process.env.MARKO_CLEAN ? false : true,
/**
* If true (the default) then compiled templates will be written to disk. If false,
* compiled templates will not be written to disk (i.e., no `.marko.js` file will
* be generated)
*/
writeToDisk: true,
/* globals window */
var g = typeof window === 'undefined' ? global : window;

/**
* If true, then the compiled template on disk will assumed to be up-to-date if it exists.
*/
assumeUpToDate: process.env.MARKO_CLEAN != null || process.env.hasOwnProperty('MARKO_HOT_RELOAD') ? false : ( NODE_ENV == null ? false : (NODE_ENV !== 'development' && NODE_ENV !== 'dev')),

/**
* If true, whitespace will be preserved in templates. Defaults to false.
* @type {Boolean}
*/
preserveWhitespace: false
};
if (g.__MARKO_CONFIG) {
config = g.__MARKO_CONFIG;
} else {
config = g.__MARKO_CONFIG = {
/**
* If true, then the compiler will check the disk to see if a previously compiled
* template is the same age or newer than the source template. If so, the previously
* compiled template will be loaded. Otherwise, the template will be recompiled
* and saved to disk.
*
* If false, the template will always be recompiled. If `writeToDisk` is false
* then this option will be ignored.
*/
checkUpToDate: process.env.MARKO_CLEAN ? false : true,
/**
* If true (the default) then compiled templates will be written to disk. If false,
* compiled templates will not be written to disk (i.e., no `.marko.js` file will
* be generated)
*/
writeToDisk: true,

/**
* If true, then the compiled template on disk will assumed to be up-to-date if it exists.
*/
assumeUpToDate: process.env.MARKO_CLEAN != null || process.env.hasOwnProperty('MARKO_HOT_RELOAD') ? false : ( NODE_ENV == null ? false : (NODE_ENV !== 'development' && NODE_ENV !== 'dev')),

/**
* If true, whitespace will be preserved in templates. Defaults to false.
* @type {Boolean}
*/
preserveWhitespace: false
};
}

module.exports = config;
14 changes: 14 additions & 0 deletions test/autotests/api-compiler/configure-singleton/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.check = function(marko, markoCompiler, expect, done) {
var configModulePath = require.resolve('../../../../compiler/config');
var config = require(configModulePath);

var globalConfig = global.__MARKO_CONFIG;
expect(config).to.equal(globalConfig);

delete require.cache[configModulePath];

config = require(configModulePath);
expect(config).to.equal(globalConfig);

done();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Hello ${data.name}!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<include('./include-target.marko') name="Frank"/>
28 changes: 28 additions & 0 deletions test/autotests/api/writeToDisk-false-include/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var nodePath = require('path');
var fs = require('fs');

exports.check = function(marko, markoCompiler, expect, done) {

markoCompiler.configure({
writeToDisk: false
});

var templatePath = nodePath.join(__dirname, 'template.marko');
var template = marko.load(templatePath);
expect(template.renderSync({name: 'Frank'})).to.equal('Hello Frank!');

markoCompiler.configure({
writeToDisk: false
});

var compiledTemplatePath = nodePath.join(__dirname, 'template.marko.js');
var compiledIncludeTemplatePath = nodePath.join(__dirname, 'include-target.marko.js');

expect(fs.existsSync(compiledTemplatePath)).to.equal(false);
expect(fs.existsSync(compiledIncludeTemplatePath)).to.equal(false);

// Revert back to defaults
markoCompiler.configure();

done();
};
3 changes: 3 additions & 0 deletions test/autotests/api/writeToDisk-false-layout/layout.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<layout-placeholder name="body"></layout-placeholder>
</div>
3 changes: 3 additions & 0 deletions test/autotests/api/writeToDisk-false-layout/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<layout-use('./layout.marko')>
<layout-put into="body">Hello ${data.name}!</layout-put>
</layout-use>
28 changes: 28 additions & 0 deletions test/autotests/api/writeToDisk-false-layout/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var nodePath = require('path');
var fs = require('fs');

exports.check = function(marko, markoCompiler, expect, done) {

markoCompiler.configure({
writeToDisk: false
});

var templatePath = nodePath.join(__dirname, 'template.marko');
var template = marko.load(templatePath);
expect(template.renderSync({name: 'Frank'})).to.equal('<div>Hello Frank!</div>');

markoCompiler.configure({
writeToDisk: false
});

var compiledTemplatePath = nodePath.join(__dirname, 'template.marko.js');
var compiledIncludeTemplatePath = nodePath.join(__dirname, 'layout.marko.js');

expect(fs.existsSync(compiledTemplatePath)).to.equal(false);
expect(fs.existsSync(compiledIncludeTemplatePath)).to.equal(false);

// Revert back to defaults
markoCompiler.configure();

done();
};

0 comments on commit cd79732

Please sign in to comment.