closure-calculate-chunks
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -185,4 +185,9 @@ #!/usr/bin/env node | ||
} else { | ||
process.stdout.write(JSON.stringify(chunkGraph.getClosureCompilerFlags(namingStyle), null, 2) + '\n'); | ||
try { | ||
process.stdout.write(JSON.stringify(chunkGraph.getClosureCompilerFlags(namingStyle), null, 2) + '\n'); | ||
} catch (e) { | ||
process.stderr.write(`Error: ${e.message}\n`); | ||
process.exitCode = 1; | ||
} | ||
} | ||
}); |
@@ -5,2 +5,3 @@ import graphlib from 'graphlib'; | ||
import normalizeGraph from './normalize-graph.js'; | ||
import fs from 'fs/promises'; | ||
import {NAMING_STYLE, outputChunkNaming} from './chunk-naming.js'; | ||
@@ -17,2 +18,10 @@ | ||
#entrypoint = undefined; | ||
/** @type {!Map<string, !Set<string>>} */ | ||
#sourceReferences = new Map(); | ||
/** @type {!{readFile:(function(string,string):!Promise<string>)}} */ | ||
static fsAdapter = { | ||
readFile(filepath, encoding) { | ||
return fs.readFile(filepath, encoding); | ||
} | ||
}; | ||
@@ -22,6 +31,8 @@ /** | ||
* @param {!graphlib.Graph=} graph | ||
* @param {!Map<string, !Set<string>>=} sourceReferences | ||
*/ | ||
constructor(entrypoint, graph = new graphlib.Graph({directed: true, compound: false})) { | ||
constructor(entrypoint, graph = new graphlib.Graph({directed: true, compound: false}), sourceReferences = new Map()) { | ||
this.#graph = graph; | ||
this.#entrypoint = entrypoint; | ||
this.#sourceReferences = sourceReferences; | ||
} | ||
@@ -138,4 +149,6 @@ | ||
const relativePathName = path.relative(process.cwd(), chunkName); | ||
const referencingChunks = Array.from(this.#sourceReferences.get(chunkName) || []); | ||
errors.push(`Chunk entrypoint ${relativePathName} not found in chunk sources. ` + | ||
`Ensure that all imports of ${relativePathName} are dynamic.`); | ||
`Ensure that all imports of ${relativePathName} are dynamic. ` + | ||
`Referenced in: ${JSON.stringify(referencingChunks, null, 2)}`); | ||
} | ||
@@ -184,6 +197,16 @@ sourceCount += chunk.sources.length; | ||
googDepsMap) { | ||
const depFinder = new DepsFinder(packageJsonEntryNames, baseDirectory, googBasePath, googDepsMap); | ||
const depFinder = new DepsFinder(packageJsonEntryNames, baseDirectory, googBasePath, googDepsMap, this.fsAdapter); | ||
let graphData = await depFinder.fromEntryPoints(entrypoints, manualEntrypoints); | ||
let chunkGraph = new this(graphData.entrypoint, graphData.graph); | ||
const sourceReferences = new Map(); | ||
depFinder.fileDependencies.forEach((node, filename) => { | ||
node.deps.forEach((dep) => { | ||
let references = sourceReferences.get(dep); | ||
if (!references) { | ||
references = new Set(); | ||
sourceReferences.set(dep, references); | ||
} | ||
references.add(filename); | ||
}); | ||
}); | ||
let chunkGraph = new this(graphData.entrypoint, graphData.graph, sourceReferences); | ||
const dependenciesToHoist = normalizeGraph(chunkGraph.entrypoint, chunkGraph.graph); | ||
@@ -199,3 +222,3 @@ let graphNeedsRebuilt = false; | ||
graphData = await depFinder.fromEntryPoints(entrypoints, manualEntrypoints); | ||
chunkGraph = new this(graphData.entrypoint, graphData.graph); | ||
chunkGraph = new this(graphData.entrypoint, graphData.graph, sourceReferences); | ||
normalizeGraph(chunkGraph.entrypoint, chunkGraph.graph); | ||
@@ -202,0 +225,0 @@ } |
@@ -45,2 +45,8 @@ import {Parser} from 'acorn'; | ||
#fileDepsCache = new Map(); | ||
/** @type {{readFile:(function(string,string):!Promise<string>)}=} */ | ||
#fs = { | ||
readFile(filepath, encoding) { | ||
return fs.readFile(filepath, encoding); | ||
} | ||
}; | ||
@@ -52,4 +58,5 @@ /** | ||
* @param {Map<string, string>=} googDepsMap map of closure library provided namespace to filepath | ||
* @param {{readFile:(function(string,string):!Promise<string>)}=} fsAdapter | ||
*/ | ||
constructor(packageJsonEntryNames, baseDirectory, googBasePath, googDepsMap) { | ||
constructor(packageJsonEntryNames, baseDirectory, googBasePath, googDepsMap, fsAdapter) { | ||
this.#packageJsonEntryNames = packageJsonEntryNames; | ||
@@ -59,4 +66,12 @@ this.#baseDirectory = baseDirectory; | ||
this.#googDepsMap = googDepsMap; | ||
if (fsAdapter) { | ||
this.#fs = fsAdapter; | ||
}; | ||
} | ||
/** @type {!Map<string, !GraphNode>} */ | ||
get fileDependencies() { | ||
return this.#fileDepsCache; | ||
} | ||
/** @param {!Map<string, !Array<string>>} sourcesToHoist */ | ||
@@ -156,3 +171,3 @@ addDependenciesToHoist(sourcesToHoist) { | ||
}, | ||
// goog.require, goog.requiireType, goog.provide, goog.module | ||
// goog.require, goog.requireType, goog.provide, goog.module | ||
CallExpression: (node) => { | ||
@@ -231,3 +246,3 @@ // require('module') | ||
if (!filepath.endsWith('.json')) { | ||
const fileContents = await fs.readFile(filepath, 'utf8'); | ||
const fileContents = await this.#fs.readFile(filepath, 'utf8'); | ||
try { | ||
@@ -257,3 +272,3 @@ const ast = Parser.parse(fileContents, { | ||
transientDeps.unshift(parsedDeps[i]); | ||
transientDepInfo =new GraphNode( | ||
transientDepInfo = new GraphNode( | ||
transientDepInfo.name, | ||
@@ -260,0 +275,0 @@ new Set(transientDeps), |
import fs from 'fs'; | ||
import graphlib from 'graphlib'; | ||
import path from 'path'; | ||
import resolve from 'resolve'; | ||
import Module from 'module'; | ||
import url from 'url'; | ||
import ChunkGraph from './chunk-graph.js'; | ||
@@ -90,18 +91,4 @@ import {NAMING_STYLE, outputChunkNaming} from './chunk-naming.js'; | ||
const basedir = path.dirname(import.meta.url).replace(/^file:\/\//, ''); | ||
function requireResolve(moduleId) { | ||
return new Promise((res, rej) => { | ||
resolve(moduleId, { | ||
basedir, | ||
includeCoreModules: false, | ||
preserveSymlinks: true | ||
}, | ||
(err, absFilepath) => { | ||
if (err) { | ||
return rej(err); | ||
} | ||
res(absFilepath); | ||
}); | ||
}); | ||
} | ||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); | ||
const require = Module.createRequire(import.meta.url); | ||
@@ -119,4 +106,4 @@ /** | ||
'entrypoint': getOutputChunkName(chunkGraph.entrypoint), | ||
'sigma_path': await requireResolve('sigma/build/sigma.min.js'), | ||
'sigma_force_atlas_path': await requireResolve('sigma/build/plugins/sigma.layout.forceAtlas2.min.js'), | ||
'sigma_path': require.resolve('sigma/build/sigma.min.js'), | ||
'sigma_force_atlas_path': require.resolve('sigma/build/plugins/sigma.layout.forceAtlas2.min.js'), | ||
'load_graph': JSON.stringify(convertGraph(chunkGraph.graph, getOutputChunkName)), | ||
@@ -127,3 +114,3 @@ 'dep_graph': JSON.stringify(convertGraph(chunkGraph.toDependencyGraph(), getOutputChunkName)) | ||
const templateContents = await new Promise((resolve, reject) => { | ||
fs.readFile(path.join(basedir, 'visualize-graph.html'), 'utf8', (err, contents) => { | ||
fs.readFile(path.join(__dirname, 'visualize-graph.html'), 'utf8', (err, contents) => { | ||
if (err) { | ||
@@ -130,0 +117,0 @@ return reject(err); |
{ | ||
"name": "closure-calculate-chunks", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Analyze dependencies from entry points and split code for closure-compiler", | ||
@@ -5,0 +5,0 @@ "bin": { |
Sorry, the diff of this file is not supported yet
73029
1147
7