From 26b9caa8e2fccf81224218c7d168c89085498727 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 7 Jun 2021 17:58:40 +0200 Subject: [PATCH 1/2] dont shortcut to request.path --- .../transpilers/sass/worker/sass-worker.js | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js b/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js index 20dbff7c784..895f6862159 100644 --- a/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js +++ b/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js @@ -18,18 +18,12 @@ declare var Sass: { registerPlugin: (name: string, plugin: Function) => void, }; -const resolveAsyncModule = ( +function resolveAsyncModule( path: string, - { ignoredExtensions }?: { ignoredExtensions?: Array } -) => - new Promise((r, reject) => { + { ignoredExtensions }: { ignoredExtensions?: Array } = {} +) { + return new Promise((r, reject) => { const sendId = Math.floor(Math.random() * 10000); - self.postMessage({ - type: 'resolve-async-transpiled-module', - path, - id: sendId, - options: { isAbsolute: true, ignoredExtensions }, - }); const resolveFunc = message => { const { type, id, found } = message.data; @@ -48,12 +42,20 @@ const resolveAsyncModule = ( }; self.addEventListener('message', resolveFunc); + + self.postMessage({ + type: 'resolve-async-transpiled-module', + path, + id: sendId, + options: { isAbsolute: true, ignoredExtensions }, + }); }); +} const SUPPORTED_EXTS = ['scss', 'sass', 'css']; -const existsPromise = (fs, file) => - new Promise(r => { +function existsPromise(fs, file) { + return new Promise(r => { fs.stat(file, async (err, stats) => { if (err || stats.isDirectory()) { if (stats && stats.isDirectory()) { @@ -82,6 +84,7 @@ const existsPromise = (fs, file) => } }); }); +} /** * Return and stop as soon as one promise returns a truthy value @@ -116,25 +119,23 @@ const getExistingPath = async (fs, p) => { }; const resolvedCache = {}; -const resolveSass = (fs, p, path) => { +async function resolveSass(fs, p, path) { const usedPath = p.startsWith('~') ? p.replace('~', '/node_modules/') : p; const sourceDir = dirname(path); resolvedCache[sourceDir] = resolvedCache[sourceDir] || {}; if (resolvedCache[sourceDir][usedPath]) { - return Promise.resolve(resolvedCache[sourceDir][usedPath]); + return resolvedCache[sourceDir][usedPath]; } - return new Promise((r, reject) => { - const directPath = join(sourceDir, usedPath); - - // First try to do the relative path, as a performance optimization - getExistingPath(fs, directPath).then(foundPath => { - if (foundPath) { - r(foundPath); - return; - } + const directPath = usedPath[0] === '/' ? usedPath : join(sourceDir, usedPath); + // First try to do the relative path, as a performance optimization + const existingPath = await getExistingPath(fs, directPath); + if (existingPath) { + resolvedCache[sourceDir][usedPath] = existingPath; + } else { + await new Promise((promiseResolve, promiseReject) => { resolve( usedPath, { @@ -165,23 +166,21 @@ const resolveSass = (fs, p, path) => { async (err, resolvedPath) => { if (err) { if (/^\w/.test(p)) { - r(resolveSass(fs, '.' + absolute(p), path)); + promiseResolve(resolveSass(fs, '.' + absolute(p), path)); + } else { + promiseReject(err); } - - reject(err); } else { const newFoundPath = await getExistingPath(fs, resolvedPath); - - r(newFoundPath); + promiseResolve(newFoundPath); } } ); }); - }).then(result => { - resolvedCache[sourceDir][usedPath] = result; - return result; - }); -}; + } + + return resolvedCache[sourceDir][usedPath]; +} function initializeBrowserFS() { return new Promise(res => { @@ -232,8 +231,8 @@ self.addEventListener('message', async event => { const currentPath = request.previous === 'stdin' ? path : request.previous; - const foundPath = - request.path || (await resolveSass(fs, request.current, currentPath)); + // request.path sometimes returns a partially resolved path + const foundPath = await resolveSass(fs, request.current, currentPath); self.postMessage({ type: 'add-transpilation-dependency', From 6c1715b82994ca4324089a99c551d9a01faaf3e3 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 7 Jun 2021 17:59:42 +0200 Subject: [PATCH 2/2] tweak comment a bit more --- .../app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js b/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js index 895f6862159..f4bdf10e587 100644 --- a/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js +++ b/packages/app/src/sandbox/eval/transpilers/sass/worker/sass-worker.js @@ -232,6 +232,7 @@ self.addEventListener('message', async event => { request.previous === 'stdin' ? path : request.previous; // request.path sometimes returns a partially resolved path + // See: https://github.com/codesandbox/codesandbox-client/issues/4865 const foundPath = await resolveSass(fs, request.current, currentPath); self.postMessage({