Skip to content

Commit 292d5e8

Browse files
authored
Fix all sass resolving (codesandbox#621)
* Fix all sass resolving * Remove unused code * Fix indented syntax
1 parent 9693ff3 commit 292d5e8

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed
5.3 KB
Loading
3.16 KB
Loading

packages/app/integration-tests/tests/sandboxes.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const SANDBOXES = [
2525
'github/cssinjs/egghead/tree/master/from-sass-to-cssinjs/templates-and-variables', // postcss egghead
2626
'xp5qy8r93q', // babel example
2727
'angular', // angular template
28+
29+
// Sass importing
30+
'2ppkvzx570', // nested imports
31+
'rl2m3xklyo', // node_modules import
2832
];
2933

3034
function pageLoaded(page) {

packages/app/src/sandbox/compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ async function compile({
270270

271271
const startTime = Date.now();
272272
try {
273+
inject();
273274
clearErrorTransformers();
274275
initializeErrorTransformers();
275276
unmount(manager && manager.webpackHMR ? true : hadError);

packages/app/src/sandbox/eval/transpilers/sass/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ class SassTranspiler extends WorkerTranspiler {
1717
code: string,
1818
loaderContext: LoaderContext
1919
): Promise<{ transpiledCode: string }> {
20-
const extension =
21-
typeof loaderContext.options.indentedSyntax === 'undefined'
22-
? 'scss'
23-
: 'sass';
24-
25-
const customPath = loaderContext.path + '.' + extension;
20+
const indentedSyntax =
21+
loaderContext.options.indentedSyntax == null
22+
? loaderContext.path.endsWith('.sass')
23+
: true;
2624

2725
return new Promise((resolve, reject) => {
2826
if (code === '' || code == null) {
@@ -33,7 +31,8 @@ class SassTranspiler extends WorkerTranspiler {
3331
this.queueTask(
3432
{
3533
code,
36-
path: customPath,
34+
path: loaderContext.path,
35+
indentedSyntax,
3736
},
3837
loaderContext,
3938
(err, data) => {

packages/app/src/sandbox/eval/transpilers/sass/sass-worker.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { join, basename, absolute, dirname } from 'common/utils/path';
33
import delay from 'common/utils/delay';
44

55
self.importScripts([
6-
'https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.10.6/sass.sync.min.js',
6+
process.env.NODE_ENV === 'production'
7+
? 'https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.10.6/sass.sync.min.js'
8+
: 'https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.10.6/sass.sync.js',
79
]);
810

911
self.postMessage('ready');
@@ -27,18 +29,17 @@ const existsPromise = (fs, file) =>
2729
});
2830
});
2931

32+
const pathCaches = new Map();
3033
const getExistingPath = async (fs, p) => {
31-
const underscoredPath = join(dirname(p), '_' + basename(p));
32-
const possiblePaths = [
33-
p,
34-
`${p}.css`,
35-
`${p}.scss`,
36-
`${p}.sass`,
37-
underscoredPath,
38-
`${underscoredPath}.css`,
39-
`${underscoredPath}.scss`,
40-
`${underscoredPath}.sass`,
41-
];
34+
if (p.endsWith('.json')) {
35+
return false;
36+
}
37+
38+
if (pathCaches.get(p)) {
39+
return pathCaches.get(p);
40+
}
41+
42+
const possiblePaths = Sass.getPathVariations(p);
4243

4344
let existedFile = false;
4445

@@ -48,6 +49,8 @@ const getExistingPath = async (fs, p) => {
4849
}
4950
}
5051

52+
pathCaches.set(p, existedFile);
53+
5154
return existedFile;
5255
};
5356

@@ -123,7 +126,7 @@ function initializeBrowserFS() {
123126
let fsInitialized = false;
124127

125128
self.addEventListener('message', async event => {
126-
const { code, path, codesandbox } = event.data;
129+
const { code, path, indentedSyntax, codesandbox } = event.data;
127130

128131
if (!codesandbox) {
129132
return;
@@ -141,16 +144,18 @@ self.addEventListener('message', async event => {
141144
}
142145
}
143146

147+
pathCaches.clear();
148+
Sass._path = '/';
149+
Sass.clearFiles();
150+
144151
// register a custom importer callback
145152
Sass.importer(async (request, done) => {
146153
// eslint-disable-next-line
147154
const fs = BrowserFS.BFSRequire('fs');
148155

149156
try {
150157
const currentPath =
151-
request.previous === 'stdin'
152-
? path
153-
: join(dirname(path), request.previous);
158+
request.previous === 'stdin' ? path : request.previous;
154159

155160
const foundPath = await resolveSass(fs, request.current, currentPath);
156161

@@ -166,8 +171,10 @@ self.addEventListener('message', async event => {
166171
return;
167172
}
168173

169-
done({
170-
content: data.toString(),
174+
Sass.writeFile(foundPath, data.toString(), () => {
175+
done({
176+
path: foundPath,
177+
});
171178
});
172179
});
173180
} catch (e) {
@@ -179,7 +186,7 @@ self.addEventListener('message', async event => {
179186
code,
180187
{
181188
sourceMapEmbed: true,
182-
indentedSyntax: path.endsWith('.sass'),
189+
indentedSyntax,
183190
},
184191
result => {
185192
if (result.status === 0) {

0 commit comments

Comments
 (0)