Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Fix #195
Browse files Browse the repository at this point in the history
The previous cache value we used was non-serializable we provoqued
bug #195 when the cache was used. We now follow the Meteor built-in
CachingHtmlCompiler model.
  • Loading branch information
mquandalle committed Dec 26, 2015
1 parent 5064396 commit b990acc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 58 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
"$": false,
"_": false,
"BlazeTools": false,
"CachingCompiler": false,
"CachingHtmlCompiler": false,
"HTML": false,
"HTMLTools": false,
"Npm": false,
"Package": false,
"SpacebarsCompiler": false,
"Template": false,
"TemplateCompiler": false,
"Tinytest": false,

// Our globals variables
Expand Down
2 changes: 1 addition & 1 deletion packages/jade/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Package.registerBuildPlugin({
name: "compileJadeBatch",
use: [
"[email protected]",
"[email protected].0",
"caching-html-[email protected].2",
"[email protected]",
"[email protected]",
"[email protected]",
Expand Down
79 changes: 24 additions & 55 deletions packages/jade/plugin/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ Plugin.registerCompiler({
isTemplate: true,
}, () => new JadeCompilerPlugin());

class JadeCompilerPlugin extends CachingCompiler {
class JadeCompilerPlugin extends CachingHtmlCompiler {
constructor() {
super({
compilerName: 'jade',
defaultCacheSize: 1024*1024*10,
});
super('jade');
}

compileOneFile(file) {
compileOneFile(inputFile) {
const mode = this._getMode(inputFile);

try {
return this._getCompilerResult(file);
const compileResult = this._getCompilerResult(mode, inputFile);
return this[`_${mode}ModeHandler`](inputFile, compileResult);
} catch (err) {
file.error({
inputFile.error({
message: "Jade syntax error: " + err.message
});
}
Expand All @@ -28,19 +28,6 @@ class JadeCompilerPlugin extends CachingCompiler {
return [ inputFile.getSourceHash() ];
}

addCompileResult(inputFile, compileResult) {
const fileMode = this._getMode(inputFile);
this[`_${fileMode}ModeHandler`](inputFile, compileResult);
}

compileResultSize(compileResult) {
let n = 0;
n += Object.keys(compileResult.templates || {}).length;
n += compileResult.head ? 1 : 0;
n += compileResult.body ? 1 : 0;
return n;
}

_getMode(file) {
const ext = file.getExtension();
return (ext === 'jade') ? 'file' : 'template';
Expand Down Expand Up @@ -75,11 +62,11 @@ class JadeCompilerPlugin extends CachingCompiler {
`;
}

_getCompilerResult(file) {
_getCompilerResult(mode, file) {
try {
return JadeCompiler.parse(file.getContentsAsString(), {
filename: file.getPathInPackage(),
fileMode: this._getMode(file) === 'file'
fileMode: mode === 'file'
});
} catch (err) {
return file.error({
Expand All @@ -90,53 +77,35 @@ class JadeCompilerPlugin extends CachingCompiler {
}

_fileModeHandler(file, results) {
// Head
let head = '', body = '', js = '', bodyAttrs = {};

if (results.head !== null) {
file.addHtml({
section: "head",
data: HTML.toHTML(results.head)
});
head = HTML.toHTML(results.head);
}

let jsContent = "";
if (results.body !== null) {
jsContent += this._bodyGen(results.body, results.bodyAttrs);
js += this._bodyGen(results.body, results.bodyAttrs);
}
if (! _.isEmpty(results.templates)) {
jsContent += _.map(results.templates, this._templateGen).join("");
js += _.map(results.templates, this._templateGen).join("");
}

if (jsContent !== "") {
file.addJavaScript({
path: file.getPathInPackage() + '.js',
sourcePath: file.getPathInPackage(),
data: jsContent
});
}
return { head, body, js, bodyAttrs };
}

_templateModeHandler(file, result) {
let head = '', body = '', js = '', bodyAttrs = {};

const templateName = path.basename(file.getPathInPackage(), '.tpl.jade');
let jsContent;

if (templateName === "head") {
file.addHtml({
section: "head",
data: HTML.toHTML(result)
});

head = HTML.toHTML(result);
} else if (templateName === "body") {
js = this._bodyGen(result);
} else {
if (templateName === "body") {
jsContent = this._bodyGen(result);
} else {
jsContent = this._templateGen(result, templateName);
}

file.addJavaScript({
path: file.getPathInPackage() + '.js',
sourcePath: file.getPathInPackage(),
data: jsContent
});
js = this._templateGen(result, templateName);
}

return { head, body, js, bodyAttrs };
}
}

0 comments on commit b990acc

Please sign in to comment.