rollup-plugin-visualizer
Advanced tools
Comparing version 3.2.3 to 3.3.0
@@ -16,2 +16,4 @@ #!/usr/bin/env node | ||
const TEMPLATE = require("../plugin/template-types"); | ||
const warn = require("../plugin/warn"); | ||
const JSON_VERSION = require("../plugin/version"); | ||
@@ -40,2 +42,6 @@ const argv = require("yargs") | ||
}) | ||
.option("sourcemap", { | ||
describe: "Provided files is sourcemaps", | ||
boolean: true | ||
}) | ||
.help().argv; | ||
@@ -45,3 +51,9 @@ | ||
const run = async (title, template, extraStylePath, filename, files) => { | ||
const runForPluginJson = async ( | ||
{ title, template, extraStylePath, filename }, | ||
files | ||
) => { | ||
if (extraStylePath) { | ||
warn("`--extra-style-path` will be removed in next major version"); | ||
} | ||
if (files.length === 0) { | ||
@@ -66,3 +78,10 @@ throw new Error("Empty file list"); | ||
for (const [, fileContent] of fileContents) { | ||
for (const [file, fileContent] of fileContents) { | ||
if (fileContent.version !== JSON_VERSION) { | ||
warn( | ||
`Version in ${file} is not supported (${fileContent.version}). Current version ${JSON_VERSION}. Skipping...` | ||
); | ||
continue; | ||
} | ||
if (fileContent.tree.name === "root") { | ||
@@ -79,3 +98,3 @@ tree.children = tree.children.concat(fileContent.tree.children); | ||
const data = { tree, links, nodes }; | ||
const data = { version: JSON_VERSION, tree, links, nodes }; | ||
@@ -94,11 +113,5 @@ const fileContent = await buildStats( | ||
run( | ||
argv.title, | ||
argv.template, | ||
argv.extraStylePath, | ||
argv.filename, | ||
listOfFiles | ||
).catch(err => { | ||
console.error(err.message); | ||
runForPluginJson(argv, listOfFiles).catch(err => { | ||
warn(err.message); | ||
process.exit(1); | ||
}); |
{ | ||
"name": "rollup-plugin-visualizer", | ||
"version": "3.2.3", | ||
"version": "3.3.0", | ||
"main": "plugin/index.js", | ||
@@ -57,2 +57,3 @@ "author": "Denis Bardadym <bardadymchik@gmail.com>", | ||
"rollup-plugin-postcss": "^2.0.3", | ||
"rollup-plugin-terser": "^5.1.2", | ||
"sass": "^1.22.7" | ||
@@ -59,0 +60,0 @@ }, |
@@ -7,3 +7,3 @@ "use strict"; | ||
const buildTree = (name, ids, getInitialModuleData, mapper) => { | ||
const buildTree = (name, modules, mapper) => { | ||
let tree = { | ||
@@ -14,9 +14,9 @@ name: "root", | ||
for (const id of ids) { | ||
for (const [id, { renderedLength }] of modules) { | ||
const mod = { renderedLength }; | ||
const name = id; | ||
const mod = getInitialModuleData(id); | ||
const uid = mapper.setValueByModuleId(id, mod); | ||
if (mod.size === 0) { | ||
if (mod.renderedLength === 0) { | ||
continue; | ||
@@ -43,23 +43,23 @@ } | ||
let newRoot = root; | ||
while (newRoot.children) { | ||
if (newRoot.children.length === 1) { | ||
newRoot = newRoot.children[0]; | ||
const pluginChildren = []; | ||
const otherChildren = []; | ||
for (const child of root.children || []) { | ||
if (child.name.startsWith(PLUGIN_PREFIX)) { | ||
pluginChildren.push(child); | ||
} else { | ||
const pluginChildren = []; | ||
const otherChildren = []; | ||
for (const child of newRoot.children) { | ||
if (child.name.startsWith(PLUGIN_PREFIX)) { | ||
pluginChildren.push(child); | ||
} else { | ||
otherChildren.push(child); | ||
} | ||
} | ||
if (otherChildren.length === 1) { | ||
newRoot = otherChildren[0]; | ||
newRoot.children = newRoot.children.concat(pluginChildren); | ||
} else { | ||
break; | ||
} | ||
otherChildren.push(child); | ||
} | ||
} | ||
if (otherChildren.length === 1 && otherChildren[0].children) { | ||
newRoot = otherChildren[0]; | ||
} | ||
while ( | ||
newRoot.children && | ||
newRoot.children.length === 1 && | ||
newRoot.children[0].children | ||
) { | ||
newRoot = newRoot.children[0]; | ||
} | ||
newRoot.children = newRoot.children.concat(pluginChildren); | ||
return newRoot; | ||
@@ -139,3 +139,4 @@ }; | ||
const skipModule = (id, node) => id.startsWith(PLUGIN_PREFIX) || node.isExternal; | ||
const skipModule = (id, node) => | ||
id.startsWith(PLUGIN_PREFIX) || node.isExternal; | ||
@@ -142,0 +143,0 @@ const removeCommonPrefix = (nodes, nodeIds) => { |
@@ -15,8 +15,13 @@ "use strict"; | ||
const TEMPLATE = require("./template-types"); | ||
const ModuleMapper = require("./module-mapper"); | ||
const buildStats = require("./build-stats"); | ||
const { buildTree, mergeTrees, addLinks, removeCommonPrefix } = require("./data"); | ||
const addMinifiedSizesToModules = require("./sourcemap"); | ||
const JSON_VERSION = require("./version"); | ||
const { | ||
buildTree, | ||
mergeTrees, | ||
addLinks, | ||
removeCommonPrefix | ||
} = require("./data"); | ||
const getSourcemapModules = require("./sourcemap"); | ||
const warn = require("./warn"); | ||
@@ -33,3 +38,2 @@ const WARN_SOURCEMAP_DISABLED = | ||
const useSourceMap = !!opts.sourcemap; | ||
const open = !!opts.open; | ||
@@ -40,8 +44,10 @@ const openOptions = opts.openOptions || {}; | ||
if (!TEMPLATE.includes(template)) { | ||
throw new Error(`Unknown template type ${template}`); | ||
throw new Error(`Unknown template type ${template}. Known: ${TEMPLATE}`); | ||
} | ||
let extraStylePath = opts.extraStylePath; | ||
if (opts.styleOverridePath) { | ||
console.warn("[rollup-plugin-visualizer] `styleOverridePath` was renamed to `extraStylePath`"); | ||
if ("styleOverridePath" in opts) { | ||
warn( | ||
"`styleOverridePath` was renamed to `extraStylePath`, but will be removed in next major version" | ||
); | ||
extraStylePath = opts.styleOverridePath; | ||
@@ -56,3 +62,3 @@ } | ||
async generateBundle(outputOptions, outputBundle) { | ||
if (useSourceMap && !outputOptions.sourcemap) { | ||
if (opts.sourcemap && !outputOptions.sourcemap) { | ||
this.warn(WARN_SOURCEMAP_DISABLED); | ||
@@ -69,20 +75,20 @@ } | ||
if (useSourceMap) { | ||
let tree; | ||
if (opts.sourcemap) { | ||
if (!bundle.map) { | ||
this.warn(WARN_SOURCEMAP_MISSING(id)); | ||
} | ||
await addMinifiedSizesToModules(bundle); | ||
} | ||
const getInitialModuleData = id => { | ||
const mod = bundle.modules[id]; | ||
const modules = await getSourcemapModules( | ||
id, | ||
bundle, | ||
outputOptions.dir || path.dirname(outputOptions.file) | ||
); | ||
return { | ||
size: useSourceMap ? mod.minifiedSize || 0 : mod.renderedLength, | ||
originalSize: mod.originalLength | ||
}; | ||
}; | ||
tree = buildTree(id, Object.entries(modules), mapper); | ||
} else { | ||
tree = buildTree(id, Object.entries(bundle.modules), mapper); | ||
} | ||
const tree = buildTree(id, Object.keys(bundle.modules), getInitialModuleData, mapper); | ||
roots.push(tree); | ||
@@ -95,3 +101,8 @@ } | ||
addLinks(bundle.facadeModuleId, this.getModuleInfo.bind(this), links, mapper); | ||
addLinks( | ||
bundle.facadeModuleId, | ||
this.getModuleInfo.bind(this), | ||
links, | ||
mapper | ||
); | ||
} | ||
@@ -112,7 +123,13 @@ | ||
const data = { tree, nodes, links }; | ||
const data = { version: JSON_VERSION, tree, nodes, links }; | ||
const fileContent = json | ||
? JSON.stringify(data, null, 2) | ||
: await buildStats(title, data, template, extraStylePath, chartParameters); | ||
: await buildStats( | ||
title, | ||
data, | ||
template, | ||
extraStylePath, | ||
chartParameters | ||
); | ||
@@ -119,0 +136,0 @@ await mkdir(path.dirname(filename)); |
"use strict"; | ||
const generate = require("nanoid/non-secure/generate"); | ||
const warn = require("./warn"); | ||
@@ -25,3 +26,9 @@ class ModuleMapper { | ||
if (uid in this.nodes) { | ||
console.warn("Override (probably this is a bug)", moduleId, uid, value, this.nodes[uid]); | ||
warn( | ||
"Override (probably this is a bug)", | ||
moduleId, | ||
uid, | ||
value, | ||
this.nodes[uid] | ||
); | ||
} | ||
@@ -28,0 +35,0 @@ this.nodes[uid] = value; |
"use strict"; | ||
const path = require("path"); | ||
const { SourceMapConsumer } = require("source-map"); | ||
const getBytesPerFileUsingSourceMap = (code, map) => { | ||
const lines = code.split(/[\r\n]/); | ||
const UNKNOWN_SOURCE = "\u0000unknown"; | ||
const bytesPerFile = {}; | ||
const getBytesPerFileUsingSourceMap = (bundleId, code, map, dir) => { | ||
const modules = Object.create(null); | ||
// For every byte in the minified code, do a sourcemap lookup. | ||
for (let line = 0; line < lines.length; line++) { | ||
for (let col = 0; col < lines[line].length; col++) { | ||
const result = map.originalPositionFor({ line: line + 1, column: col }); | ||
const source = result.source || "root"; | ||
if (!bytesPerFile[source]) { | ||
bytesPerFile[source] = 0; | ||
} | ||
bytesPerFile[source]++; | ||
let line = 1; | ||
let column = 0; | ||
for (let i = 0; i < code.length; i++, column++) { | ||
const { source } = map.originalPositionFor({ | ||
line, | ||
column | ||
}); | ||
const id = | ||
source == null | ||
? `${UNKNOWN_SOURCE}-${bundleId}` | ||
: path.resolve(dir, source); | ||
modules[id] = modules[id] || { renderedLength: 0 }; | ||
modules[id].renderedLength += 1; | ||
if (code[i] === "\n") { | ||
line += 1; | ||
column = -1; | ||
} | ||
} | ||
return Object.keys(bytesPerFile).map(file => ({ | ||
file: path.resolve(file), | ||
bytes: bytesPerFile[file] | ||
})); | ||
}; | ||
// Given a file C:/path/to/file/on/filesystem.js | ||
// - remove extension | ||
// - strip filesystem root | ||
// - return path segments, starting from the tail and working backwards | ||
// segments('C:/path/to/file/on/filesystem.js') === ['filesystem', 'on', 'file', 'to', 'path'] | ||
const segments = filepath => { | ||
const parsed = path.parse(filepath); | ||
const dirWithoutRoot = parsed.dir.substring(parsed.root.length); | ||
return dirWithoutRoot | ||
.split(path.sep) | ||
.concat(parsed.name) | ||
.reverse(); | ||
return modules; | ||
}; | ||
// Adds a .minifiedSize property to each module in the bundle (using sourcemap data) | ||
// If the minified size could not be computed, no property is added. | ||
// Module id are mapped to sources by finding the best match. | ||
// Matching is done by removing the file extensions and comparing path segments | ||
const addMinifiedSizesToModules = bundle => { | ||
const findBestMatchingModule = filename => { | ||
const filenameSegments = segments(filename); | ||
for (let i = 1; i <= filenameSegments.length; i++) { | ||
const leftVals = filenameSegments.slice(0, i); | ||
const matches = Object.keys(bundle.modules).filter(id => { | ||
const moduleSegments = segments(id); | ||
const rightVals = moduleSegments.slice(0, i); | ||
if (rightVals.length !== leftVals.length) { | ||
return false; | ||
} | ||
return rightVals.every((rightVal, i) => rightVal === leftVals[i]); | ||
}); | ||
if (matches.length === 1) { | ||
return bundle.modules[matches[0]]; | ||
} | ||
} | ||
return null; | ||
}; | ||
return SourceMapConsumer.with(bundle.map, null, map => { | ||
const fileSizes = getBytesPerFileUsingSourceMap(bundle.code, map); | ||
fileSizes.forEach(tuple => { | ||
const module = findBestMatchingModule(tuple.file); | ||
if (module) { | ||
module.minifiedSize = tuple.bytes; | ||
} | ||
}); | ||
const getSourcemapModules = (id, { map, code }, dir) => { | ||
return SourceMapConsumer.with(map, null, map => { | ||
return getBytesPerFileUsingSourceMap(id, code, map, dir); | ||
}); | ||
}; | ||
module.exports = addMinifiedSizesToModules; | ||
module.exports = getSourcemapModules; |
@@ -41,3 +41,3 @@ # Rollup Plugin Visualizer | ||
`sourcemap` (boolean, default `false`) - Use sourcemaps to calculate sizes (e.g. after UglifyJs) | ||
`sourcemap` (boolean, default `false`) - Use sourcemaps to calculate sizes (e.g. after UglifyJs or Terser) | ||
@@ -48,3 +48,3 @@ `open` (boolean, default `false`) - Open generated file in default user agent | ||
`extraStylePath` (string, default `undefined`) - Link your own css file to override or enhance the current templates | ||
~~`extraStylePath` (string, default `undefined`) - Link your own css file to override or enhance the current templates~~ **deprecated** | ||
@@ -51,0 +51,0 @@ `chartParameters.width` (number, default `undefined`) - Set svg viewBox width to this number |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
410172
19
21
12150