UNPKG

2.29 kBJavaScriptView Raw
1'use strict';
2
3var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
4
5var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
9/*
10 Copyright 2017 Google Inc.
11
12 Licensed under the Apache License, Version 2.0 (the "License");
13 you may not use this file except in compliance with the License.
14 You may obtain a copy of the License at
15
16 https://www.apache.org/licenses/LICENSE-2.0
17
18 Unless required by applicable law or agreed to in writing, software
19 distributed under the License is distributed on an "AS IS" BASIS,
20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 See the License for the specific language governing permissions and
22 limitations under the License.
23*/
24
25// This is the build type that is expected to be present "by default".
26var DEFAULT_BUILD_TYPE = 'prod';
27
28/**
29 * Switches a string from using the "default" build type to an alternative
30 * build type. In practice, this is used to swap out "prod" for "dev" in the
31 * filename of a Workbox library.
32 *
33 * @param {string} source The path to a file, which will normally contain
34 * DEFAULT_BUILD_TYPE somewhere in it.
35 * @param {string} buildType The alternative build type value to swap in.
36 * @return {string} source, with the last occurrence of DEFAULT_BUILD_TYPE
37 * replaced with buildType.
38 * @private
39 */
40module.exports = function (source, buildType) {
41 // If we want the DEFAULT_BUILD_TYPE, then just return things as-is.
42 if (buildType === DEFAULT_BUILD_TYPE) {
43 return source;
44 }
45 // Otherwise, look for the last instance of DEFAULT_BUILD_TYPE, and replace it
46 // with the new buildType. This is easier via split/join than RegExp.
47 var parts = source.split(DEFAULT_BUILD_TYPE);
48 // Join the last two split parts with the new buildType. (If parts only has
49 // one item, this will be a no-op.)
50 var replaced = parts.slice(parts.length - 2).join(buildType);
51 // Take the remaining parts, if any exist, and join them with the replaced
52 // part using the DEFAULT_BUILD_TYPE, to restore any other matches as-is.
53 return [].concat((0, _toConsumableArray3.default)(parts.slice(0, parts.length - 2)), [replaced]).join(DEFAULT_BUILD_TYPE);
54};
\No newline at end of file