diff --git a/config.json b/config.json index 16dafda6..134d0617 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,4 @@ { - "version": "0.1.0", + "version": "0.2.0", "repository": "https://github.com/guess-js/guess" } diff --git a/infra/build.ts b/infra/build.ts index 066c6938..97f22fa5 100755 --- a/infra/build.ts +++ b/infra/build.ts @@ -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; diff --git a/packages/ga/src/ga.ts b/packages/ga/src/ga.ts index 627d1f06..b2fc0fb5 100644 --- a/packages/ga/src/ga.ts +++ b/packages/ga/src/ga.ts @@ -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'; diff --git a/packages/ga/webpack.config.js b/packages/ga/webpack.config.js index eb71aa74..dd530002 100644 --- a/packages/ga/webpack.config.js +++ b/packages/ga/webpack.config.js @@ -1,5 +1,5 @@ module.exports = { - mode: 'development', + mode: 'production', entry: './index.ts', target: 'node', output: { diff --git a/packages/parser/webpack.config.js b/packages/parser/webpack.config.js index 30544312..845b10a6 100644 --- a/packages/parser/webpack.config.js +++ b/packages/parser/webpack.config.js @@ -1,5 +1,5 @@ module.exports = { - mode: 'development', + mode: 'production', entry: './index.ts', target: 'node', output: { diff --git a/packages/webpack/package.json b/packages/webpack/package.json index 733a3c06..3a323458 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -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", diff --git a/packages/webpack/src/api.ts b/packages/webpack/src/api.ts index 27087285..2f5be44a 100644 --- a/packages/webpack/src/api.ts +++ b/packages/webpack/src/api.ts @@ -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); diff --git a/packages/webpack/src/ga-provider.ts b/packages/webpack/src/ga-provider.ts index 9c8c6e5d..4b6d084d 100644 --- a/packages/webpack/src/ga-provider.ts +++ b/packages/webpack/src/ga-provider.ts @@ -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'; @@ -13,6 +13,7 @@ const cache = flatCache.load('guess-plugin'); const id = (r: T) => r; export interface Config { + jwt?: any; viewId: string; routes: RoutingModule[]; formatter?: (path: string) => string; @@ -28,19 +29,26 @@ export const getReport = (c: Config): Promise => { 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 diff --git a/packages/webpack/src/guess-webpack.ts b/packages/webpack/src/guess-webpack.ts index acb88b7f..af061fa1 100644 --- a/packages/webpack/src/guess-webpack.ts +++ b/packages/webpack/src/guess-webpack.ts @@ -16,25 +16,26 @@ export interface RuntimeConfig { export interface GuessPluginConfig { GA?: string; + jwt?: any; + period?: Period; reportProvider?: (...args: any[]) => Promise; 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) { @@ -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; } @@ -65,6 +67,7 @@ export class GuessPlugin { private _getReport(routes: RoutingModule[]): Promise { if (this._config.GA) { return getReport({ + jwt: this._config.jwt, viewId: this._config.GA, routes, formatter: this._config.routeFormatter, @@ -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); diff --git a/packages/webpack/src/prefetch-plugin.ts b/packages/webpack/src/prefetch-plugin.ts index 19072313..184fc7d5 100644 --- a/packages/webpack/src/prefetch-plugin.ts +++ b/packages/webpack/src/prefetch-plugin.ts @@ -82,7 +82,7 @@ export class PrefetchPlugin { context: '/src/', mode: 'production', entry: './index.js', - target: 'web', + target: 'node', output: { filename: './output.js' } @@ -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(); }); } diff --git a/packages/webpack/src/runtime/guess.tpl b/packages/webpack/src/runtime/guess.tpl index 8694e7cf..725279b7 100644 --- a/packages/webpack/src/runtime/guess.tpl +++ b/packages/webpack/src/runtime/guess.tpl @@ -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 %>); diff --git a/packages/webpack/src/runtime/guess.ts b/packages/webpack/src/runtime/guess.ts index 53d6bf43..bf176f90 100644 --- a/packages/webpack/src/runtime/guess.ts +++ b/packages/webpack/src/runtime/guess.ts @@ -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( diff --git a/packages/webpack/src/runtime/runtime.tpl b/packages/webpack/src/runtime/runtime.tpl index 1687d243..8c311caf 100644 --- a/packages/webpack/src/runtime/runtime.tpl +++ b/packages/webpack/src/runtime/runtime.tpl @@ -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 %>); diff --git a/packages/webpack/test/fixtures/delegate/webpack.config.js b/packages/webpack/test/fixtures/delegate/webpack.config.js index 48653007..c341c22b 100644 --- a/packages/webpack/test/fixtures/delegate/webpack.config.js +++ b/packages/webpack/test/fixtures/delegate/webpack.config.js @@ -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', diff --git a/packages/webpack/test/fixtures/prefetch/webpack.config.js b/packages/webpack/test/fixtures/prefetch/webpack.config.js index 0086abb3..1bfb31de 100644 --- a/packages/webpack/test/fixtures/prefetch/webpack.config.js +++ b/packages/webpack/test/fixtures/prefetch/webpack.config.js @@ -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); diff --git a/packages/webpack/webpack.config.js b/packages/webpack/webpack.config.js index 558c8afc..23cafc2f 100644 --- a/packages/webpack/webpack.config.js +++ b/packages/webpack/webpack.config.js @@ -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 @@ -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 @@ -54,7 +66,7 @@ module.exports = [ { entry: './index.ts', output: { - filename: 'index.js', + filename: 'main.js', path: __dirname + '/dist/webpack/', libraryTarget: 'umd' }, @@ -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 ) ];