common-shakeify
Advanced tools
Comparing version 0.3.1 to 0.3.2
57
index.js
@@ -8,2 +8,4 @@ 'use strict' | ||
const kDuplicates = Symbol('duplicates') | ||
module.exports = function commonShake (b, opts) { | ||
@@ -52,4 +54,21 @@ if (typeof b !== 'object') { | ||
const file = row.id | ||
let source = row.source | ||
if (row.dedupe) { | ||
// For modules that were deduped, attach the duplicates to the original row, | ||
// and pass the original source to the analyzer. | ||
// 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) | ||
if (deduped) { | ||
addDuplicate(deduped, row) | ||
source = deduped.source | ||
} else { | ||
return next(new Error(`Could not redupe module ${row.file}`)) | ||
} | ||
} | ||
let ast | ||
const string = transformAst(row.source, { locations: true }, (node) => { | ||
const string = transformAst(source, { locations: true }, (node) => { | ||
if (node.type === 'Program') ast = node | ||
@@ -87,3 +106,11 @@ }) | ||
const row = rows.get(key) | ||
const dupes = getDuplicates(row) | ||
// If this module was a duplicate of another module, | ||
// the original module will have been rewritten already. | ||
if (row.dedupe) { | ||
this.push(row) | ||
return | ||
} | ||
if (module.bailouts) { | ||
@@ -96,3 +123,3 @@ opts.onModuleBailout(module, module.bailouts) | ||
module.getDeclarations().forEach((decl) => { | ||
if (!module.isUsed(decl.name)) { | ||
if (!isUsed(decl.name)) { | ||
opts.onExportDelete(row.sourceFile || row.file, decl.name) | ||
@@ -104,3 +131,19 @@ remove(string, decl.ast) | ||
row.source = string.toString() | ||
this.push(row) | ||
// Check if a name was used in this module, or | ||
// in any of this module's deduped versions. | ||
function isUsed (name) { | ||
if (module.isUsed(name)) { | ||
return true | ||
} | ||
if (dupes.length > 0) { | ||
return dupes.some((dupe) => { | ||
const m = analyzer.modules.get(dupe.file) | ||
return m && m.isUsed(name) | ||
}) | ||
} | ||
return false | ||
} | ||
}) | ||
@@ -149,1 +192,11 @@ | ||
} | ||
function addDuplicate (row, dupe) { | ||
if (!row[kDuplicates]) { | ||
row[kDuplicates] = [] | ||
} | ||
row[kDuplicates].push(dupe) | ||
} | ||
function getDuplicates (row) { | ||
return row[kDuplicates] || [] | ||
} |
{ | ||
"name": "common-shakeify", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "browserify tree shaking plugin using @indutny common-shake", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25141
50
498
29