Merge branch 'master' of github.com:marko-js/marko into htmljs-parser

This commit is contained in:
Patrick Steele-Idem 2016-02-11 15:29:59 -07:00
commit 261088791d
12 changed files with 137 additions and 23 deletions

View File

@ -3,6 +3,32 @@ Changelog
# 2.x
## 2.8.x
### 2.8.4
- Fixes circular dependency issue between runtime/index.js and hot-reload/index.js
### 2.8.3
- Fixes circular dependency issue between runtime/index.js and hot-reload/index.js
### 2.8.2
- Fixes #203 - Incorrect behavior when attrs is used on a standard HTML tag with a tag def
### 2.8.1
- Fixes #202 - Pass along options to compiler when loading a template
### 2.8.0
- Added support for automatically discovering taglibs from installed packages that are scoped. ([PR #183](https://github.com/marko-js/marko/pull/183) by [@tropperstyle](https://github.com/tropperstyle))
### 2.8.3
- Fixes circular dependency issue between `hot-reload/index.js` and `runtime/index.js`
## 2.7.x
### 2.7.31

View File

@ -1,7 +1,7 @@
![Marko Logo](https://raw.githubusercontent.com/marko-js/branding/master/marko-logo-small.png)
[![Build Status](https://travis-ci.org/marko-js/marko.svg?branch=master)](https://travis-ci.org/marko-js/marko) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/marko-js/marko)
[![NPM](https://nodei.co/npm/marko.png)](https://nodei.co/npm/marko/)
[![NPM](https://img.shields.io/npm/v/marko.svg)](https://www.npmjs.com/package/marko)
[![Downloads](https://img.shields.io/npm/dm/marko.svg)](http://npm-stat.com/charts.html?package=marko)
Marko is a fast and lightweight HTML-based templating engine that compiles templates to CommonJS modules and supports streaming, async rendering and custom tags. Learn more on [http://markojs.com/](http://markojs.com/).

View File

@ -6,7 +6,7 @@ var Parser = require('./Parser');
var HtmlJsParser = require('./HtmlJsParser');
var Builder = require('./Builder');
var extend = require('raptor-util/extend');
var NODE_ENV = process.env.NODE_ENV;
var defaultParser = new Parser(new HtmlJsParser());
var defaultOptions = {
@ -25,7 +25,12 @@ var defaultOptions = {
* compiled templates will not be written to disk (i.e., no `.marko.js` file will
* be generated)
*/
writeToDisk: true
writeToDisk: true,
/**
* If true, then the compiled template on disk will assumed to be up-to-date if it exists.
*/
assumeUpToDate: NODE_ENV == null ? false : (NODE_ENV !== 'development' && NODE_ENV !== 'dev')
};
function configure(config) {

View File

@ -84,30 +84,52 @@ function tryNodeModules(parent, helper) {
if ((nodeModulesDir = realpathCached(nodeModulesDir))) {
taglibsForNodeModulesDir = [];
var children = fs.readdirSync(nodeModulesDir);
children.forEach(function(moduleDirBasename) {
var handlePackageDir = function(packageName) {
// Fixes https://github.com/marko-js/marko/issues/140
// If the same node_module is found multiple times then only load the first one.
// Only the package name (that is: node_modules/<module_name>) matters and the
// package version does not matter.
if (helper.foundTaglibPackages[moduleDirBasename]) {
if (helper.foundTaglibPackages[packageName]) {
return;
}
helper.foundTaglibPackages[moduleDirBasename] = true;
helper.foundTaglibPackages[packageName] = true;
var moduleDir = nodePath.join(nodeModulesDir, moduleDirBasename);
var moduleDir = nodePath.join(nodeModulesDir, packageName);
var taglibPath = nodePath.join(moduleDir, 'marko-taglib.json');
if (existsCached(taglibPath)) {
taglibPath = fs.realpathSync(taglibPath);
var taglib = taglibLoader.load(taglibPath);
taglib.moduleName = moduleDirBasename;
taglib.moduleName = packageName;
taglibsForNodeModulesDir.push(taglib);
helper.addTaglib(taglib);
}
});
};
fs.readdirSync(nodeModulesDir)
.forEach(function(packageName) {
if (packageName.charAt(0) === '@') {
// Add support for npm scoped packages. Scoped packages
// get instaled into subdirectories organized by user.
// For example:
// node_modules/@foo/my-package
// node_modules/@foo/another-package
var scope = packageName; // e.g. scope = '@foo'
// We need to loop over the nested directory to automatically
// discover taglibs exported by scoped packages.
fs.readdirSync(nodePath.join(nodeModulesDir, scope))
.forEach(function(packageName) {
handlePackageDir(scope + '/' + packageName); // @foo/my-package
});
} else {
handlePackageDir(packageName);
}
});
taglibsForNodeModulesDirCache[nodeModulesDir] = taglibsForNodeModulesDir.length ? taglibsForNodeModulesDir : null;
} else {
@ -180,4 +202,4 @@ function clearCaches() {
exports.find = find;
exports.excludeDir = excludeDir;
exports.clearCaches = clearCaches;
exports.clearCaches = clearCaches;

View File

@ -1,12 +1,12 @@
/*
* Copyright 2011 eBay Software Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -19,19 +19,29 @@ require('raptor-polyfill/string/endsWith');
var extend = require('raptor-util/extend');
var compiler = require('../compiler');
var nodePath = require('path');
var runtime = require('../runtime');
var Template = runtime.Template;
var modifiedFlag = 1;
var runtime;
/**
* Lazily require the Marko runtime because there is a circular dependency.
* We need to export our `enable` function before actually requiring the
* Marko runtime.
*/
function _getMarkoRuntime() {
return runtime || (runtime = require('../runtime'));
}
exports.enable = function() {
var runtime = _getMarkoRuntime();
if (runtime.__hotReloadEnabled) {
// Marko has already been monkey-patched. Nothing to do!
return;
}
var Template = runtime.Template;
runtime.__hotReloadEnabled = true;
// We set an environment variable so that _all_ marko modules
@ -93,7 +103,7 @@ exports.enable = function() {
};
exports.handleFileModified = function(path) {
var runtime = _getMarkoRuntime();
var basename = nodePath.basename(path);
if (path.endsWith('.marko') ||

View File

@ -69,4 +69,4 @@
"logo": {
"url": "https://raw.githubusercontent.com/marko-js/branding/master/marko-logo-small.png"
}
}
}

View File

@ -21,6 +21,7 @@ var Module = require('module').Module;
var markoCompiler = require('../compiler');
var cwd = process.cwd();
var fsReadOptions = {encoding: 'utf8'};
var extend = require('raptor-util/extend');
if (process.env.hasOwnProperty('MARKO_HOT_RELOAD')) {
require('../hot-reload').enable();
@ -45,16 +46,43 @@ function loadSource(templatePath, compiledSrc) {
}
function loadFile(templatePath, options) {
var targetFile = templatePath + '.js';
// Short-circuit loading if the template has already been cached in the Node.js require cache
var cached = require.cache[targetFile];
if (cached) {
return cached.exports;
}
templatePath = nodePath.resolve(cwd, templatePath);
var targetDir = nodePath.dirname(templatePath);
var targetFile = templatePath + '.js';
var isUpToDate = markoCompiler.checkUpToDate(templatePath, targetFile);
targetFile = templatePath + '.js';
// Check the require cache again after fully resolving the path
cached = require.cache[targetFile];
if (cached) {
return cached.exports;
}
options = extend(extend({}, markoCompiler.defaultOptions), options);
// If the `assumeUpToDate` option is true then we just assume that the compiled template on disk is up-to-date
// if it exists
if (options.assumeUpToDate) {
if (fs.existsSync(targetFile)) {
return require(targetFile);
}
}
var isUpToDate = markoCompiler.checkUpToDate(targetFile);
if (isUpToDate) {
return require(targetFile);
}
var compiledSrc = markoCompiler.compileFile(templatePath, options);
// console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc);
var filename = nodePath.basename(targetFile);
@ -86,7 +114,6 @@ module.exports = function load(templatePath, templateSrc, options) {
// Don't write the compiled template to disk. Instead, load it
// directly from the compiled source using the internals of the
// Node.js module loading system.
if (templateSrc === undefined) {
templateSrc = fs.readFileSync(templatePath, fsReadOptions);
}
@ -98,4 +125,4 @@ module.exports = function load(templatePath, templateSrc, options) {
}
};
module.exports.loadSource = loadSource;
module.exports.loadSource = loadSource;

View File

@ -0,0 +1,3 @@
<script foo="bar" nonce="foo" hello="world">
console.log('foo')
</script>

View File

@ -0,0 +1,5 @@
{
"<script>": {
"transformer": "./script-nonce-transformer.js"
}
}

View File

@ -0,0 +1,8 @@
module.exports = function transform(el, context) {
if (el.hasAttribute('csp-nonce')) {
el.removeAttribute('csp-nonce');
el.setAttributeValue('nonce', context.builder.literal('foo'));
}
};

View File

@ -0,0 +1,3 @@
<script foo="bar" ${data.myAttrs} csp-nonce>
console.log('foo')
</script>

View File

@ -0,0 +1,5 @@
exports.templateData = {
"myAttrs": {
"hello": "world"
}
};