rollup-dependency-tree
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -18,20 +18,23 @@ "use strict"; | ||
exports.dependenciesForTree = dependenciesForTree; | ||
function addChunk(chunk, result, opts, dynamicImport) { | ||
if (!opts || !opts.filter || opts.filter({ chunk: chunk, dynamicImport: dynamicImport })) { | ||
result.add(chunk); | ||
function addChunk(ctx, result, opts) { | ||
if (!opts || !opts.filter || opts.filter(ctx)) { | ||
result.add(ctx.chunk); | ||
} | ||
} | ||
var visitKey = function (ctx) { return ctx.chunk.fileName + ':' + ctx.dynamicImport; }; | ||
function dependenciesForTrees(result, visited, chunkToResolve, allChunks, dynamicImport, opts) { | ||
if (opts && opts.walk && !opts.walk({ chunk: chunkToResolve, dynamicImport: dynamicImport })) { | ||
var ctx = { chunk: chunkToResolve, dynamicImport: dynamicImport }; | ||
if (opts && opts.walk && !opts.walk(ctx)) { | ||
return; | ||
} | ||
visited.add(chunkToResolve); | ||
addChunk(chunkToResolve, result, opts, dynamicImport); | ||
// need to hand the case where we come across a chunk as a dynamic import and static import | ||
visited.add(visitKey(ctx)); | ||
addChunk(ctx, result, opts); | ||
chunkToResolve.imports.concat(chunkToResolve.dynamicImports).forEach(function (fileName) { | ||
var chunk = allChunks.find(function (c) { return c.fileName === fileName; }); | ||
if (chunk && !visited.has(chunk)) { // avoid cycles | ||
var dynamicImport_1 = chunkToResolve.imports.indexOf(chunk.fileName) < 0; | ||
dependenciesForTrees(result, visited, chunk, allChunks, dynamicImport_1, opts); | ||
var dynamicImport = chunkToResolve.imports.indexOf(chunk.fileName) < 0; | ||
if (chunk && !visited.has(visitKey({ chunk: chunk, dynamicImport: dynamicImport }))) { // avoid cycles | ||
dependenciesForTrees(result, visited, chunk, allChunks, dynamicImport, opts); | ||
} | ||
}); | ||
} |
@@ -7,2 +7,3 @@ "use strict"; | ||
var index2_1 = require("./index2"); | ||
var bar_1 = require("./bar"); | ||
describe('test suite', function () { | ||
@@ -15,4 +16,2 @@ it('dependenciesForTree should handle sveltejs/realworld example', function () { | ||
}); | ||
}); | ||
describe('test suite', function () { | ||
it('dependenciesForTree should handle sapper css index2 test', function () { | ||
@@ -24,2 +23,8 @@ var rollupData = index2_1.default; | ||
}); | ||
it('dependenciesForTree should handle sapper css bar test', function () { | ||
var rollupData = bar_1.default; | ||
var entryChunk = rollupData.find(function (c) { return c.facadeModuleId && c.facadeModuleId.includes('client.js'); }); | ||
var result = index_1.dependenciesForTree(entryChunk, rollupData, { filter: function (ctx) { return ctx.dynamicImport; } }); | ||
chai_1.expect(Array.from(result).find(function (c) { return c.fileName === 'Title.9a7177bc.js'; })).to.be.ok; | ||
}); | ||
}); |
24
index.ts
@@ -25,3 +25,3 @@ import type { RenderedChunk } from 'rollup'; | ||
const result = new Set<RenderedChunk>(); | ||
const visited = new Set<RenderedChunk>(); | ||
const visited = new Set<string>(); | ||
dependenciesForTrees(result, visited, chunk, allChunks, false, opts); | ||
@@ -31,11 +31,13 @@ return result; | ||
function addChunk(chunk: RenderedChunk, result: Set<RenderedChunk>, opts: DependencyTreeOptions, dynamicImport: boolean) { | ||
if (!opts || !opts.filter || opts.filter({chunk, dynamicImport})) { | ||
result.add(chunk); | ||
function addChunk(ctx: DependencyTreeContext, result: Set<RenderedChunk>, opts: DependencyTreeOptions) { | ||
if (!opts || !opts.filter || opts.filter(ctx)) { | ||
result.add(ctx.chunk); | ||
} | ||
} | ||
const visitKey = (ctx: DependencyTreeContext) => ctx.chunk.fileName + ':' + ctx.dynamicImport; | ||
function dependenciesForTrees( | ||
result: Set<RenderedChunk>, | ||
visited: Set<RenderedChunk>, | ||
visited: Set<string>, | ||
chunkToResolve: RenderedChunk, | ||
@@ -46,11 +48,13 @@ allChunks: RenderedChunk[], | ||
if (opts && opts.walk && !opts.walk({chunk: chunkToResolve, dynamicImport})) { | ||
const ctx = {chunk: chunkToResolve, dynamicImport}; | ||
if (opts && opts.walk && !opts.walk(ctx)) { | ||
return; | ||
} | ||
visited.add(chunkToResolve); | ||
addChunk(chunkToResolve, result, opts, dynamicImport); | ||
// need to hand the case where we come across a chunk as a dynamic import and static import | ||
visited.add(visitKey(ctx)); | ||
addChunk(ctx, result, opts); | ||
chunkToResolve.imports.concat(chunkToResolve.dynamicImports).forEach(fileName => { | ||
let chunk = allChunks.find(c => c.fileName === fileName); | ||
if (chunk && !visited.has(chunk)) { // avoid cycles | ||
const dynamicImport = chunkToResolve.imports.indexOf(chunk.fileName) < 0; | ||
const dynamicImport = chunkToResolve.imports.indexOf(chunk.fileName) < 0; | ||
if (chunk && !visited.has(visitKey({chunk, dynamicImport}))) { // avoid cycles | ||
dependenciesForTrees(result, visited, chunk, allChunks, dynamicImport, opts); | ||
@@ -57,0 +61,0 @@ } |
{ | ||
"name": "rollup-dependency-tree", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Builds a dependency tree from a Rollup compilation result", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,4 +7,6 @@ import { expect } from 'chai'; | ||
import index2 from './index2'; | ||
import bar from './bar'; | ||
describe('test suite', () => { | ||
it('dependenciesForTree should handle sveltejs/realworld example', () => { | ||
@@ -16,5 +18,3 @@ const rollupData: OutputChunk[] = realworld as unknown as OutputChunk[]; | ||
}) | ||
}); | ||
describe('test suite', () => { | ||
it('dependenciesForTree should handle sapper css index2 test', () => { | ||
@@ -26,2 +26,10 @@ const rollupData: OutputChunk[] = index2 as unknown as OutputChunk[]; | ||
}) | ||
it('dependenciesForTree should handle sapper css bar test', () => { | ||
const rollupData: OutputChunk[] = bar as unknown as OutputChunk[]; | ||
const entryChunk = rollupData.find(c => c.facadeModuleId && c.facadeModuleId.includes('client.js')); | ||
const result = dependenciesForTree(entryChunk, rollupData, { filter: ctx => ctx.dynamicImport }); | ||
expect(Array.from(result).find(c => c.fileName === 'Title.9a7177bc.js')).to.be.ok; | ||
}) | ||
}); |
1027254
19
7706