Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling changes/improvements #1831

Merged
merged 11 commits into from
Jan 14, 2019
40 changes: 17 additions & 23 deletions gulp-tasks/utils/build-browser-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const rollupHelper = require('./rollup-helper');
* To test sourcemaps are valid and working, use:
* http://paulirish.github.io/source-map-visualization/#custom-choose
*/
const ERROR_NO_MODULE_BROWSER = `Could not find the modules browser.mjs file: `;
const ERROR_NO_MODULE_INDEX = `Could not find the module's index.mjs file: `;
const ERROR_NO_NAMSPACE = oneLine`
You must define a 'browserNamespace' parameter in the 'package.json'.
Example: 'workbox-precaching' would have a browserNamespace param of
Expand All @@ -53,26 +53,24 @@ const globals = (moduleId) => {
return path.basename(importPathPiece, path.extname(importPathPiece));
});

let additionalNamespace;
if (namespacePathParts.length === 0) {
// Tried to pull in default export of module - this isn't allowed.
// A specific file must be referenced
throw new Error(oneLine`
You cannot use a module directly - you must specify
file, this is to force a best practice for tree shaking (i.e. only
pulling in what you use). Please fix the import: '${moduleId}'
`);
You cannot use a module directly - you must specify
file, this is to force a best practice for tree shaking (i.e. only
pulling in what you use). Please fix the import: '${moduleId}'`);
}

let additionalNamespace;
if (namespacePathParts.length > 1) {
if (namespacePathParts[0] !== '_private' || namespacePathParts.length > 2) {
// Tried to pull in default export of module - this isn't allowed.
// A specific file must be referenced
// A specific file must be referenced
throw new Error(oneLine`
You cannot use nested files. It must be a top level (and public) file
or a file under '_private' in a module. Please fix the import:
'${moduleId}'
`);
You cannot use nested files. It must be a top level (and public) file
or a file under '_private' in a module. Please fix the import:
'${moduleId}'`);
}
additionalNamespace = namespacePathParts[0];
}
Expand All @@ -84,14 +82,10 @@ const globals = (moduleId) => {
return [
pkg.workbox.browserNamespace,
additionalNamespace,
]
.filter((value) => {
return (value && value.length > 0);
})
.join('.');
].filter((value) => (value && value.length > 0)).join('.');
} catch (err) {
logHelper.error(`Unable to get browserNamespace for package: ` +
`'${packageName}'`);
`'${packageName}'`);
logHelper.error(err);
throw err;
}
Expand All @@ -105,13 +99,13 @@ const externalAndPure = (importPath) => {

module.exports = (packagePath, buildType) => {
const packageName = pkgPathToName(packagePath);
const moduleBrowserPath = path.join(packagePath, `browser.mjs`);
const packageIndex = path.join(packagePath, `index.mjs`);

// First check if the bundle file exists, if it doesn't
// there is nothing to build
if (!fs.existsSync(moduleBrowserPath)) {
logHelper.error(ERROR_NO_MODULE_BROWSER + packageName);
return Promise.reject(ERROR_NO_MODULE_BROWSER + packageName);
if (!fs.existsSync(packageIndex)) {
logHelper.error(ERROR_NO_MODULE_INDEX + packageName);
return Promise.reject(ERROR_NO_MODULE_INDEX + packageName);
}

const pkgJson = require(path.join(packagePath, 'package.json'));
Expand Down Expand Up @@ -147,7 +141,7 @@ module.exports = (packagePath, buildType) => {
const plugins = rollupHelper.getDefaultPlugins(buildType);

return rollupStream({
input: moduleBrowserPath,
input: packageIndex,
rollup,
output: {
name: namespace,
Expand Down Expand Up @@ -184,7 +178,7 @@ module.exports = (packagePath, buildType) => {
})
// We must give the generated stream the same name as the entry file
// for the sourcemaps to work correctly
.pipe(source(moduleBrowserPath))
.pipe(source(packageIndex))
// gulp-sourcemaps don't work with streams so we need
.pipe(buffer())
// This tells gulp-sourcemaps to load the inline sourcemap
Expand Down
14 changes: 7 additions & 7 deletions infra/testing/activate-and-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
const activateSWSafari = require('./activate-sw-safari');

// TODO(philipwalton): remove this in favor of using workbox-window.
module.exports = async (swUrl) => {
module.exports = async (swURL) => {
if (global.__workbox.seleniumBrowser.getId() === 'safari') {
return activateSWSafari(swUrl);
return activateSWSafari(swURL);
}

const error = await global.__workbox.webdriver.executeAsyncScript((swUrl, cb) => {
const error = await global.__workbox.webdriver.executeAsyncScript((swURL, cb) => {
function _onStateChangePromise(registration, desiredState) {
return new Promise((resolve, reject) => {
if (desiredState === 'activated' && registration.active) {
Expand Down Expand Up @@ -49,24 +49,24 @@ module.exports = async (swUrl) => {
});
}

navigator.serviceWorker.register(swUrl).then((registration) => {
navigator.serviceWorker.register(swURL).then((registration) => {
return _onStateChangePromise(registration, 'activated');
}).then(() => {
// Ensure the page is being controlled by the SW.
if (navigator.serviceWorker.controller &&
navigator.serviceWorker.controller.scriptURL === swUrl) {
navigator.serviceWorker.controller.scriptURL === swURL) {
return;
} else {
return new Promise((resolve) => {
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (navigator.serviceWorker.controller.scriptURL === swUrl) {
if (navigator.serviceWorker.controller.scriptURL === swURL) {
resolve();
}
});
});
}
}).then(() => cb()).catch((error) => cb(error.message));
}, swUrl);
}, swURL);

if (error) {
throw new Error(error);
Expand Down
16 changes: 8 additions & 8 deletions infra/testing/activate-sw-safari.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
*/

// TODO(philipwalton): remove this in favor of using workbox-window.
module.exports = async (swUrl) => {
module.exports = async (swURL) => {
// First step: Wait for the page to activate
let error = await global.__workbox.webdriver.executeAsyncScript((swUrl, cb) => {
let error = await global.__workbox.webdriver.executeAsyncScript((swURL, cb) => {
const onStateChangePromise = (registration, desiredState) => {
return new Promise((resolve, reject) => {
if (desiredState === 'activated' &&
registration.active &&
// Checking that the URLs match is needed to fix:
// https://github.com/GoogleChrome/workbox/issues/1633
registration.active.scriptURL === swUrl) {
registration.active.scriptURL === swURL) {
resolve();
return;
}
Expand Down Expand Up @@ -47,27 +47,27 @@ module.exports = async (swUrl) => {
});
};

navigator.serviceWorker.register(swUrl)
navigator.serviceWorker.register(swURL)
.then((registration) => onStateChangePromise(registration, 'activated'))
.then(() => cb())
.catch((err) => cb(err.message));
}, swUrl);
}, swURL);

if (error) {
throw error;
}

// To be 100% certain - ensure the SW is controlling the page.
error = await global.__workbox.webdriver.executeAsyncScript((swUrl, cb) => {
error = await global.__workbox.webdriver.executeAsyncScript((swURL, cb) => {
if (navigator.serviceWorker.controller &&
navigator.serviceWorker.controller.scriptURL === swUrl) {
navigator.serviceWorker.controller.scriptURL === swURL) {
cb();
} else if (!navigator.serviceWorker.controller) {
cb(`There's no service worker controlling the page.`);
} else {
cb(`There's an unexpected SW controlling the page: ${navigator.serviceWorker.controller.scriptURL}`);
}
}, swUrl);
}, swURL);

if (error) {
throw error;
Expand Down
7 changes: 4 additions & 3 deletions infra/testing/auto-stub-logger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import sinon from 'sinon';
import {logger} from '../../packages/workbox-core/_private/logger.mjs';

const sandbox = sinon.createSandbox();

const stubLogger = () => {
sandbox.stub(logger);
sandbox.stub(logger.unprefixed);
// Logger will be `null` in production mode.
if (logger) {
sandbox.stub(logger);
}
};

// Silence any early messages (Normally caused by logging from an import at
Expand Down
6 changes: 3 additions & 3 deletions infra/testing/clean-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

const runInSW = require('./comlink/node-interface');

module.exports = async (webdriver, testingUrl) => {
await webdriver.get(testingUrl);
module.exports = async (webdriver, testingURL) => {
await webdriver.get(testingURL);
try {
await runInSW('clearAllCaches');
} catch (ignored) {
Expand All @@ -26,5 +26,5 @@ module.exports = async (webdriver, testingUrl) => {
if (error) {
throw new Error(error);
}
await webdriver.get(testingUrl);
await webdriver.get(testingURL);
};
2 changes: 1 addition & 1 deletion infra/testing/comlink/sw-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const api = {
});
},

cacheUrls: async (cacheName) => {
cacheURLs: async (cacheName) => {
const cache = await caches.open(cacheName);
const requests = await cache.keys();
return requests.map((request) => request.url);
Expand Down
2 changes: 1 addition & 1 deletion infra/testing/server/request-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class RequestCounter {
return this.headerCount[headerValue] || 0;
}

getUrlCount(url) {
getURLCount(url) {
return this.urlCount[url] || 0;
}
}
Expand Down
8 changes: 4 additions & 4 deletions infra/testing/validator/service-worker-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function setupSpiesAndContext() {
cacheableResponse: {
Plugin: CacheableResponsePlugin,
},
clientsClaim: sinon.spy(),
expiration: {
Plugin: CacheExpirationPlugin,
},
Expand All @@ -55,10 +54,11 @@ function setupSpiesAndContext() {
registerRoute: sinon.spy(),
},
core: {
clientsClaim: sinon.spy(),
setCacheNameDetails: sinon.spy(),
skipWaiting: sinon.spy(),
},
setConfig: sinon.spy(),
skipWaiting: sinon.spy(),
// To make testing easier, return the name of the strategy.
strategies: {
cacheFirst: sinon.stub().returns('cacheFirst'),
Expand All @@ -76,15 +76,15 @@ function setupSpiesAndContext() {
cacheableResponsePlugin: cacheableResponsePluginSpy,
cacheExpirationPlugin: cacheExpirationPluginSpy,
cacheFirst: workbox.strategies.cacheFirst,
clientsClaim: workbox.clientsClaim,
clientsClaim: workbox.core.clientsClaim,
googleAnalyticsInitialize: workbox.googleAnalytics.initialize,
networkFirst: workbox.strategies.networkFirst,
precacheAndRoute: workbox.precaching.precacheAndRoute,
registerNavigationRoute: workbox.routing.registerNavigationRoute,
registerRoute: workbox.routing.registerRoute,
setCacheNameDetails: workbox.core.setCacheNameDetails,
setConfig: workbox.setConfig,
skipWaiting: workbox.skipWaiting,
skipWaiting: workbox.core.skipWaiting,
suppressWarnings: workbox.precaching.suppressWarnings,
};

Expand Down
16 changes: 0 additions & 16 deletions packages/workbox-background-sync/_public.mjs

This file was deleted.

12 changes: 0 additions & 12 deletions packages/workbox-background-sync/browser.mjs

This file was deleted.

8 changes: 7 additions & 1 deletion packages/workbox-background-sync/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
https://opensource.org/licenses/MIT.
*/

import {Queue} from './Queue.mjs';
import {Plugin} from './Plugin.mjs';
import './_version.mjs';


/**
* @namespace workbox.backgroundSync
*/

export * from './_public.mjs';
export {
Queue,
Plugin,
};
20 changes: 0 additions & 20 deletions packages/workbox-broadcast-cache-update/_public.mjs

This file was deleted.

11 changes: 0 additions & 11 deletions packages/workbox-broadcast-cache-update/browser.mjs

This file was deleted.

16 changes: 13 additions & 3 deletions packages/workbox-broadcast-cache-update/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
https://opensource.org/licenses/MIT.
*/

import {BroadcastCacheUpdate} from './BroadcastCacheUpdate.mjs';
import {Plugin} from './Plugin.mjs';
import {broadcastUpdate} from './broadcastUpdate.mjs';
import {responsesAreSame} from './responsesAreSame.mjs';
import './_version.mjs';


/**
* @namespace workbox.broadcastUpdate
*/

import './_version.mjs';

export * from './_public.mjs';
export {
BroadcastCacheUpdate,
Plugin,
broadcastUpdate,
responsesAreSame,
};
Loading