Skip to content

Commit 2918b65

Browse files
full paths hotfix (#33)
* Use a Map for duplicates * Make it work with --fullPaths * unbork skipped test
1 parent 1034d3f commit 2918b65

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

index.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const wrapComment = require('wrap-comment')
66
const through = require('through2')
77
const convertSourceMap = require('convert-source-map')
88

9-
const kDuplicates = Symbol('duplicates')
10-
119
module.exports = function commonShake (b, opts) {
1210
if (typeof b !== 'object') {
1311
throw new Error('common-shakeify: must be used as a plugin, not a transform')
@@ -44,6 +42,7 @@ module.exports = function commonShake (b, opts) {
4442
}, opts)
4543

4644
opts.sourceMap = !!b._options.debug
45+
opts.fullPaths = !!b._options.fullPaths
4746

4847
addHooks()
4948
b.on('reset', addHooks)
@@ -57,11 +56,12 @@ function createStream (opts) {
5756

5857
const rows = new Map()
5958
const strings = new Map()
59+
const duplicates = new Map()
6060

6161
return through.obj(onfile, onend)
6262

6363
function onfile (row, enc, next) {
64-
const index = row.index
64+
const index = opts.fullPaths ? row.file : row.index
6565
let source = row.source
6666

6767
if (row.dedupe) {
@@ -89,9 +89,10 @@ function createStream (opts) {
8989
})
9090
analyzer.run(ast, index)
9191

92-
Object.keys(row.indexDeps).forEach((name) => {
93-
if (row.indexDeps[name]) {
94-
analyzer.resolve(index, name, row.indexDeps[name])
92+
const deps = opts.fullPaths ? row.deps : row.indexDeps
93+
Object.keys(deps).forEach((name) => {
94+
if (deps[name]) {
95+
analyzer.resolve(index, name, deps[name])
9596
}
9697
})
9798

@@ -227,14 +228,15 @@ function createStream (opts) {
227228
function commentify (str) {
228229
return wrapComment(`common-shake removed: ${str}`)
229230
}
230-
}
231231

232-
function addDuplicate (row, dupe) {
233-
if (!row[kDuplicates]) {
234-
row[kDuplicates] = []
232+
function addDuplicate (row, dupe) {
233+
if (!duplicates.has(row)) {
234+
duplicates.set(row, [dupe])
235+
} else {
236+
duplicates.get(row).push(dupe)
237+
}
238+
}
239+
function getDuplicates (row) {
240+
return duplicates.get(row) || []
235241
}
236-
row[kDuplicates].push(dupe)
237-
}
238-
function getDuplicates (row) {
239-
return row[kDuplicates] || []
240242
}

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,29 @@ test('dash-r node_modules', function (t) {
161161
t.end()
162162
}))
163163
})
164+
165+
// TODO fix this one
166+
test('dash-r node_modules with full paths', { skip: true }, function (t) {
167+
var b = browserify({
168+
fullPaths: true,
169+
entries: path.join(__dirname, 'dash-r-node-modules/app.js')
170+
})
171+
b.require('net-browserify-stub', { expose: 'net' })
172+
b.plugin(commonShake)
173+
174+
var bundle = b.bundle()
175+
bundle.on('error', t.fail)
176+
177+
bundle.pipe(fs.createWriteStream(
178+
path.join(__dirname, 'dash-r-node-modules/actual-fullpaths.js')
179+
))
180+
181+
bundle.pipe(concat(function (result) {
182+
t.is(
183+
result.toString('utf8'),
184+
fs.readFileSync(path.join(__dirname, 'dash-r-node-modules/expected-fullpaths.js'), 'utf8'),
185+
'dash-r'
186+
)
187+
t.end()
188+
}))
189+
})

0 commit comments

Comments
 (0)