fix logic. It's more lines of code but safer.

This commit is contained in:
Dmitriy Lyner 2018-05-30 11:07:22 -06:00
parent 083face122
commit f1ea23c229

View file

@ -95,6 +95,7 @@ function createLoaderRules(languages, features, workers, publicPath) {
[label]: `${publicPath ? `${stripTrailingSlash(publicPath)}/` : ''}${output}`, [label]: `${publicPath ? `${stripTrailingSlash(publicPath)}/` : ''}${output}`,
}), {}); }), {});
const getWorkerPath = (workerPath) => {
const getBasePath = () => { const getBasePath = () => {
const bases = document.getElementsByTagName('base'); const bases = document.getElementsByTagName('base');
let contextPath = '/'; let contextPath = '/';
@ -102,17 +103,24 @@ function createLoaderRules(languages, features, workers, publicPath) {
contextPath = bases[0].getAttribute('context') || bases[0].href || '/'; contextPath = bases[0].getAttribute('context') || bases[0].href || '/';
} }
return contextPath; return contextPath;
};
const basePath = getBasePath();
if (basePath[basePath.length - 1] !== '/') {
basePath = `${basePath}/`;
}
if (workerPath[0] === '/') {
workerPath = workerPath.substr(1);
} }
const getWorkerPath = (workerPath) => { const contextPath = basePath + workerPath;
return `${getBasePath()}/${workerPath}`.replace('//', '/'); return contextPath;
} };
const globals = { const globals = {
'MonacoEnvironment': `((paths) => ({ getWorkerUrl: (moduleId, label) => ${getWorkerPath(paths[label])}}))(${ 'MonacoEnvironment': `((paths) => ({ getWorkerUrl: (moduleId, label) => (${getWorkerPath.toString()})(paths[label]) }))(${JSON.stringify(workerPaths, null, 2)})`
JSON.stringify(workerPaths, null, 2)
})`,
}; };
return [ return [
{ {
test: MONACO_EDITOR_API_PATHS, test: MONACO_EDITOR_API_PATHS,