Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mgechev committed Sep 12, 2019
1 parent e2c01f5 commit cff525d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Expand Up @@ -3,5 +3,5 @@
"packages": [
"packages/*"
],
"version": "0.3.13"
"version": "0.4.0"
}
2 changes: 1 addition & 1 deletion packages/guess-ga/package.json
@@ -1,6 +1,6 @@
{
"name": "guess-ga",
"version": "0.3.13",
"version": "0.4.0",
"description": "Fetch structured data from Google Analytics",
"main": "dist/guess-ga/index.js",
"types": "dist/guess-ga/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/guess-parser/package.json
@@ -1,6 +1,6 @@
{
"name": "guess-parser",
"version": "0.3.13",
"version": "0.4.0",
"description": "Finds the route declarations in your application.",
"main": "dist/guess-parser/index.js",
"types": "dist/guess-parser/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/guess-webpack/package.json
@@ -1,6 +1,6 @@
{
"name": "guess-webpack",
"version": "0.3.13",
"version": "0.4.0",
"description": "Webpack plugins for the Machine Learning-driven bundler",
"main": "dist/guess-webpack/main.js",
"types": "dist/guess-webpack/index.d.ts",
Expand Down Expand Up @@ -28,7 +28,7 @@
"flat-cache": "^2.0.0",
"google-oauth2-node": "0.0.3",
"googleapis": "^43.0.0",
"guess-ga": "^0.3.13",
"guess-ga": "^0.4.0",
"lodash.template": "^4.4.0",
"rollup": "^1.0.0",
"rollup-plugin-hypothetical": "^2.1.0",
Expand Down
26 changes: 15 additions & 11 deletions packages/guess-webpack/src/prefetch-aot-plugin.ts
Expand Up @@ -81,7 +81,7 @@ const alterChunk = (
};

export class PrefetchAotPlugin {
logger = new Logger();
private logger = new Logger();
constructor(private _config: PrefetchAotPluginConfig) {
if (!_config.data) {
throw new Error('Page graph not provided');
Expand All @@ -106,6 +106,7 @@ export class PrefetchAotPlugin {
const res = getCompilationMapping(
compilation,
new Set(this._config.routes.map(r => stripExtension(r.modulePath))),
this.logger,
this._config.debug
);
mainName = res.mainName;
Expand Down Expand Up @@ -139,6 +140,7 @@ export class PrefetchAotPlugin {
};
}),
this._config.data,
this.logger,
!!this._config.debug
);

Expand Down Expand Up @@ -178,7 +180,7 @@ export class PrefetchAotPlugin {
c: PrefetchAotNeighbor
) => {
if (!c.chunk) {
this.logger.warn('Cannot find chunk name for', c, 'from route', route);
this.logger.debug('Cannot find chunk name for', c, 'from route', route);

return false;
}
Expand All @@ -192,13 +194,12 @@ export class PrefetchAotPlugin {
const chunkName = asset.name;
const route = chunkRoute[chunkName];
if (!route) {
this.logger.warn(
this.logger.debug(
`Cannot find the route "${route}" for chunk "${chunkName}"`
);
asset.callback();
return;
}
chunksLeft -= 1;

const neighbors = (newConfig[route] || [])
.map(generateNeighbors.bind(null, route, chunkName))
Expand Down Expand Up @@ -243,18 +244,21 @@ export class PrefetchAotPlugin {
newCode,
isMainChunk
).finally(asset.callback);

chunksLeft -= 1;
if (!chunksLeft) {
this.logger.info(
chalk.blue(
'\n\n\n馃敭 Guess.js introduced the following prefetching instructions:'
)
);
this.logger.info('\n\n' + table(tableOutput));
}
};

assetObserver.onAsset(handleAsset);
assetObserver.buffer.forEach(handleAsset);

this.logger.info(
chalk.blue(
'\n\n\n馃敭 Guess.js introduced the following prefetching instructions:'
)
);
this.logger.info('\n\n' + table(tableOutput));

callback();
}
}
5 changes: 5 additions & 0 deletions packages/guess-webpack/src/prefetch-plugin.ts
Expand Up @@ -12,11 +12,14 @@ import {
getCompilationMapping,
stripExtension
} from './utils';
import { Logger } from 'common/logger';

const template = require('lodash.template');
const ConcatSource = require('webpack-sources').ConcatSource;

export class PrefetchPlugin {
private logger = new Logger();

constructor(private _config: PrefetchPluginConfig) {
if (!_config.data) {
throw new Error('Page graph not provided');
Expand All @@ -31,6 +34,7 @@ export class PrefetchPlugin {
const res = getCompilationMapping(
compilation,
new Set(this._config.routes.map(r => stripExtension(r.modulePath))),
this.logger,
this._config.debug
);
mainName = res.mainName;
Expand All @@ -55,6 +59,7 @@ export class PrefetchPlugin {
};
}),
this._config.data,
this.logger,
!!this._config.debug
);
Object.keys(initialGraph).forEach(c => {
Expand Down
36 changes: 21 additions & 15 deletions packages/guess-webpack/src/utils.ts
@@ -1,6 +1,7 @@
import { PrefetchConfig, BundleEntryGraph } from './declarations';
import { Graph, RoutingModule } from '../../common/interfaces';
import { join } from 'path';
import { Logger } from 'common/logger';

export const defaultPrefetchConfig: PrefetchConfig = {
'4g': 0.15,
Expand All @@ -12,6 +13,7 @@ export const defaultPrefetchConfig: PrefetchConfig = {
const validateInput = (
routes: RoutingModule[],
graph: Graph,
logger: Logger,
debug: boolean
) => {
const routesInReport = new Set();
Expand All @@ -24,7 +26,7 @@ const validateInput = (
.filter(x => !routesInReport.has(x))
.forEach(r => {
if (debug) {
console.warn(
logger.debug(
`The route ${r} is not present in the report or in the route declarations`
);
}
Expand All @@ -34,9 +36,10 @@ const validateInput = (
export const buildMap = (
routes: RoutingModule[],
graph: Graph,
logger: Logger,
debug: boolean
): BundleEntryGraph => {
validateInput(routes, graph, debug);
validateInput(routes, graph, logger, debug);
const result: BundleEntryGraph = {};
const routeFile = {} as { [key: string]: string };
routes.forEach(r => {
Expand Down Expand Up @@ -97,6 +100,7 @@ export interface JSCompilation {
export const getCompilationMapping = (
compilation: Compilation,
entryPoints: Set<string>,
logger: Logger,
debug?: boolean
): { mainName: string | null; fileChunk: { [path: string]: string } } => {
const fileChunk: { [path: string]: string } = {};
Expand Down Expand Up @@ -125,7 +129,15 @@ export const getCompilationMapping = (
return;
}
if (c.initial) {
mainName = c.files.filter(f => f.endsWith('.js')).pop()!;
const pickers = [
(f: string) => f.startsWith('main'),
(f: string) => f.startsWith('runtime'),
(f: string) => f.startsWith('vendor'),
(f: string) => f.endsWith('.js'),
]
while (!mainName && pickers.length) {
mainName = c.files.filter(pickers.shift()!).pop()!;
}
}
if (c.modules && c.modules.length) {
const existingEntries = c.modules.filter(m => {
Expand All @@ -136,26 +148,20 @@ export const getCompilationMapping = (
return entryPoints.has(path);
});
if (existingEntries.length > 1) {
if (debug) {
console.warn(
'There are more than two entry points associated with chunk',
c.files[0]
);
}
logger.debug(
'There are more than two entry points associated with chunk',
c.files[0]
);
} else if (existingEntries.length === 0) {
if (debug) {
console.error('Cannot find entry point for chunk: ' + c.files[0]);
}
logger.debug('Cannot find entry point for chunk: ' + c.files[0]);
} else {
const path = getModulePath(existingEntries[0].name);
if (path) {
fileChunk[path] = c.files[0];
}
}
} else {
if (debug) {
console.warn('Cannot find modules for chunk', c.files[0]);
}
logger.debug('Cannot find modules for chunk', c.files[0]);
}
});

Expand Down

0 comments on commit cff525d

Please sign in to comment.