Skip to content

Commit

Permalink
Start working on #9 for browser and amd-combined.
Browse files Browse the repository at this point in the history
  • Loading branch information
arian committed Dec 27, 2013
1 parent 197439f commit 9f6574d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 32 deletions.
10 changes: 10 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var Module = prime({
this.ready = false
this.err = false
this.invalid = false
this.astOut = null
this.srcOut = ''

this.moduleIDs = {}
},
Expand All @@ -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'
},
Expand Down
2 changes: 1 addition & 1 deletion lib/moduleStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 32 additions & 19 deletions lib/output/amdOneFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}
})

})

Expand Down
33 changes: 22 additions & 11 deletions lib/output/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')"
Expand Down
10 changes: 10 additions & 0 deletions lib/output/mixin/generateModuleVerbatim.js
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/output/mixin/singleFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = prime({
compact: true,
semicolons: false,
parentheses: false
} : {}
} : {},
verbatim: 'x-wrup-verbatim'
}

if (sourcemap){
Expand Down

0 comments on commit 9f6574d

Please sign in to comment.