mirror of
https://github.com/locomotivemtl/locomotive-boilerplate.git
synced 2026-01-15 00:55:08 +08:00
Improve HTTPS/Proxy URL support in watch.js
Added logic to prepend "https://" to the proxy URL if missing.
This commit is contained in:
@@ -9,6 +9,9 @@ import resolve from './utils/template.js';
|
|||||||
import browserSync from 'browser-sync';
|
import browserSync from 'browser-sync';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
|
|
||||||
|
// Match a URL protocol.
|
||||||
|
const regexUrlStartsWithProtocol = /^[a-z0-9\-]:\/\//i;
|
||||||
|
|
||||||
// Build scripts, compile styles, concat files,
|
// Build scripts, compile styles, concat files,
|
||||||
// and generate spritesheets on first hit
|
// and generate spritesheets on first hit
|
||||||
concatFiles(...developmentConcatFilesArgs);
|
concatFiles(...developmentConcatFilesArgs);
|
||||||
@@ -113,18 +116,29 @@ function createServerOptions({
|
|||||||
notify: false
|
notify: false
|
||||||
};
|
};
|
||||||
|
|
||||||
// Resolve the URI for the BrowserSync server
|
// Resolve the URL for the BrowserSync server
|
||||||
if (typeof paths.url === 'string' && paths.url.length > 0) {
|
if (isNonEmptyString(paths.url)) {
|
||||||
// Use proxy
|
// Use proxy
|
||||||
config.proxy = paths.url;
|
config.proxy = paths.url;
|
||||||
} else if (typeof paths.dest === 'string' && paths.dest.length > 0) {
|
} else if (isNonEmptyString(paths.dest)) {
|
||||||
// Use base directory
|
// Use base directory
|
||||||
config.server = {
|
config.server = {
|
||||||
baseDir: paths.dest
|
baseDir: paths.dest
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return merge(config, resolve(options));
|
merge(config, resolve(options));
|
||||||
|
|
||||||
|
// If HTTPS is enabled, prepend `https://` to proxy URL
|
||||||
|
if (options?.https) {
|
||||||
|
if (isNonEmptyString(config.proxy?.target)) {
|
||||||
|
config.proxy.target = prependSchemeToUrl(config.proxy.target, 'https');
|
||||||
|
} else if (isNonEmptyString(config.proxy)) {
|
||||||
|
config.proxy = prependSchemeToUrl(config.proxy, 'https');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,3 +167,29 @@ function createViewsArray(views) {
|
|||||||
'Expected \'views\' to be a string, array, or object'
|
'Expected \'views\' to be a string, array, or object'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepends the scheme to the URL.
|
||||||
|
*
|
||||||
|
* @param {string} url - The URL to mutate.
|
||||||
|
* @param {string} [scheme] - The URL scheme to prepend.
|
||||||
|
* @return {string} Returns the mutated URL.
|
||||||
|
*/
|
||||||
|
function prependSchemeToUrl(url, scheme = 'http') {
|
||||||
|
if (regexUrlStartsWithProtocol.test(url)) {
|
||||||
|
return url.replace(regexUrlStartsWithProtocol, `${scheme}://`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${scheme}://${url}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether the passed value is a string with at least one character.
|
||||||
|
*
|
||||||
|
* @param {*} value - The value to be checked.
|
||||||
|
* @return {boolean} Returns `true` if the value is a non-empty string,
|
||||||
|
* otherwise `false`.
|
||||||
|
*/
|
||||||
|
function isNonEmptyString(value) {
|
||||||
|
return (typeof value === 'string' && value.length > 0);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user