Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have multiple CWDs for each pattern #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lib/remapify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ var _ = require('lodash')
, processCwd = process.cwd()
, eachPattern
, eachFile
, eachCwd
, setLogger
, log
, transformAliases
, turnIntoArray

setLogger = function setLogger(verbose){
if (!verbose) log = _.noop
Expand All @@ -28,7 +30,6 @@ transformAliases = function transformAliases(b, results){
return total + count
}).valueOf()
, aliasMap = {}
, realPaths = []


if (expandedAliasesCount) {
Expand All @@ -41,9 +42,6 @@ transformAliases = function transformAliases(b, results){

// console.log(aliasNames[0])

realPaths.push(realityPath)
// b.require(realityPath)
// b.ignore(aliasGroup[aliasNames[0]])
aliasNames.forEach(function addToAliasMap(alias){
aliasMap[alias] = realityPath
})
Expand All @@ -63,8 +61,7 @@ transformAliases = function transformAliases(b, results){

// expects to be called with the context of a bundle
eachFile = function eachFile(b, file, pattern){
var cwd = pattern.cwd || processCwd
// we'll send the process-relative path to aliasify
var cwd = pattern.cwd
, aliasifyFilePath = './' + path.relative(process.cwd(), path.resolve(path.join(cwd, file)))
// we'll use the relative path to create our alias
, relativeFilePath = file.replace(cwd, '')
Expand Down Expand Up @@ -109,19 +106,42 @@ eachFile = function eachFile(b, file, pattern){
return expandedAliases
}

// expects to be called with the context of a bundle
eachPattern = function forEachPattern(b, pattern){
eachCwd = function eachCwd(b, pattern) {
// TODO: it would be nice to go back to this being async, but I'm not sure if
// browserify will cooperate
var realityFiles = glob.sync(pattern.src, pattern)
, parsedFiles = realityFiles.map(function getAliases(file){
return eachFile(b, file, pattern)
})

return {realityFiles: realityFiles, aliases: parsedFiles}
}

turnIntoArray = function turnIntoArray(value) {
// Browserify Command Line bug
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm… is this because of minimist? Is there an issue number we can reference?

if (value._)
return value._
else if (_.isArray(value))
return value
else
return [value]
}

// expects to be called with the context of a bundle
eachPattern = function forEachPattern(b, pattern){
var cwds = turnIntoArray(pattern.cwd || processCwd)

// Does a reverse concat, so the first CWD comes at the end
, result = cwds.reduce(function getCwds(acc, cwd) {
var cwdAlias = eachCwd(b, _.extend(pattern, {cwd: cwd}))
return {realityFiles: cwdAlias.realityFiles.concat(acc.realityFiles)
, aliases: cwdAlias.aliases.concat(acc.aliases)}
}, {realityFiles: [], aliases: []})

// emit the files we found and their alias mappings as one object
b.emit('remapify:files', realityFiles, _.assign.apply(_, parsedFiles), pattern)
b.emit('remapify:files', result.realityFiles, _.assign.apply(_, result.aliases), pattern)

return {pattern: pattern, aliases: parsedFiles}
return {pattern: pattern, aliases: result.aliases}
}

module.exports = function remapify(b, options){
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/target2/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function(){return 'a thing'}
40 changes: 40 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ describe('remapify', function(){
}])
})

it('looks for files in multiple directories, in precedence', function(done){
b.on('remapify:files', function(files, expandedAliases){
expandedAliases.should.contain.keys(
'a.js',
'b.js'
)
expandedAliases['a.js'].split(path.sep).join('/').should.equal('./test/fixtures/target2/a.js')
expandedAliases['b.js'].split(path.sep).join('/').should.equal('./test/fixtures/target/b.js')

b.emit.should.not.have.been.calledWith('error')

done()
})

plugin(b, [{
src: '**/*.js'
, cwd: ['./test/fixtures/target2', './test/fixtures/target']
}])
})

it('multiple cwds works when passing in an array from command line', function(done){
b.on('remapify:files', function(files, expandedAliases){
expandedAliases.should.contain.keys(
'a.js',
'b.js'
)
expandedAliases['a.js'].split(path.sep).join('/').should.equal('./test/fixtures/target2/a.js')
expandedAliases['b.js'].split(path.sep).join('/').should.equal('./test/fixtures/target/b.js')

b.emit.should.not.have.been.calledWith('error')

done()
})

plugin(b, [{
src: '**/*.js'
, cwd: {'_': ['./test/fixtures/target2', './test/fixtures/target'] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm… I thought the ideal here was an array for cwd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does accept an array, but I'm also accepting a map with one key. This is because browserify seems to call remapify with the above arguments, if called as follows from the command line:

browserify -p [ reactify --cwd ['/directory1' '/directory2'] ]

I have no idea if this is documented to work like this, or if it's random, but it does this consistently.

Thus we accept 4 types of arguments for cwd

  • null / undefined => processCwd
  • a string => the actual CWD
  • an array
  • the above structure from command line

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. That's gotta be coming from minimist, and is likely a bug in browserify. You might want to submit an issue there and reference that in the code comment.

If we're going to support this hack, mind adding a test that shows that just an array works too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There actually is a test. It's in the previous commit?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha – my bad. Sorry!

}])
})

it('works with non-standard extensions', function(done){
// setup
b._extensions = b._extensions.concat('.coffee')
Expand Down