Skip to content

Commit

Permalink
Improve the code that swaps 'prod' for 'dev' in copyWorkboxLibraries (#…
Browse files Browse the repository at this point in the history
…1488)

* (Slightly) improve the code that swaps 'prod' for 'dev' in filepaths.

* Update some strings.

* Typo.

* String mismatch in a test.

* Sigh... adding @Private
  • Loading branch information
jeffposnick authored May 18, 2018
1 parent 49ec72a commit 90883e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/workbox-build/src/lib/use-build-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,36 @@
limitations under the License.
*/

// TODO (jeffposnick): More flexibility in case naming conventions change.
// This is the build type that is expected to be present "by default".
const DEFAULT_BUILD_TYPE = 'prod';

/**
* Switches a string from using the "default" build type to an alternative
* build type. In practice, this is used to swap out "prod" for "dev" in the
* filename of a Workbox library.
*
* @param {string} source The path to a file, which will normally contain
* DEFAULT_BUILD_TYPE somewhere in it.
* @param {string} buildType The alternative build type value to swap in.
* @return {string} source, with the last occurrence of DEFAULT_BUILD_TYPE
* replaced with buildType.
* @private
*/
module.exports = (source, buildType) => {
return source.replace(DEFAULT_BUILD_TYPE, buildType);
// If we want the DEFAULT_BUILD_TYPE, then just return things as-is.
if (buildType === DEFAULT_BUILD_TYPE) {
return source;
}
// Otherwise, look for the last instance of DEFAULT_BUILD_TYPE, and replace it
// with the new buildType. This is easier via split/join than RegExp.
const parts = source.split(DEFAULT_BUILD_TYPE);
// Join the last two split parts with the new buildType. (If parts only has
// one item, this will be a no-op.)
const replaced = parts.slice(parts.length - 2).join(buildType);
// Take the remaining parts, if any exist, and join them with the replaced
// part using the DEFAULT_BUILD_TYPE, to restore any other matches as-is.
return [
...parts.slice(0, parts.length - 2),
replaced,
].join(DEFAULT_BUILD_TYPE);
};
25 changes: 25 additions & 0 deletions test/workbox-build/node/lib/use-build-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const expect = require('chai').expect;

const useBuildType = require('../../../../packages/workbox-build/src/lib/use-build-type');

describe(`[workbox-build] lib/use-build-type.js`, function() {
it(`should not update anything when buildType is 'prod'`, function() {
const result = useBuildType('/path/to/workbox.prod.js', 'prod');
expect(result).to.eql('/path/to/workbox.prod.js');
});

it(`should update when buildType is 'dev'`, function() {
const result = useBuildType('/path/to/workbox.prod.js', 'dev');
expect(result).to.eql('/path/to/workbox.dev.js');
});

it(`should only update the last match when buildType is 'dev'`, function() {
const result = useBuildType('/path/to/production/and.prod.check/workbox.prod.js', 'dev');
expect(result).to.eql('/path/to/production/and.prod.check/workbox.dev.js');
});

it(`should not update anything if there is no match for the default build type`, function() {
const result = useBuildType('/does/not/match', 'dev');
expect(result).to.eql('/does/not/match');
});
});

0 comments on commit 90883e6

Please sign in to comment.