Skip to content

Commit

Permalink
Do not include base path in each instruction (#225)
Browse files Browse the repository at this point in the history
This should shrink bundles with lots of instructions. It also introduces a breaking config change:

```ts
{
  runtime: {
    basePath: '...'
  }
}
```

is now:

```ts
{
  runtime: {
    base: '...'
  }
}
```
  • Loading branch information
mgechev committed Sep 21, 2019
1 parent 509abaa commit 337c218
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
6 changes: 6 additions & 0 deletions infra/e2e.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ if (mainModule.indexOf('__GUESS__.p(') < 0 || mainModule.indexOf('__GUESS__.p=')
process.exit(1); process.exit(1);
} }


// No base
if (mainModule.indexOf('"http://localhost:1337"') < 0) {
console.error('Unable to find the base path');
process.exit(1);
}

// Prod build should work // Prod build should work
console.log(execSync( console.log(execSync(
`${enterTest} && ./node_modules/.bin/ng build --prod --extra-webpack-config webpack.extra.js` `${enterTest} && ./node_modules/.bin/ng build --prod --extra-webpack-config webpack.extra.js`
Expand Down
6 changes: 3 additions & 3 deletions packages/guess-webpack/src/aot/aot.tpl
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
import { initialize } from './guess-aot'; import { initialize } from './guess-aot';


(function(g, thresholds) { (function(g, thresholds, base) {
initialize(g, thresholds); initialize(g, thresholds, base);
})(typeof window === 'undefined' ? global : window, <%= THRESHOLDS %>); })(typeof window === 'undefined' ? global : window, <%= THRESHOLDS %>, '<%= BASE_PATH %>');
8 changes: 5 additions & 3 deletions packages/guess-webpack/src/aot/guess-aot.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const supportedPrefetchStrategy = support('prefetch')
const preFetched: { [key: string]: boolean } = {}; const preFetched: { [key: string]: boolean } = {};


const prefetch = (basePath: string, url: string) => { const prefetch = (basePath: string, url: string) => {
url = basePath + url; if (basePath) {
url = basePath + '/' + url;
}
if (preFetched[url]) { if (preFetched[url]) {
return; return;
} }
Expand All @@ -50,14 +52,14 @@ const getConnection = (global: any): ConnectionEffectiveType => {
return global.navigator.connection.effectiveType || '3g'; return global.navigator.connection.effectiveType || '3g';
}; };


export const initialize = (g: any, t: PrefetchConfig) => { export const initialize = (g: any, t: PrefetchConfig, base?: string) => {
const idle = g.requestIdleCallback || ((cb: Function) => setTimeout(cb, 0)); const idle = g.requestIdleCallback || ((cb: Function) => setTimeout(cb, 0));
g.__GUESS__ = {}; g.__GUESS__ = {};
g.__GUESS__.p = (...p: [number, string][]) => { g.__GUESS__.p = (...p: [number, string][]) => {
idle(() => idle(() =>
p.forEach(c => p.forEach(c =>
c[0] >= t[getConnection(g)] c[0] >= t[getConnection(g)]
? c.slice(1).forEach(f => prefetch('', f as string)) ? c.slice(1).forEach(f => prefetch(base || '', f as string))
: void 0 : void 0
) )
); );
Expand Down
2 changes: 1 addition & 1 deletion packages/guess-webpack/src/declarations.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface PrefetchAotGraph {
export interface PrefetchAotPluginConfig { export interface PrefetchAotPluginConfig {
debug?: boolean; debug?: boolean;
data: Graph; data: Graph;
basePath: string; base: string;
prefetchConfig?: PrefetchConfig; prefetchConfig?: PrefetchConfig;
routes: RoutingModule[]; routes: RoutingModule[];
} }
12 changes: 6 additions & 6 deletions packages/guess-webpack/src/guess-webpack.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AssetObserver } from './asset-observer';


export interface RuntimeConfig { export interface RuntimeConfig {
/** @internal */ /** @internal */
basePath?: string; base?: string;
/** @internal */ /** @internal */
prefetchConfig?: PrefetchConfig; prefetchConfig?: PrefetchConfig;
/** @internal */ /** @internal */
Expand Down Expand Up @@ -126,9 +126,9 @@ export class GuessPlugin {
data, data,
debug: this._config.debug, debug: this._config.debug,
basePath: runtime basePath: runtime
? runtime.basePath === undefined ? runtime.base === undefined
? '' ? ''
: runtime.basePath : runtime.base
: '', : '',
prefetchConfig: runtime ? runtime.prefetchConfig : undefined, prefetchConfig: runtime ? runtime.prefetchConfig : undefined,
routes, routes,
Expand All @@ -138,10 +138,10 @@ export class GuessPlugin {
new PrefetchAotPlugin({ new PrefetchAotPlugin({
data, data,
debug: this._config.debug, debug: this._config.debug,
basePath: runtime base: runtime
? runtime.basePath === undefined ? runtime.base === undefined
? '' ? ''
: runtime.basePath : runtime.base
: '', : '',
prefetchConfig: runtime ? runtime.prefetchConfig : undefined, prefetchConfig: runtime ? runtime.prefetchConfig : undefined,
routes routes
Expand Down
12 changes: 3 additions & 9 deletions packages/guess-webpack/src/prefetch-aot-plugin.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ const alterChunk = (
}); });
}; };


const joinUrl = (basePath: string, chunk: string) => {
if (basePath) {
return basePath + '/' + chunk;
}
return chunk;
};

export class PrefetchAotPlugin { export class PrefetchAotPlugin {
private logger = new Logger(); private logger = new Logger();
constructor(private _config: PrefetchAotPluginConfig) { constructor(private _config: PrefetchAotPluginConfig) {
Expand Down Expand Up @@ -199,7 +192,7 @@ export class PrefetchAotPlugin {
return [ return [
c.probability, c.probability,
`[${c.probability},${c.chunks `[${c.probability},${c.chunks
.map(chunk => `'${joinUrl(this._config.basePath, chunk)}'`) .map(chunk => `'${chunk}'`)
.join(',')}]` .join(',')}]`
]; ];
}; };
Expand Down Expand Up @@ -258,7 +251,8 @@ export class PrefetchAotPlugin {
defaultPrefetchConfig, defaultPrefetchConfig,
this._config.prefetchConfig this._config.prefetchConfig
) )
) ),
BASE_PATH: this._config.base
}); });
newCode = prefetchingLogic + ';' + newCode; newCode = prefetchingLogic + ';' + newCode;
this.logger.debug('Altering the main chunk'); this.logger.debug('Altering the main chunk');
Expand Down
3 changes: 3 additions & 0 deletions packages/guess-webpack/test/fixtures/angular/webpack.extra.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
reportProvider() { reportProvider() {
return Promise.resolve(JSON.parse(require('fs').readFileSync('./routes.json').toString())); return Promise.resolve(JSON.parse(require('fs').readFileSync('./routes.json').toString()));
}, },
runtime: {
base: 'http://localhost:1337'
},
routeProvider() { routeProvider() {
return parseRoutes('.'); return parseRoutes('.');
} }
Expand Down

0 comments on commit 337c218

Please sign in to comment.