From 9f6574da3033b3a00475a82252c1dc0a41daf3d4 Mon Sep 17 00:00:00 2001 From: Arian Stolwijk Date: Fri, 27 Dec 2013 19:03:26 +0100 Subject: [PATCH] Start working on #9 for browser and amd-combined. --- lib/module.js | 10 +++++ lib/moduleStorage.js | 2 +- lib/output/amdOneFile.js | 51 ++++++++++++++-------- lib/output/browser.js | 33 +++++++++----- lib/output/mixin/generateModuleVerbatim.js | 10 +++++ lib/output/mixin/singleFile.js | 3 +- 6 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 lib/output/mixin/generateModuleVerbatim.js diff --git a/lib/module.js b/lib/module.js index a62d164..bde3fd2 100644 --- a/lib/module.js +++ b/lib/module.js @@ -22,6 +22,8 @@ var Module = prime({ this.ready = false this.err = false this.invalid = false + this.astOut = null + this.srcOut = '' this.moduleIDs = {} }, @@ -45,6 +47,14 @@ var Module = prime({ return file }, + invalidate: function(){ + this.invalid = true + this.src = '' + this.ast = null + this.srcOut = '' + this.astOut = null + }, + getModuleFile: function(_path){ return this.getModuleID(_path) + '.js' }, diff --git a/lib/moduleStorage.js b/lib/moduleStorage.js index 1400379..7084b1c 100644 --- a/lib/moduleStorage.js +++ b/lib/moduleStorage.js @@ -36,7 +36,7 @@ var Store = prime({ invalidate: function(filename){ var module = this._storage[filename] if (module){ - module.invalid = true + module.invalidate() this.emit("invalidate", filename) } return this diff --git a/lib/output/amdOneFile.js b/lib/output/amdOneFile.js index 1fe2fe3..b7c2845 100644 --- a/lib/output/amdOneFile.js +++ b/lib/output/amdOneFile.js @@ -8,6 +8,7 @@ var errors = require('../errors') var Output = require('./') var SingleFile = require('./mixin/singleFile') var ModulesByID = require('./mixin/modulesByID') +var generateModuleVerbatim = require('./mixin/generateModuleVerbatim') var getDefineAST = util.getAST('amdOneFile-module') var getWrapperAST = util.getAST('amdOneFile-wrapper') @@ -45,34 +46,46 @@ var AMD = prime({ forOwn(modules, function(module){ if (module.err) return + if (!module.srcOut || !module.astOut){ - var file = module.getModuleFile(_path) - if (file.slice(0, 5) == '__oos'){ - self.emit("warn", new errors.OutOfScopeError(module.full)) - } + var file = module.getModuleFile(_path) + if (file.slice(0, 5) == '__oos'){ + self.emit("warn", new errors.OutOfScopeError(module.full)) + } - var ast = util.clone(module.ast) + var ast = util.clone(module.ast) - // replace require() calls - Output.replaceRequire(ast, module, function(dep){ - return dep.getModuleID(_path) - }) + // replace require() calls + Output.replaceRequire(ast, module, function(dep){ + return dep.getModuleID(_path) + }) + + var newAST = util.clone(defineAST.body[0]) + var args = newAST.expression['arguments'] - var newAST = util.clone(defineAST.body[0]) - var args = newAST.expression['arguments'] + // change module ID + args[0].value = module.getModuleID(_path) - // change module ID - args[0].value = module.getModuleID(_path) + // body of the define function + var body = args[1].body.body + // put the module JS in the define factory function + for (var i = 0; i < ast.body.length; i++){ + body.push(ast.body[i]) + } + + module.astOut = newAST + module.srcOut = generateModuleVerbatim(newAST, options) - // body of the define function - var body = args[1].body.body - // put the module JS in the define factory function - for (var i = 0; i < ast.body.length; i++){ - body.push(ast.body[i]) } // and add the define() function to the wrapper - wrapper.body.push(newAST) + wrapper.body.push({ + type: 'ExpressionStatement', + expression: { + type: 'Literal', + 'x-wrup-verbatim': module.srcOut + } + }) }) diff --git a/lib/output/browser.js b/lib/output/browser.js index ab7c36a..fcaceb5 100644 --- a/lib/output/browser.js +++ b/lib/output/browser.js @@ -6,6 +6,7 @@ var async = require('async') var util = require('../util') var singleFile = require('./mixin/singleFile') var Output = require('./') +var generateModuleVerbatim = require('./mixin/generateModuleVerbatim') var getWrapperAST = util.getAST('browser-wrapper') var getModuleAST = util.getAST('browser-module') @@ -52,24 +53,34 @@ var Browser = prime({ storage.each(function(module){ if (module.err) return + if (!module.astOut || !module.srcOut){ - var ast = util.clone(module.ast) + var ast = util.clone(module.ast) - // replace require() calls - Output.replaceRequire(ast, module) + // replace require() calls + Output.replaceRequire(ast, module) - // module key and value - var newAST = util.clone(moduleAST.body[0].declarations[0].init.properties[0]) - newAST.key.value = module.uid + // module key and value + var newAST = util.clone(moduleAST.body[0].declarations[0].init.properties[0]) + newAST.key.value = module.uid + + // put the module JS into the module function + var body = newAST.value.body.body + for (var i = 0; i < ast.body.length; i++){ + body.push(ast.body[i]) + } + + module.astOut = newAST + module.srcOut = generateModuleVerbatim(newAST, options) - // put the module JS into the module function - var body = newAST.value.body.body - for (var i = 0; i < ast.body.length; i++){ - body.push(ast.body[i]) } // and the module function in the "modules" object - properties.push(newAST) + properties.push({ + type: 'Property', + 'x-wrup-verbatim': module.srcOut + }) + }) // body where to place "require('0')" and "window['foo'] = require('1')" diff --git a/lib/output/mixin/generateModuleVerbatim.js b/lib/output/mixin/generateModuleVerbatim.js new file mode 100644 index 0000000..6247287 --- /dev/null +++ b/lib/output/mixin/generateModuleVerbatim.js @@ -0,0 +1,10 @@ +"use strict"; + +var escodegen = require('escodegen') +var esmangle = require('esmangle') + +function generateModuleVerbatim(ast, options){ + return escodegen.generate(ast) +} + +module.exports = generateModuleVerbatim diff --git a/lib/output/mixin/singleFile.js b/lib/output/mixin/singleFile.js index 92824f3..5c726a8 100644 --- a/lib/output/mixin/singleFile.js +++ b/lib/output/mixin/singleFile.js @@ -28,7 +28,8 @@ module.exports = prime({ compact: true, semicolons: false, parentheses: false - } : {} + } : {}, + verbatim: 'x-wrup-verbatim' } if (sourcemap){