Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.1.0",
"version": "0.2.0",
"repository": "https://github.com/guess-js/guess"
}
2 changes: 1 addition & 1 deletion infra/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const build = (hook = (path: string) => {}) => {

for (const p of Packages) {
const path = join(PackagesDir, p);
console.log(execSync(`cd ${path} && rm -rf dist && ${cwd}/node_modules/.bin/webpack .`).toString());
console.log(execSync(`cd ${path} && rm -rf dist && ${cwd}/node_modules/.bin/webpack`).toString());
const packageJsonPath = join(path, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
packageJson.version = config.version;
Expand Down
2 changes: 1 addition & 1 deletion packages/ga/src/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getClient } from './client';
import { normalize } from './normalize';
import { Graph, Period } from '../../common/interfaces';

const PageSize = 1000;
const PageSize = 10000;
const id = (r: string) => r;
const DefaultExpression = 'ga:pageviews';

Expand Down
2 changes: 1 addition & 1 deletion packages/ga/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
mode: 'development',
mode: 'production',
entry: './index.ts',
target: 'node',
output: {
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
mode: 'development',
mode: 'production',
entry: './index.ts',
target: 'node',
output: {
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "guess-webpack",
"version": "0.1.0",
"description": "Webpack plugins for the Machine Learning-driven bundler",
"main": "webpack/index.js",
"main": "webpack/main.js",
"types": "webpack/index.d.ts",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const guess = (current: string, links?: string[]) =>
((typeof window === 'undefined' ? global : window) as any).__GUESS__.guess(current, links);
export const guess = (params: any) =>
((typeof window === 'undefined' ? global : window) as any).__GUESS__.guess(params);
28 changes: 18 additions & 10 deletions packages/webpack/src/ga-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { auth } from 'google-oauth2-node';
import { auth as oauth2 } from 'google-oauth2-node';
import { RoutingModule, Period, Graph } from '../../common/interfaces';
import { fetch } from 'guess-ga';

Expand All @@ -13,6 +13,7 @@ const cache = flatCache.load('guess-plugin');
const id = <T>(r: T) => r;

export interface Config {
jwt?: any;
viewId: string;
routes: RoutingModule[];
formatter?: (path: string) => string;
Expand All @@ -28,19 +29,26 @@ export const getReport = (c: Config): Promise<Graph> => {
if (report) {
return Promise.resolve(JSON.parse(report));
}
return auth({
clientId,
clientSecret,
scope
})
.then((token: any) => {
const { google } = require('googleapis');
const { google } = require('googleapis');
let client: Promise<{}> = Promise.reject('Non-existing GA token');
if (!c.jwt) {
client = oauth2({
clientId,
clientSecret,
scope
}).then((token: any) => {
const oauth2Client = new google.auth.OAuth2();
oauth2Client.setCredentials(token);

return oauth2Client;
});
} else {
client = Promise.resolve(new google.auth.JWT(c.jwt.client_email, null, c.jwt.private_key, [scope], null));
}
return client
.then((auth: any) => {
return fetch({
viewId: c.viewId,
auth: oauth2Client,
auth,
period: period,
routes: c.routes.map(r => r.path),
formatter: c.formatter || id
Expand Down
20 changes: 11 additions & 9 deletions packages/webpack/src/guess-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ export interface RuntimeConfig {

export interface GuessPluginConfig {
GA?: string;
jwt?: any;
period?: Period;
reportProvider?: (...args: any[]) => Promise<Graph>;
mode?: Mode;
layout?: ProjectLayout;
period?: Period;

/** @internal */
routeFormatter?: (path: string) => string;
routeProvider?: RouteProvider | boolean;
/** @internal */
debug?: boolean;
routeFormatter?: (path: string) => string;
/** @internal */
runtime?: RuntimeConfig;
/** @internal */
routeProvider?: RouteProvider | boolean;
}

export class GuessPlugin {
constructor(private _config: GuessPluginConfig) {
if (this._config.GA && this._config.reportProvider) {
if ((this._config.GA || this._config.jwt) && this._config.reportProvider) {
throw new Error(
'Only a single report provider is allowed. You have specified `GA` (used by Google Analytics provider) and `reportProvider`'
'Only a single report provider is allowed. You have specified `GA` and/or ' +
'a GA authentication provider (used by Google Analytics provider) and `reportProvider`'
);
}
if (!this._config.GA && !this._config.reportProvider) {
Expand All @@ -50,11 +51,12 @@ export class GuessPlugin {

private _execute(compilation: any, cb: any) {
extractRoutes(this._config).then(routes => {
this._getReport(routes).then(
return this._getReport(routes).then(
data => {
return this._executePrefetchPlugin(data, routes, compilation, cb);
},
err => {
console.error(err);
cb();
throw err;
}
Expand All @@ -65,6 +67,7 @@ export class GuessPlugin {
private _getReport(routes: RoutingModule[]): Promise<Graph> {
if (this._config.GA) {
return getReport({
jwt: this._config.jwt,
viewId: this._config.GA,
routes,
formatter: this._config.routeFormatter,
Expand All @@ -81,7 +84,6 @@ export class GuessPlugin {
data,
basePath: runtime ? (runtime.basePath === undefined ? '' : runtime.basePath) : '',
prefetchConfig: runtime ? runtime.prefetchConfig : undefined,
debug: this._config.debug,
routes,
delegate: runtime ? !!runtime.delegate : false
}).execute(compilation, cb);
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/src/prefetch-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class PrefetchPlugin {
context: '/src/',
mode: 'production',
entry: './index.js',
target: 'web',
target: 'node',
output: {
filename: './output.js'
}
Expand All @@ -100,7 +100,7 @@ export class PrefetchPlugin {
}

const code = stats.compilation.assets['./output.js'].source();
compilation.assets[mainName] = new ConcatSource(code, '\n', old.source());
compilation.assets[mainName] = new ConcatSource(code, '\n;', old.source());
callback();
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/runtime/guess.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { initialize } from './guess';

(function(g, graph, m, basePath, thresholds) {
initialize(g, graph, m, basePath, thresholds);
})(typeof window === 'undefined' ? {} : window, <%= GRAPH %>, <%= GRAPH_MAP %>, <%= THRESHOLDS %>);
})(typeof window === 'undefined' ? global : window, <%= GRAPH %>, <%= GRAPH_MAP %>, <%= THRESHOLDS %>);
4 changes: 0 additions & 4 deletions packages/webpack/src/runtime/guess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ const matchRoute = (route: string, declaration: string) => {
}
};

const polyfillConnection = {
effectiveType: '3g'
};

const guessNavigation = (graph: Graph, params: GuessFnParams): Navigations => {
const matches = graph.findMatch(params.path);
return matches.reduce(
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/src/runtime/runtime.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { initialize } from './runtime';

(function(g, history, graph, m, basePath, thresholds) {
initialize(history, g, graph, m, basePath, thresholds);
})(typeof window === 'undefined' ? {} : window, (typeof window === 'undefined' ? {} : window).history, <%= GRAPH %>, <%= GRAPH_MAP %>, '<%= BASE_PATH %>', <%= THRESHOLDS %>);
})(typeof window === 'undefined' ? global : window, (typeof window === 'undefined' ? {} : window).history, <%= GRAPH %>, <%= GRAPH_MAP %>, '<%= BASE_PATH %>', <%= THRESHOLDS %>);
2 changes: 1 addition & 1 deletion packages/webpack/test/fixtures/delegate/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { GuessPlugin } = require('../../../dist/webpack/index');
const { GuessPlugin } = require('../../../dist/webpack/main');

module.exports = {
mode: 'development',
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack/test/fixtures/prefetch/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { GuessPlugin } = require('../../../dist/webpack/index');
const { GuessPlugin } = require('../../../dist/webpack/main');

const absolute = path => {
return join(__dirname, path);
Expand Down
38 changes: 17 additions & 21 deletions packages/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ const common = {
};

module.exports = [
Object.assign(
{
entry: './src/api.ts',
output: {
filename: 'index.js',
path: __dirname + '/dist/api/',
libraryTarget: 'commonjs'
},
target: 'web'
},
common
),
Object.assign(
{
entry: {
runtime: './src/runtime/runtime.ts'
},
target: 'web',
target: 'node',
output: {
filename: '[name].js',
path: __dirname + '/dist/webpack/',
libraryTarget: 'umd'
libraryTarget: 'commonjs'
}
},
common
Expand All @@ -41,11 +53,11 @@ module.exports = [
entry: {
guess: './src/runtime/guess.ts'
},
target: 'web',
target: 'node',
output: {
filename: '[name].js',
path: __dirname + '/dist/webpack/',
libraryTarget: 'umd'
libraryTarget: 'commonjs'
}
},
common
Expand All @@ -54,7 +66,7 @@ module.exports = [
{
entry: './index.ts',
output: {
filename: 'index.js',
filename: 'main.js',
path: __dirname + '/dist/webpack/',
libraryTarget: 'umd'
},
Expand All @@ -71,21 +83,5 @@ module.exports = [
]
},
common
),
Object.assign(
{
entry: './src/api.ts',
output: {
filename: 'index.js',
path: __dirname + '/dist/api/',
libraryTarget: 'umd'
},
target: 'node',
node: {
__dirname: false,
__filename: false
}
},
common
)
];