Skip to content

Commit 9305180

Browse files
nicknisiCompuIves
authored andcommitted
add support for dojo theming (codesandbox#804)
For dojo themes to work properly a special " _key" property should exist on the CSS module JS file that's value is the package name plus the name of the css file. This commit makes the following changes: - add a custom styles transpiler for dojo that adds the " _key" property - Update the dojo preset to use the new styles transpiler - split out the `classesToDefinition` method into its own utility module and import it into the existing and dojo style transpilers
1 parent 1b0f674 commit 9305180

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

packages/app/src/sandbox/eval/presets/dojo/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import typescriptTranspiler from '../../transpilers/typescript';
55
import rawTranspiler from '../../transpilers/raw';
66
import jsonTranspiler from '../../transpilers/json';
77
import stylesTranspiler from '../../transpilers/style';
8+
import dojoStylesTranspiler from './transpilers/style';
89
import babelTranspiler from '../../transpilers/babel';
910

1011
export default function initialize() {
@@ -47,7 +48,7 @@ export default function initialize() {
4748
]);
4849

4950
preset.registerTranspiler(module => /\.m\.css$/.test(module.path), [
50-
{ transpiler: stylesTranspiler, options: { module: true } },
51+
{ transpiler: dojoStylesTranspiler },
5152
]);
5253

5354
preset.registerTranspiler(module => /\.css$/.test(module.path), [
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @flow
2+
import { dispatch } from 'codesandbox-api';
3+
import { StyleTranspiler } from '../../../transpilers/style';
4+
import { type LoaderContext } from '../../../transpiled-module';
5+
import insertCss from '../../../transpilers/style/utils/insert-css';
6+
import toDefinition from '../../../transpilers/style/utils/to-definition';
7+
import getModules from '../../../transpilers/style/get-modules';
8+
9+
const getStyleId = id => id + '-css';
10+
11+
class DojoStyleTranspiler extends StyleTranspiler {
12+
async doTranspilation(code: string, loaderContext: LoaderContext) {
13+
const id = getStyleId(loaderContext._module.getId());
14+
const { path } = loaderContext;
15+
const { code: packageJson } = loaderContext.getModules().find(module => module.path === '/package.json');
16+
const { name: packageName } = JSON.parse(packageJson);
17+
const [, baseName ] = /\/([^/.]*)[^/]*$/.exec(path);
18+
const key = `${packageName}/${baseName}`;
19+
const { css, exportTokens } = await getModules(code, loaderContext);
20+
let result = insertCss(id, css);
21+
result += `\nmodule.exports=${JSON.stringify({ ' _key': key, ...exportTokens })};`
22+
dispatch({ type: 'add-extra-lib', path, code: toDefinition(exportTokens) });
23+
return { transpiledCode: result };
24+
}
25+
}
26+
27+
const transpiler = new DojoStyleTranspiler();
28+
29+
export { DojoStyleTranspiler as StyleTranspiler };
30+
31+
export default transpiler;

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ import Transpiler from '../';
44
import { type LoaderContext } from '../../transpiled-module';
55

66
import insertCss from './utils/insert-css';
7+
import toDefinition from './utils/to-definition';
78
import getModules from './get-modules';
89

910
const getStyleId = id => id + '-css'; // eslint-disable-line
1011

11-
function classesToDefinition(classes): string {
12-
return Object.keys(classes).reduce(
13-
(previous, className) => previous + `export const ${className}: string;\n`,
14-
''
15-
);
16-
}
17-
1812
class StyleTranspiler extends Transpiler {
1913
constructor() {
2014
super('style-loader');
@@ -42,7 +36,7 @@ class StyleTranspiler extends Transpiler {
4236
dispatch({
4337
type: 'add-extra-lib',
4438
path,
45-
code: classesToDefinition(exportTokens),
39+
code: toDefinition(exportTokens),
4640
});
4741
return Promise.resolve({ transpiledCode: result });
4842
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @flow
2+
3+
export default function toDefinition(classes): string {
4+
return Object.keys(classes).reduce(
5+
(previous, className) => previous + `export const ${className}: string;\n`,
6+
''
7+
);
8+
}

0 commit comments

Comments
 (0)