Skip to content

Commit

Permalink
Rename the workbox-google-analytics output file
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Oct 8, 2018
1 parent b6ac5d7 commit 050f680
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 55 deletions.
10 changes: 6 additions & 4 deletions gulp-tasks/utils/build-browser-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,18 @@ module.exports = (packagePath, buildType) => {
return Promise.reject(ERROR_NO_NAMSPACE + ' ' + packageName);
}

let outputFilename = `${packageName}.${buildType.slice(0, 4)}.js`;
let outputFilename = pkgJson.workbox.outputFilename || packageName;
if (pkgJson.workbox.prodOnly) {
// Bail out early if this is a non-prod build.
if (buildType !== constants.BUILD_TYPES.prod) {
return Promise.resolve();
}
// If it is a prod build, then we don't have to bother including the
// buildType in the filename, since there is no dev build.
outputFilename = `${packageName}.js`;
} else {
// Prod-only builds (above) don't need the build type, but when there's a
// dev and prod build we have to include it.
outputFilename += `.${buildType.slice(0, 4)}`;
}
outputFilename += '.js';

const namespace = pkgJson.workbox.browserNamespace;
const outputDirectory = path.join(packagePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
const glob = require('glob');
const path = require('path');

module.exports = (root, type) => {

const DEFAULT_ROOT = path.join(__dirname, '..', '..');


const getPackages = ({type, root = DEFAULT_ROOT} = {}) => {
const pathToPkgJsons = glob.sync('packages/*/package.json', {cwd: root});
return pathToPkgJsons.filter((pathToPkgJson) => {

return pathToPkgJsons.map((pathToPkgJson) => {
const pkg = require(`${path.resolve(root)}/${pathToPkgJson}`);
return pkg.workbox && (pkg.workbox.packageType === type);
}).map((pathToPkgJson) => {
// Since we matched the glob pattern then we know that this will always
// return the name of the package.
return pathToPkgJson.split('/')[1];
return pkg;
}).filter((pkg) => {
return pkg.workbox && pkg.workbox.packageType === type;
});
};

module.exports = {
getPackages,
};
22 changes: 22 additions & 0 deletions gulp-tasks/utils/output-filename-to-package-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

const {getPackages} = require('./get-packages');


const outputFilenameToPkgMap = {};
getPackages({type: 'browser'}).forEach((pkg) => {
// When no `outputFilename` property exists, the package name is used.
const outputFilename = pkg.workbox.outputFilename || pkg.name;

outputFilenameToPkgMap[outputFilename] = pkg;
});

module.exports = {
outputFilenameToPkgMap,
};
7 changes: 5 additions & 2 deletions gulp-tasks/utils/publish-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const archiver = require('archiver');
const oneLine = require('common-tags').oneLine;

const constants = require('./constants');
const getPackagesOfType = require('./get-packages-of-type');
const {getPackages} = require('./get-packages');
const logHelper = require('../../infra/utils/log-helper');
const spawn = require('./spawn-promise-wrapper');

Expand Down Expand Up @@ -106,7 +106,10 @@ const groupBuildFiles = async (tagName, gitBranch) => {

const sourceCodePath = path.join(getBuildPath(tagName), SOURCE_CODE_DIR);

const browserPackages = getPackagesOfType(sourceCodePath, 'browser');
const browserPackages = getPackages({
type: 'browser',
root: sourceCodePath,
});

const pattern = path.posix.join(
sourceCodePath, 'packages', `{${browserPackages.join(',')}}`,
Expand Down
18 changes: 10 additions & 8 deletions infra/testing/server/routes/build-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
*/

const path = require('path');

const constants = require('../../../../gulp-tasks/utils/constants');
const {outputFilenameToPkgMap} = require('../../../../gulp-tasks/utils/output-filename-to-package-map');


const match = '/__WORKBOX/buildFile/:moduleInfo';
const ROOT_DIR = path.join(__dirname, '..', '..', '..', '..');
const match = '/__WORKBOX/buildFile/:packageFile';

async function handler(req, res) {
const {moduleInfo} = req.params;
const [moduleName, buildType, extension] = moduleInfo.split('.', 3);
const {packageFile} = req.params;
const [outputFilename, buildType, extension] = packageFile.split('.', 3);

const packagePath = `../../../../packages/${moduleName}`;
const {main} = require(`${packagePath}/package.json`);
const pkg = outputFilenameToPkgMap[outputFilename];
const packagePath = path.join(ROOT_DIR, 'packages', pkg.name);
const buildPath = path.dirname(path.join(packagePath, pkg.main));

const buildPath = path.dirname(path.join(packagePath, main));
let fileName = path.basename(main);
let fileName = path.basename(pkg.main);

if (buildType) {
fileName = fileName.replace('.prod.', `.${buildType}.`);
Expand Down
3 changes: 2 additions & 1 deletion packages/workbox-google-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
},
"workbox": {
"browserNamespace": "workbox.googleAnalytics",
"outputFilename": "workbox-offline-ga",
"packageType": "browser"
},
"main": "build/workbox-google-analytics.prod.js",
"main": "build/workbox-offline-ga.prod.js",
"module": "index.mjs",
"dependencies": {
"workbox-background-sync": "^3.6.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-sw/controllers/WorkboxSW.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MODULE_KEY_TO_NAME_MAPPING = {
cacheableResponse: 'cacheable-response',
core: 'core',
expiration: 'cache-expiration',
googleAnalytics: 'google-analytics',
googleAnalytics: 'offline-ga',
navigationPreload: 'navigation-preload',
precaching: 'precaching',
rangeRequests: 'range-requests',
Expand Down
8 changes: 4 additions & 4 deletions test/workbox-build/node/entry-points/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ describe(`[workbox-build] entry-points/generate-sw.js (End to End)`, function()
'workbox-core.dev.js.map',
'workbox-core.prod.js',
'workbox-core.prod.js.map',
'workbox-google-analytics.dev.js',
'workbox-google-analytics.dev.js.map',
'workbox-google-analytics.prod.js',
'workbox-google-analytics.prod.js.map',
'workbox-navigation-preload.dev.js',
'workbox-navigation-preload.dev.js.map',
'workbox-navigation-preload.prod.js',
'workbox-navigation-preload.prod.js.map',
'workbox-offline-ga.dev.js',
'workbox-offline-ga.dev.js.map',
'workbox-offline-ga.prod.js',
'workbox-offline-ga.prod.js.map',
'workbox-precaching.dev.js',
'workbox-precaching.dev.js.map',
'workbox-precaching.prod.js',
Expand Down
9 changes: 3 additions & 6 deletions test/workbox-google-analytics/static/basic-example/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
https://opensource.org/licenses/MIT.
*/

/* globals workbox */
importScripts('/__WORKBOX/buildFile/workbox-sw');
importScripts('/infra/testing/comlink/sw-interface.js');

importScripts('/__WORKBOX/buildFile/workbox-core');
importScripts('/__WORKBOX/buildFile/workbox-background-sync');
importScripts('/__WORKBOX/buildFile/workbox-routing');
importScripts('/__WORKBOX/buildFile/workbox-strategies');
importScripts('/__WORKBOX/buildFile/workbox-google-analytics');
workbox.setConfig({modulePathPrefix: '/__WORKBOX/buildFile/'});

// Spy on .fetch() calls from inside the service worker.
// If `simulateOffline` is set to true, throw a network error.
Expand Down
24 changes: 10 additions & 14 deletions test/workbox-sw/node/test-WorkboxSW.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

import {expect} from 'chai';
import sinon from 'sinon';
import path from 'path';
import fs from 'fs-extra';
import generateTestVariants from '../../../infra/testing/generate-variant-tests';
import WorkboxSW from '../../../packages/workbox-sw/controllers/WorkboxSW.mjs';
import getPackagesOfType from '../../../gulp-tasks/utils/get-packages-of-type';
import {getPackages} from '../../../gulp-tasks/utils/get-packages';
import {outputFilenameToPkgMap} from '../../../gulp-tasks/utils/output-filename-to-package-map';

const ROOT_DIR = path.join(__dirname, '..', '..', '..');

describe(`[workbox-sw] WorkboxSW`, function() {
let sandbox = sinon.createSandbox();
Expand All @@ -30,11 +28,12 @@ describe(`[workbox-sw] WorkboxSW`, function() {
return;
}

const packageName = match[1];
const pkgJson = fs.readJSONSync(path.join(ROOT_DIR, 'packages', packageName, 'package.json'));
const namespace = pkgJson.workbox.browserNamespace.split('.')[1];
const outputFilename = match[1];
const pkg = outputFilenameToPkgMap[outputFilename];

const namespace = pkg.workbox.browserNamespace.split('.')[1];
self.workbox[namespace] = {
injectedMsg: `Injected value for ${packageName}.`,
injectedMsg: `Injected value for ${pkg.name}.`,
};
});
});
Expand Down Expand Up @@ -231,12 +230,9 @@ describe(`[workbox-sw] WorkboxSW`, function() {
});
});

const browserPackages = getPackagesOfType(ROOT_DIR, 'browser');
browserPackages.forEach((pkgName) => {
const pkg = fs.readJSONSync(path.join(ROOT_DIR, 'packages', pkgName, 'package.json'));
if (pkg.workbox.browserNamespace === 'workbox') {
return;
}
getPackages({type: 'browser'}).forEach((pkg) => {
// Don't test workbox-sw, which exports the `workbox` namespace.
if (pkg.workbox.browserNamespace === 'workbox') return;

describe(`get ${pkg.workbox.browserNamespace}`, function() {
it(`should return ${pkg.workbox.browserNamespace}`, function() {
Expand Down
3 changes: 3 additions & 0 deletions test/workbox-sw/static/integration/invalid-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/

importScripts('/__WORKBOX/buildFile/workbox-sw');
importScripts('/infra/testing/comlink/sw-interface.js');

workbox.setConfig({modulePathPrefix: '/__WORKBOX/buildFile/'});

// This is expected to lead to an error.
const namespace = 'doesnotexist';
Expand Down
3 changes: 3 additions & 0 deletions test/workbox-sw/static/integration/valid-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/

importScripts('/__WORKBOX/buildFile/workbox-sw');
importScripts('/infra/testing/comlink/sw-interface.js');

workbox.setConfig({modulePathPrefix: '/__WORKBOX/buildFile/'});

// TODO: Find some way to autogenerate this list.
const namespaces = [
Expand Down
8 changes: 4 additions & 4 deletions test/workbox-webpack-plugin/node/generate-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ describe(`[workbox-webpack-plugin] GenerateSW (End to End)`, function() {
'workbox-core.dev.js.map',
'workbox-core.prod.js',
'workbox-core.prod.js.map',
'workbox-google-analytics.dev.js',
'workbox-google-analytics.dev.js.map',
'workbox-google-analytics.prod.js',
'workbox-google-analytics.prod.js.map',
'workbox-navigation-preload.dev.js',
'workbox-navigation-preload.dev.js.map',
'workbox-navigation-preload.prod.js',
'workbox-navigation-preload.prod.js.map',
'workbox-offline-ga.dev.js',
'workbox-offline-ga.dev.js.map',
'workbox-offline-ga.prod.js',
'workbox-offline-ga.prod.js.map',
'workbox-precaching.dev.js',
'workbox-precaching.dev.js.map',
'workbox-precaching.prod.js',
Expand Down
8 changes: 4 additions & 4 deletions test/workbox-webpack-plugin/node/inject-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ describe(`[workbox-webpack-plugin] InjectManifest (End to End)`, function() {
'workbox-core.dev.js.map',
'workbox-core.prod.js',
'workbox-core.prod.js.map',
'workbox-google-analytics.dev.js',
'workbox-google-analytics.dev.js.map',
'workbox-google-analytics.prod.js',
'workbox-google-analytics.prod.js.map',
'workbox-navigation-preload.dev.js',
'workbox-navigation-preload.dev.js.map',
'workbox-navigation-preload.prod.js',
'workbox-navigation-preload.prod.js.map',
'workbox-offline-ga.dev.js',
'workbox-offline-ga.dev.js.map',
'workbox-offline-ga.prod.js',
'workbox-offline-ga.prod.js.map',
'workbox-precaching.dev.js',
'workbox-precaching.dev.js.map',
'workbox-precaching.prod.js',
Expand Down

0 comments on commit 050f680

Please sign in to comment.