From cd855f6edfc2f8b92c172ce04565e288c0a18e09 Mon Sep 17 00:00:00 2001 From: Michael Rawlings Date: Wed, 18 Jan 2017 18:22:14 -0800 Subject: [PATCH] work around issues with server bundling --- compiler/CompileContext.js | 2 +- .../index.js} | 2 +- runtime/env-init.js | 14 +- runtime/loader/index-default.js | 160 +++++++++++++++++ runtime/loader/index.js | 163 +----------------- 5 files changed, 176 insertions(+), 165 deletions(-) rename runtime/{dependencies.js => dependencies/index.js} (97%) create mode 100644 runtime/loader/index-default.js diff --git a/compiler/CompileContext.js b/compiler/CompileContext.js index 44082c5c2..667fdc9be 100644 --- a/compiler/CompileContext.js +++ b/compiler/CompileContext.js @@ -523,7 +523,7 @@ class CompileContext extends EventEmitter { var templateVar; - if (this.options.browser) { + if (this.options.browser || this.options.requireTemplates) { // When compiling a Marko template for the browser we just use `require('./template.marko')` templateVar = this.addStaticVar(varName, builder.require(builder.literal(relativePath))); } else { diff --git a/runtime/dependencies.js b/runtime/dependencies/index.js similarity index 97% rename from runtime/dependencies.js rename to runtime/dependencies/index.js index 16bc617e7..eb2557183 100644 --- a/runtime/dependencies.js +++ b/runtime/dependencies/index.js @@ -1,6 +1,6 @@ var path = require('path'); var resolveFrom = require('resolve-from'); -var Template = require('./html/Template'); +var Template = require('../html/Template'); exports.getDeps = getDeps; exports.resolveDep = resolveDep; diff --git a/runtime/env-init.js b/runtime/env-init.js index 599c961ab..e0af6ea4b 100644 --- a/runtime/env-init.js +++ b/runtime/env-init.js @@ -1,13 +1,15 @@ require('./stream'); require('./dependencies'); -if (process.env.hasOwnProperty('MARKO_HOT_RELOAD')) { - require('../hot-reload').enable(); -} +if (!process.env.BUNDLE) { + if (process.env.MARKO_HOT_RELOAD) { + require('../hot-reload').enable(); + } -// If process was launched with browser refresh then automatically -// enable browser-refresh -require('../browser-refresh').enable(); + // If process was launched with browser refresh then automatically + // enable browser-refresh + require('../browser-refresh').enable(); +} function fixFlush() { try { diff --git a/runtime/loader/index-default.js b/runtime/loader/index-default.js new file mode 100644 index 000000000..38f065bf7 --- /dev/null +++ b/runtime/loader/index-default.js @@ -0,0 +1,160 @@ +'use strict'; + +module.exports = function load(templatePath, templateSrc, options) { + if (arguments.length === 1) { + return doLoad(templatePath); + } else if (arguments.length === 2) { + // see if second argument is templateSrc (a String) + // or options (an Object) + var lastArg = arguments[arguments.length - 1]; + if (typeof lastArg === 'string') { + return doLoad(templatePath, templateSrc); + } else { + var finalOptions = templateSrc; + return doLoad(templatePath, null, finalOptions); + } + } else if (arguments.length === 3) { + // assume function called according to function signature + return doLoad(templatePath, templateSrc, options); + } else { + throw new Error('Illegal arguments'); + } +}; + +var nodePath = require('path'); +var fs = require('fs'); +var Module = require('module').Module; +var compilerPath = nodePath.join(__dirname, '../../compiler'); +var markoCompiler = require(compilerPath); +var cwd = process.cwd(); +var fsOptions = {encoding: 'utf8'}; + +function loadSource(templatePath, compiledSrc) { + var templateModulePath = templatePath + '.js'; + + // Short-circuit loading if the template has already been cached in the Node.js require cache + var cached = require.cache[templateModulePath]; + if (cached) { + return cached.exports; + } + + var templateModule = new Module(templateModulePath, module); + templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templateModulePath)); + templateModule.filename = templateModulePath; + + Module._cache[templateModulePath] = templateModule; + + templateModule._compile( + compiledSrc, + templateModulePath); + + return templateModule.exports; +} + +function getLoadedTemplate(path) { + var cached = require.cache[path]; + return cached && cached.exports.render ? cached.exports : undefined; +} + +function loadFile(templatePath, options) { + options = Object.assign({}, markoCompiler.defaultOptions, options); + + var targetFile = templatePath + '.js'; + + // Short-circuit loading if the template has already been cached in the Node.js require cache + var cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath); + if (cachedTemplate) { + return cachedTemplate; + } + + // Just in case the the path wasn't a fully resolved file system path... + templatePath = nodePath.resolve(cwd, templatePath); + targetFile = templatePath + '.js'; + + // Check the require cache again after fully resolving the path + cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath); + if (cachedTemplate) { + return cachedTemplate; + } + + // 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); + var targetDir = nodePath.dirname(targetFile); + var tempFile = nodePath.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename); + fs.writeFileSync(tempFile, compiledSrc, fsOptions); + fs.renameSync(tempFile, targetFile); + + return require(targetFile); +} + +function createRenderProxy(template) { + return function(data, out) { + template._(data, out); + }; +} + +function doLoad(templatePath, templateSrc, options) { + options = Object.assign({}, markoCompiler.defaultOptions, options); + + var template; + if (typeof templatePath.render === 'function') { + template = templatePath; + } else { + var writeToDisk = options.writeToDisk; + + + // If the template source is provided then we can compile the string + // in memory and there is no need to read template file from disk or + // write compiled code to disk. + // + // If writeToDisk is false then there will be no up-to-date check + // since compiled source won't be written to disk. + if ((templateSrc != null) || (writeToDisk === false)) { + // 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 == null) { + templateSrc = fs.readFileSync(templatePath, fsOptions); + } + + var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options); + + if (writeToDisk === true) { + var targetFile = templatePath + '.js'; + fs.writeFileSync(targetFile, compiledSrc, fsOptions); + } + + template = loadSource(templatePath, compiledSrc); + } else { + template = loadFile(templatePath, options); + } + } + + if (options.buffer === false) { + var Template = template.constructor; + + template = new Template( + template.path, + createRenderProxy(template), + options); + } + + return template; +} \ No newline at end of file diff --git a/runtime/loader/index.js b/runtime/loader/index.js index 171d29a1f..1363736c6 100644 --- a/runtime/loader/index.js +++ b/runtime/loader/index.js @@ -1,158 +1,7 @@ -'use strict'; -module.exports = function load(templatePath, templateSrc, options) { - if (arguments.length === 1) { - return doLoad(templatePath); - } else if (arguments.length === 2) { - // see if second argument is templateSrc (a String) - // or options (an Object) - var lastArg = arguments[arguments.length - 1]; - if (typeof lastArg === 'string') { - return doLoad(templatePath, templateSrc); - } else { - var finalOptions = templateSrc; - return doLoad(templatePath, null, finalOptions); - } - } else if (arguments.length === 3) { - // assume function called according to function signature - return doLoad(templatePath, templateSrc, options); - } else { - throw new Error('Illegal arguments'); - } -}; - -var nodePath = require('path'); -var fs = require('fs'); -var Module = require('module').Module; -var markoCompiler = require('../../compiler'); -var cwd = process.cwd(); -var fsOptions = {encoding: 'utf8'}; - -function loadSource(templatePath, compiledSrc) { - var templateModulePath = templatePath + '.js'; - - // Short-circuit loading if the template has already been cached in the Node.js require cache - var cached = require.cache[templateModulePath]; - if (cached) { - return cached.exports; - } - - var templateModule = new Module(templateModulePath, module); - templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templateModulePath)); - templateModule.filename = templateModulePath; - - Module._cache[templateModulePath] = templateModule; - - templateModule._compile( - compiledSrc, - templateModulePath); - - return templateModule.exports; -} - -function getLoadedTemplate(path) { - var cached = require.cache[path]; - return cached && cached.exports.render ? cached.exports : undefined; -} - -function loadFile(templatePath, options) { - options = Object.assign({}, markoCompiler.defaultOptions, options); - - var targetFile = templatePath + '.js'; - - // Short-circuit loading if the template has already been cached in the Node.js require cache - var cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath); - if (cachedTemplate) { - return cachedTemplate; - } - - // Just in case the the path wasn't a fully resolved file system path... - templatePath = nodePath.resolve(cwd, templatePath); - targetFile = templatePath + '.js'; - - // Check the require cache again after fully resolving the path - cachedTemplate = getLoadedTemplate(targetFile) || getLoadedTemplate(templatePath); - if (cachedTemplate) { - return cachedTemplate; - } - - // 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); - var targetDir = nodePath.dirname(targetFile); - var tempFile = nodePath.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename); - fs.writeFileSync(tempFile, compiledSrc, fsOptions); - fs.renameSync(tempFile, targetFile); - - return require(targetFile); -} - -function createRenderProxy(template) { - return function(data, out) { - template._(data, out); - }; -} - -function doLoad(templatePath, templateSrc, options) { - options = Object.assign({}, markoCompiler.defaultOptions, options); - - var template; - if (typeof templatePath.render === 'function') { - template = templatePath; - } else { - var writeToDisk = options.writeToDisk; - - - // If the template source is provided then we can compile the string - // in memory and there is no need to read template file from disk or - // write compiled code to disk. - // - // If writeToDisk is false then there will be no up-to-date check - // since compiled source won't be written to disk. - if ((templateSrc != null) || (writeToDisk === false)) { - // 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 == null) { - templateSrc = fs.readFileSync(templatePath, fsOptions); - } - - var compiledSrc = markoCompiler.compile(templateSrc, templatePath, options); - - if (writeToDisk === true) { - var targetFile = templatePath + '.js'; - fs.writeFileSync(targetFile, compiledSrc, fsOptions); - } - - template = loadSource(templatePath, compiledSrc); - } else { - template = loadFile(templatePath, options); - } - } - - if (options.buffer === false) { - var Template = template.constructor; - - template = new Template( - template.path, - createRenderProxy(template), - options); - } - - return template; +if (process.env.BUNDLE) { + // you cannot load templates dynamically within a bundle + // all templates should be pre-compiled as part of the bundle + module.exports = function(){}; +} else { + module.exports = require('./index-default'); } \ No newline at end of file