Skip to content

Commit

Permalink
Use .index/.indexDeps for module resolution (#30)
Browse files Browse the repository at this point in the history
* Improve handling browserify -r

Test needs work. Don't want to add that net-browserify-stub dep

It seems to not like it when you -r something from node_modules; it was OK
when you -r with a path

* move new dash-r test to its own folder

* Use .index/.indexDeps for module resolution
  • Loading branch information
goto-bus-stop authored Jun 29, 2019
1 parent 1325412 commit 009626b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function createStream (opts) {
return through.obj(onfile, onend)

function onfile (row, enc, next) {
const file = row.file
const index = row.index
let source = row.source

if (row.dedupe) {
Expand All @@ -70,7 +70,7 @@ function createStream (opts) {
// Later on, we'll merge the used declarations together, so everything still
// works if dependencies of different copies of the deduped module use
// different parts of that module.
const deduped = rows.get(row.dedupe) || rows.get(row.dedupeIndex)
const deduped = rows.get(row.dedupeIndex)
if (deduped) {
addDuplicate(deduped, row)
source = deduped.source
Expand All @@ -83,24 +83,24 @@ function createStream (opts) {
const string = transformAst(source, {
locations: true,
ecmaVersion: 9,
inputFilename: file
inputFilename: row.file
}, (node) => {
if (node.type === 'Program') ast = node
})
analyzer.run(ast, file)
analyzer.run(ast, index)

Object.keys(row.deps).forEach((name) => {
if (row.deps[name]) {
analyzer.resolve(file, name, row.deps[name])
Object.keys(row.indexDeps).forEach((name) => {
if (row.indexDeps[name]) {
analyzer.resolve(index, name, row.indexDeps[name])
}
})

if (row.entry) {
analyzer.getModule(file).forceExport()
analyzer.getModule(index).forceExport()
}

rows.set(file, row)
strings.set(file, string)
rows.set(index, row)
strings.set(index, string)

next()
}
Expand Down Expand Up @@ -159,7 +159,7 @@ function createStream (opts) {
}
if (dupes.length > 0) {
return dupes.some((dupe) => {
const m = analyzer.modules.get(dupe.file)
const m = analyzer.modules.get(dupe.index)
return m && m.isUsed(name)
})
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"babelify": "^8.0.0",
"browserify": "^16.1.1",
"concat-stream": "^1.6.1",
"net-browserify-stub": "0.0.1",
"tape": "^4.9.0",
"uglify-js": "^3.3.15"
}
Expand Down
2 changes: 2 additions & 0 deletions test/dash-r-node-modules/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('net')
module.exports = 'app2'
30 changes: 30 additions & 0 deletions test/dash-r-node-modules/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
require('net')
module.exports = 'app2'

},{"net":"net"}],"net":[function(require,module,exports){
exports.createServer = exports.Server = exports.Socket = function () {
throw new Error('Not implemented in the browser');
};

exports.connect = exports.createConnection = function () {
throw new Error('Not implemented here but check emersion/net-browserify');
};

exports.isIP = function (input) {
if (exports.isIPv4(input)) {
return 4;
} else if (exports.isIPv6(input)) {
return 6;
} else {
return 0;
}
};
exports.isIPv4 = function(input) {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(input);
};
exports.isIPv6 = function(input) {
return /^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))$/.test(input);
};

},{}]},{},[1]);
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,27 @@ test('dash-r', function (t) {
t.end()
}))
})

test('dash-r node_modules', function (t) {
var b = browserify({
entries: path.join(__dirname, 'dash-r-node-modules/app.js')
})
b.require('net-browserify-stub', { expose: 'net' })
b.plugin(commonShake)

var bundle = b.bundle()
bundle.on('error', t.fail)

bundle.pipe(fs.createWriteStream(
path.join(__dirname, 'dash-r-node-modules/actual.js')
))

bundle.pipe(concat(function (result) {
t.is(
result.toString('utf8'),
fs.readFileSync(path.join(__dirname, 'dash-r-node-modules/expected.js'), 'utf8'),
'dash-r'
)
t.end()
}))
})

0 comments on commit 009626b

Please sign in to comment.