Skip to content

Commit

Permalink
VFS support with check for "generated" to allow native lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
janakg committed Jun 2, 2018
1 parent 3b462e6 commit f4e3a4d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 9 deletions.
9 changes: 7 additions & 2 deletions dist/compiler/taglib-loader/json-file-reader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
var fs = require("fs");
var nativeFS = require("fs");
var virtualFS = require("../../vfs");
var stripJsonComments = require("strip-json-comments");
var fsReadOptions = { encoding: "utf8" };
var fs = nativeFS;

exports.readFileSync = function (path) {

//Checking "generated" keyword to allow other lookups. Must be a better way to do this.
var vfs = virtualFS.getVirtualFileSystem();
if (vfs) {
if (vfs && path.indexOf("generated/") > -1) {
fs = vfs;
} else {
fs = nativeFS;
}

var json = fs.readFileSync(path, fsReadOptions);
Expand Down
7 changes: 5 additions & 2 deletions dist/compiler/taglib-loader/loadTagFromProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var ok = require("assert").ok;
var propertyHandlers = require("property-handlers");
var isObjectEmpty = require("raptor-util/isObjectEmpty");
var nodePath = require("path");
var virtualFS = require("../../vfs");
var markoModules = require("../modules"); // NOTE: different implementation for browser
var bodyFunctionRegExp = /^([A-Za-z_$][A-Za-z0-9_]*)(?:\(([^)]*)\))?$/;
var safeVarName = /^[A-Za-z_$][A-Za-z0-9_]*$/;
Expand Down Expand Up @@ -325,9 +326,11 @@ class TagLoader {
template(value) {
var tag = this.tag;
var dirname = this.dirname;

var path = nodePath.resolve(dirname, value);
if (!exists(path)) {
var vfs = virtualFS.getVirtualFileSystem();
var fileExists = vfs ? vfs.realpathSync(path) : exists(path);

if (!fileExists) {
throw new Error('Template at path "' + path + '" does not exist.');
}
tag.template = path;
Expand Down
12 changes: 11 additions & 1 deletion dist/compiler/taglib-loader/scanTagsDir.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"use strict";

const nativeFS = require("fs");
const virtualFS = require("../../vfs");
const nodePath = require("path");
const fs = require("fs");
const stripJsonComments = require("strip-json-comments");
const tagDefFromCode = require("./tag-def-from-code");
const loaders = require("./loaders");
const fsReadOptions = { encoding: "utf8" };
const extend = require("raptor-util/extend");
const types = require("./types");
let fs = nativeFS;

const tagFileTypes = ["template", "renderer", "transformer", "code-generator", "node-factory"];

Expand Down Expand Up @@ -97,6 +99,14 @@ module.exports = function scanTagsDir(tagsConfigPath, tagsConfigDirname, dir, ta
prefix = "";
}

//Checking "generated" keyword to allow other lookups. Must be a better way to do this.
var vfs = virtualFS.getVirtualFileSystem();
if (vfs && dir.indexOf("generated/") > -1) {
fs = vfs;
} else {
fs = nativeFS;
}

dir = nodePath.resolve(tagsConfigDirname, dir);
let children = fs.readdirSync(dir);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"lasso-resolve-from": "^1.2.0",
"lint-staged": "^7.0.0",
"marko-widgets": "^7.0.1",
"memfs": "^2.9.0",
"micromatch": "^3.0.4",
"mkdirp": "^0.5.1",
"mocha": "^5.0.1",
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/taglib-loader/json-file-reader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
var fs = require("fs");
var nativeFS = require("fs");
var virtualFS = require("../../vfs");
var stripJsonComments = require("strip-json-comments");
var fsReadOptions = { encoding: "utf8" };
var fs = nativeFS;

exports.readFileSync = function(path) {
//Checking "generated" keyword to allow other lookups. Must be a better way to do this.
var vfs = virtualFS.getVirtualFileSystem();
if (vfs) {
if (vfs && path.indexOf("generated/") > -1) {
fs = vfs;
} else {
fs = nativeFS;
}

var json = fs.readFileSync(path, fsReadOptions);
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/taglib-loader/scanTagsDir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

let fs = require("fs");
const nativeFS = require("fs");
const virtualFS = require("../../vfs");
const nodePath = require("path");
const stripJsonComments = require("strip-json-comments");
Expand All @@ -9,6 +9,7 @@ const loaders = require("./loaders");
const fsReadOptions = { encoding: "utf8" };
const extend = require("raptor-util/extend");
const types = require("./types");
let fs = nativeFS;

const tagFileTypes = [
"template",
Expand Down Expand Up @@ -119,9 +120,12 @@ module.exports = function scanTagsDir(
prefix = "";
}

//Checking "generated" keyword to allow other lookups. Must be a better way to do this.
var vfs = virtualFS.getVirtualFileSystem();
if (vfs) {
if (vfs && dir.indexOf("generated/") > -1) {
fs = vfs;
} else {
fs = nativeFS;
}

dir = nodePath.resolve(tagsConfigDirname, dir);
Expand Down
68 changes: 68 additions & 0 deletions vfs-usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var compiler = require("./compiler");
var { fs, vol } = require("memfs");

var componentCode =
"<div> \
<p>Hello ${input.name}!</p> \
<button if(input.renderBody)> \
<include(input.renderBody)/> \
</button> \
</div>";

var sourceCode = `class {
onCreate() {
this.state = {
mounted: false
};
}
onMount() {
this.state.mounted = true;
}
}
<div>
<p if(state.mounted)>
UI component successfully mounted!
<ContentBox name='Frank'> Shop Now </ContentBox>
</p>
</div>`;

/* Configure the VFS */
compiler.configureVFS(fs);

var cwd = process.cwd();
vol.mkdirpSync(cwd + "/src/generated/components/ContentBox/");
fs.writeFileSync(
cwd + "/src/generated/components/ContentBox/index.marko",
componentCode,
"utf8"
);

var tab_json = {
"tags-dir": [cwd + "/src/generated/components"]
};

// var tab_json = {
// "<ContentBox>": { "template": cwd + "/src/generated/components/ContentBox/index.marko"}
// }

fs.writeFileSync(cwd + "/src/generated/marko.json", JSON.stringify(tab_json));
compiler.registerTaglib(cwd + "/src/generated/marko.json");

vol.mkdirpSync(cwd + "/src/generated/templates/example01/");
var filePath = cwd + "/src/generated/templates/example01/index.marko";
fs.writeFileSync(filePath, sourceCode);

var compiled = compiler.compileFile(filePath, {
output: "vdom",
browser: true,
writeVersionComment: false,
sourceOnly: false,
meta: false
});

var compiledSrc = compiled.code;
fs.writeFileSync(
cwd + "/src/generated/templates/example01/index.marko.js",
compiledSrc
);

0 comments on commit f4e3a4d

Please sign in to comment.