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

Improve the code that swaps 'prod' for 'dev' in copyWorkboxLibraries #1488

Merged
merged 5 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 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,35 @@
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.
*/
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/also.prod.path/workbox.dev.js');
});

it(`should not update anything is there is no match for the default build type`, function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the first "is" to "if".

const result = useBuildType('/does/not/match', 'dev');
expect(result).to.eql('/does/not/match');
});
});