Skip to content

Commit 6a815f3

Browse files
authored
Fix preact css loading (codesandbox#308)
1 parent 010b199 commit 6a815f3

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lerna run build --scope app --scope homepage --parallel && gulp",
1010
"build:dependents":
1111
"lerna run build --ignore app --ignore homepage --parallel",
12-
"start": "lerna run start",
12+
"start": "lerna run start --parallel",
1313
"start:test": "lerna run start:test --scope app --stream",
1414
"test": "lerna run test",
1515
"test:integrations": "lerna run test:integrations --scope app --stream",

packages/app/src/sandbox/eval/presets/preact-cli/index.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,43 @@ preactPreset.registerTranspiler(module => /\.jsx?$/.test(module.path), [
5050
},
5151
]);
5252

53-
preactPreset.registerTranspiler(module => /\.s[a|c]ss/.test(module.path), [
54-
{ transpiler: sassTranspiler },
55-
{ transpiler: stylesTranspiler },
56-
]);
53+
// For these routes we need to enable css modules
54+
const cssModulesPaths = [
55+
'/src/components',
56+
'/components',
57+
'/src/routes',
58+
'/routes',
59+
];
5760

58-
preactPreset.registerTranspiler(module => /\.less/.test(module.path), [
59-
{ transpiler: lessTranspiler },
60-
{ transpiler: stylesTranspiler },
61-
]);
61+
const cssModulesRegex = extension =>
62+
new RegExp(`^(${cssModulesPaths.join('|')})\/.*\.${extension}$`);
63+
64+
const cssTypes = {
65+
css: [],
66+
's[a|c]ss': [sassTranspiler],
67+
less: [lessTranspiler],
68+
styl: [stylusTranspiler],
69+
};
70+
71+
Object.keys(cssTypes).forEach(cssType => {
72+
preactPreset.registerTranspiler(
73+
module => cssModulesRegex(cssType).test(module.path),
74+
[
75+
...cssTypes[cssType],
76+
{ transpiler: stylesTranspiler, options: { module: true } },
77+
]
78+
);
79+
80+
preactPreset.registerTranspiler(
81+
module => new RegExp(`\.${cssType}$`).test(module.path),
82+
[...cssTypes[cssType], { transpiler: stylesTranspiler }]
83+
);
84+
});
6285

6386
preactPreset.registerTranspiler(module => /\.json/.test(module.path), [
6487
{ transpiler: jsonTranspiler },
6588
]);
6689

67-
preactPreset.registerTranspiler(module => /\.styl/.test(module.path), [
68-
{ transpiler: stylusTranspiler },
69-
{ transpiler: stylesTranspiler },
70-
]);
71-
7290
// Support for !async statements
7391
preactPreset.registerTranspiler(
7492
() => false /* never load without explicit statement */,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class StyleTranspiler extends Transpiler {
2828
if (loaderContext.options.module) {
2929
return getModules(code, loaderContext).then(({ css, exportTokens }) => {
3030
let result = insertCss(id, css);
31-
result += `\nexports=${JSON.stringify(exportTokens)};`;
31+
result += `\nmodule.exports=${JSON.stringify(exportTokens)};`;
3232

3333
return Promise.resolve({ transpiledCode: result });
3434
});

packages/codesandbox-api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"build:clean": "tsc && rollup -c",
2323
"build":
2424
"tsc && rollup -c && rimraf compiled && typedoc --out dist/docs --target es6 --theme minimal src",
25-
"start": "nodemon -e ts --exec \"npm run build:clean\"",
25+
"start":
26+
"nodemon --watch \"src/**/*.ts\" -e ts --exec \"npm run build:clean\"",
2627
"test": "echo TODO",
2728
"test:watch": "jest --watch",
2829
"test:prod": "npm run lint && npm run test -- --coverage --no-cache",

0 commit comments

Comments
 (0)