diff --git a/gulp-tasks/utils/build-browser-bundle.js b/gulp-tasks/utils/build-browser-bundle.js index 9d1806af1..0465e49a6 100644 --- a/gulp-tasks/utils/build-browser-bundle.js +++ b/gulp-tasks/utils/build-browser-bundle.js @@ -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, diff --git a/gulp-tasks/utils/get-packages-of-type.js b/gulp-tasks/utils/get-packages.js similarity index 54% rename from gulp-tasks/utils/get-packages-of-type.js rename to gulp-tasks/utils/get-packages.js index f7bfd5854..39eb4886a 100644 --- a/gulp-tasks/utils/get-packages-of-type.js +++ b/gulp-tasks/utils/get-packages.js @@ -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, +}; diff --git a/gulp-tasks/utils/output-filename-to-package-map.js b/gulp-tasks/utils/output-filename-to-package-map.js new file mode 100644 index 000000000..f6a1e3035 --- /dev/null +++ b/gulp-tasks/utils/output-filename-to-package-map.js @@ -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, +}; diff --git a/gulp-tasks/utils/publish-helpers.js b/gulp-tasks/utils/publish-helpers.js index afe21ada9..13548cb55 100644 --- a/gulp-tasks/utils/publish-helpers.js +++ b/gulp-tasks/utils/publish-helpers.js @@ -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'); @@ -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(',')}}`, diff --git a/infra/testing/server/routes/build-file.js b/infra/testing/server/routes/build-file.js index c1400fd04..31475501e 100644 --- a/infra/testing/server/routes/build-file.js +++ b/infra/testing/server/routes/build-file.js @@ -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}.`); diff --git a/packages/workbox-google-analytics/package.json b/packages/workbox-google-analytics/package.json index 7ef4f4a83..741f39f99 100644 --- a/packages/workbox-google-analytics/package.json +++ b/packages/workbox-google-analytics/package.json @@ -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", diff --git a/packages/workbox-sw/controllers/WorkboxSW.mjs b/packages/workbox-sw/controllers/WorkboxSW.mjs index f1c834fca..3f276a20a 100644 --- a/packages/workbox-sw/controllers/WorkboxSW.mjs +++ b/packages/workbox-sw/controllers/WorkboxSW.mjs @@ -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', diff --git a/test/workbox-build/node/entry-points/generate-sw.js b/test/workbox-build/node/entry-points/generate-sw.js index c00a20adb..ce70500bb 100644 --- a/test/workbox-build/node/entry-points/generate-sw.js +++ b/test/workbox-build/node/entry-points/generate-sw.js @@ -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', diff --git a/test/workbox-google-analytics/static/basic-example/sw.js b/test/workbox-google-analytics/static/basic-example/sw.js index 492eb2881..d19113b97 100644 --- a/test/workbox-google-analytics/static/basic-example/sw.js +++ b/test/workbox-google-analytics/static/basic-example/sw.js @@ -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. diff --git a/test/workbox-sw/node/test-WorkboxSW.mjs b/test/workbox-sw/node/test-WorkboxSW.mjs index 30b05afd4..3adf3f371 100644 --- a/test/workbox-sw/node/test-WorkboxSW.mjs +++ b/test/workbox-sw/node/test-WorkboxSW.mjs @@ -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(); @@ -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}.`, }; }); }); @@ -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() { diff --git a/test/workbox-sw/static/integration/invalid-sw.js b/test/workbox-sw/static/integration/invalid-sw.js index 5dfffd856..dd0800e99 100644 --- a/test/workbox-sw/static/integration/invalid-sw.js +++ b/test/workbox-sw/static/integration/invalid-sw.js @@ -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'; diff --git a/test/workbox-sw/static/integration/valid-sw.js b/test/workbox-sw/static/integration/valid-sw.js index 3d65f7eae..af72cbc6d 100644 --- a/test/workbox-sw/static/integration/valid-sw.js +++ b/test/workbox-sw/static/integration/valid-sw.js @@ -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 = [ diff --git a/test/workbox-webpack-plugin/node/generate-sw.js b/test/workbox-webpack-plugin/node/generate-sw.js index 76e46d5cb..23ba8bd7e 100644 --- a/test/workbox-webpack-plugin/node/generate-sw.js +++ b/test/workbox-webpack-plugin/node/generate-sw.js @@ -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', diff --git a/test/workbox-webpack-plugin/node/inject-manifest.js b/test/workbox-webpack-plugin/node/inject-manifest.js index ba10dd736..61de0ff30 100644 --- a/test/workbox-webpack-plugin/node/inject-manifest.js +++ b/test/workbox-webpack-plugin/node/inject-manifest.js @@ -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',