-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hapi plugin to use webpack-dev-middleware
- Loading branch information
Showing
10 changed files
with
945 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/electrode-archetype-react-app-dev/lib/module-processor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Take webpack Stat JSON output and group modules by npm packages, | ||
* noting duplicates if there are any. | ||
*/ | ||
|
||
const Path = require("path"); | ||
|
||
const tildaSep = "/~/"; | ||
const nmSep = "/node_modules/"; | ||
|
||
// match @namespace/packageName/file | ||
const atModRegex = new RegExp(`([^/]+/[^/]+)/(.+)`); | ||
// match packageName/file | ||
const modRegex = new RegExp(`([^/]+)/(.+)`); | ||
|
||
class ModuleProcessor { | ||
constructor(statJson) { | ||
this.statJson = statJson; | ||
this.makeModulesByName(); | ||
} | ||
|
||
makeModulesByName() { | ||
const mbn = (this.modulesByName = {}); | ||
const obj = this.statJson; | ||
|
||
const filterLoaders = name => name.replace(/.*!/, ""); | ||
|
||
if (obj.modules) { | ||
obj.modules.forEach(module => { | ||
mbn[filterLoaders(module.name)] = module; | ||
}); | ||
} else if (obj.chunks) { | ||
obj.chunks.forEach(chunk => { | ||
if (chunk.modules) { | ||
chunk.modules.forEach(module => { | ||
mbn[filterLoaders(module.name)] = module; | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return mbn; | ||
} | ||
|
||
_splitPathName(name) { | ||
const pkgs = name.indexOf(tildaSep) < 0 ? [name] : name.split(tildaSep).splice(1); | ||
const n = pkgs.pop(); | ||
const match = n.match(n.startsWith("@") ? atModRegex : modRegex); | ||
|
||
if (match) { | ||
return { | ||
name: match[1], | ||
parents: pkgs, | ||
file: match[2] | ||
}; | ||
} else { | ||
return { | ||
name, | ||
parents: pkgs, | ||
file: "" | ||
}; | ||
} | ||
} | ||
/* | ||
* | ||
* { | ||
* packageName: { | ||
* $: { | ||
* modules: { | ||
* filename: { | ||
* chunks: [], | ||
* size: 123 | ||
* } | ||
* }, | ||
* parents: { | ||
* } | ||
* } | ||
* } | ||
* } | ||
* @returns {{}|*} | ||
*/ | ||
makeModulesByPackage() { | ||
const byPkg = (this.modulesByPackage = {}); | ||
this.totalSize = 0; | ||
Object.keys(this.modulesByName).forEach(name => { | ||
// Ignore "ignored" modules i.e. node shims etc. | ||
if (name.indexOf("(ignored)") > -1) { | ||
return; | ||
} | ||
|
||
const split = this._splitPathName(name); | ||
const pkg = byPkg[split.name] || (byPkg[split.name] = { size: 0 }); | ||
const getVersion = parents => { | ||
try { | ||
if (split.name !== ".") { | ||
return require(Path.resolve(parents.join(nmSep), split.name, "package.json")).version; // eslint-disable-line | ||
} else { | ||
return "-"; | ||
} | ||
} catch (e) { | ||
return "?"; | ||
} | ||
}; | ||
const getByParents = parents => { | ||
const parentId = `\$${parents.join(":")}`; | ||
if (pkg[parentId]) { | ||
return pkg[parentId]; | ||
} else { | ||
return (pkg[parentId] = { | ||
parents, | ||
name: split.name, | ||
size: 0, | ||
version: getVersion(parents), | ||
modules: {} | ||
}); | ||
} | ||
}; | ||
const byParents = getByParents(split.parents); | ||
const module = this.modulesByName[name]; | ||
pkg.size += module.size; | ||
byParents.size += module.size; | ||
this.totalSize += module.size; | ||
byParents.modules[split.file] = module; | ||
}); | ||
|
||
return this.modulesByPackage; | ||
} | ||
} | ||
|
||
module.exports = ModuleProcessor; |
Oops, something went wrong.