From f4e3a4d315a040ad3550d7712bacad731f4d37c1 Mon Sep 17 00:00:00 2001 From: Ram Ganesan Date: Sat, 2 Jun 2018 12:37:53 +0530 Subject: [PATCH] VFS support with check for "generated" to allow native lookups --- .../taglib-loader/json-file-reader.js | 9 ++- .../taglib-loader/loadTagFromProps.js | 7 +- dist/compiler/taglib-loader/scanTagsDir.js | 12 +++- package.json | 1 + .../taglib-loader/json-file-reader.js | 8 ++- src/compiler/taglib-loader/scanTagsDir.js | 8 ++- vfs-usage.js | 68 +++++++++++++++++++ 7 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 vfs-usage.js diff --git a/dist/compiler/taglib-loader/json-file-reader.js b/dist/compiler/taglib-loader/json-file-reader.js index 7e6f092721..b97a5de1f7 100644 --- a/dist/compiler/taglib-loader/json-file-reader.js +++ b/dist/compiler/taglib-loader/json-file-reader.js @@ -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); diff --git a/dist/compiler/taglib-loader/loadTagFromProps.js b/dist/compiler/taglib-loader/loadTagFromProps.js index 8c7be43343..614d027859 100644 --- a/dist/compiler/taglib-loader/loadTagFromProps.js +++ b/dist/compiler/taglib-loader/loadTagFromProps.js @@ -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_]*$/; @@ -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; diff --git a/dist/compiler/taglib-loader/scanTagsDir.js b/dist/compiler/taglib-loader/scanTagsDir.js index a16be98734..3c6c169e13 100644 --- a/dist/compiler/taglib-loader/scanTagsDir.js +++ b/dist/compiler/taglib-loader/scanTagsDir.js @@ -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"]; @@ -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); diff --git a/package.json b/package.json index 1e96118969..cb52c2dafc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/compiler/taglib-loader/json-file-reader.js b/src/compiler/taglib-loader/json-file-reader.js index c9ddc135bf..dd029fa693 100644 --- a/src/compiler/taglib-loader/json-file-reader.js +++ b/src/compiler/taglib-loader/json-file-reader.js @@ -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); diff --git a/src/compiler/taglib-loader/scanTagsDir.js b/src/compiler/taglib-loader/scanTagsDir.js index c9133d6c9e..8934b36047 100644 --- a/src/compiler/taglib-loader/scanTagsDir.js +++ b/src/compiler/taglib-loader/scanTagsDir.js @@ -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"); @@ -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", @@ -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); diff --git a/vfs-usage.js b/vfs-usage.js new file mode 100644 index 0000000000..919da486f8 --- /dev/null +++ b/vfs-usage.js @@ -0,0 +1,68 @@ +var compiler = require("./compiler"); +var { fs, vol } = require("memfs"); + +var componentCode = + "
\ +

Hello ${input.name}!

\ + \ +
"; + +var sourceCode = `class { + onCreate() { + this.state = { + mounted: false + }; + } + onMount() { + this.state.mounted = true; + } +} + +
+

+ UI component successfully mounted! + Shop Now +

+
`; + +/* 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 = { +// "": { "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 +);