Skip to content

Commit

Permalink
Babel plugin to mock css imports
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Oct 31, 2023
1 parent a260842 commit 8f22bf9
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 60 deletions.
1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-dark-components.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-dark-palette.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-dark-semantic-editor.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-dark-semantic-grid.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-dark-semantic.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/css/mock-theme-light-palette.js

This file was deleted.

3 changes: 0 additions & 3 deletions __mocks__/css/mock-theme-spectrum-alias.module.js

This file was deleted.

3 changes: 0 additions & 3 deletions __mocks__/css/mock-theme-spectrum-overrides.module.js

This file was deleted.

3 changes: 0 additions & 3 deletions __mocks__/css/mock-theme-spectrum-palette.module.js

This file was deleted.

16 changes: 7 additions & 9 deletions jest.config.base.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ const path = require('path');

module.exports = {
transform: {
'.(ts|tsx|js|jsx)': ['babel-jest', { rootMode: 'upward' }],
'.(ts|tsx|js|jsx)': [
'babel-jest',
{
rootMode: 'upward',
plugins: ['@deephaven/babel-preset/mockCssImportPlugin'],
},
],
},
// Makes jest transform monaco, but continue ignoring other node_modules. Used for MonacoUtils test
transformIgnorePatterns: [
'node_modules/(?!(monaco-editor|d3-interpolate|d3-color)/)',
],
moduleNameMapper: {
'theme-([^/]+?)\\.css(\\?(?:inline|raw))?$': path.join(
__dirname,
'./__mocks__/css/mock-theme-$1.js'
),
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
'\\.(css|less|scss|sass)\\?(?:inline|raw)$': path.join(
__dirname,
'./__mocks__/fileMock.js'
),
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
path.join(__dirname, './__mocks__/fileMock.js'),
'^fira$': 'identity-obj-proxy',
Expand Down
75 changes: 75 additions & 0 deletions packages/babel-preset/mockCssImportPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const CSS_INLINE_IMPORT_REGEX = /\.s?css\?(inline|raw)$/;
const CSS_MODULE_IMPORT_REGEX = /([^/]+)\.module\.s?css/;

/**
* Babel transform plugin to mock CSS imports for unit testing purposes. This
* can be passed to `babel-jest` transform as a plugin.
*
* e.g.
*
* transform: {
* '.(ts|tsx|js|jsx)': [
* 'babel-jest',
* {
* rootMode: 'upward',
* plugins: [
* '@deephaven/babel-preset/mockCssImportPlugin',
* ],
* },
* ],
* },
*/
module.exports = function cssTransformerPlugin({ types: t }) {
return {
name: 'transform-mock-css-import',
visitor: {
/** Match import declarations */
ImportDeclaration(path) {
const { node } = path;

const [, moduleName] =
CSS_MODULE_IMPORT_REGEX.exec(node.source.value) || [];

// Replace CSS module import with a const object declaration. Object
// will have single key / value derived from the module name.
// e.g.
// import saturn from './saturn.module.css';
// becomes
// const styles = { 'saturn': 'saturn' };
if (moduleName) {
const { name } = node.specifiers[0].local;

path.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(name),
t.objectExpression([
t.objectProperty(
t.identifier(`'${moduleName}'`),
t.stringLiteral(moduleName)
),
])
),
])
);
}
// Replace (s)css imports that have ?inline or ?raw query strings with a
// const variable declaration.
// e.g.
// import './saturn.css?inline';
// becomes
// const saturn = './saturn.css?inline';
else if (CSS_INLINE_IMPORT_REGEX.test(node.source.value)) {
const { name } = node.specifiers[0].local;
const { value } = node.source;

path.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(name), t.stringLiteral(value)),
])
);
}
},
},
};
};
12 changes: 6 additions & 6 deletions packages/components/src/__snapshots__/SpectrumUtils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`themeDHDefault should merge Spectrum default with DH custom styles 1`] = `
{
"dark": {
"dh-spectrum-alias": "mock-dh-spectrum-alias",
"dh-spectrum-overrides": "mock-dh-spectrum-overrides",
"dh-spectrum-palette": "mock-dh-spectrum-palette",
"spectrum--darkest": "spectrum--darkest_256eeb",
"theme-spectrum-alias": "theme-spectrum-alias",
"theme-spectrum-overrides": "theme-spectrum-overrides",
"theme-spectrum-palette": "theme-spectrum-palette",
},
"global": {
"spectrum": "spectrum_9e130c",
Expand All @@ -21,10 +21,10 @@ exports[`themeDHDefault should merge Spectrum default with DH custom styles 1`]
"spectrum--large": "spectrum--large_c40598",
},
"light": {
"dh-spectrum-alias": "mock-dh-spectrum-alias",
"dh-spectrum-overrides": "mock-dh-spectrum-overrides",
"dh-spectrum-palette": "mock-dh-spectrum-palette",
"spectrum--light": "spectrum--light_a40724",
"theme-spectrum-alias": "theme-spectrum-alias",
"theme-spectrum-overrides": "theme-spectrum-overrides",
"theme-spectrum-palette": "theme-spectrum-palette",
},
"medium": {
"spectrum--medium": "spectrum--medium_4b172c",
Expand Down
12 changes: 6 additions & 6 deletions packages/components/src/theme/ThemeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,17 @@ describe('getDefaultBaseThemes', () => {
name: 'Default Dark',
themeKey: 'default-dark',
styleContent: [
'mock-theme-dark-palette',
'mock-theme-dark-semantic',
'mock-theme-dark-semantic-editor',
'mock-theme-dark-semantic-grid',
'mock-theme-dark-components',
'./theme-dark-palette.css?raw',
'./theme-dark-semantic.css?raw',
'./theme-dark-semantic-editor.css?raw',
'./theme-dark-semantic-grid.css?raw',
'./theme-dark-components.css?raw',
].join('\n'),
},
{
name: 'Default Light',
themeKey: 'default-light',
styleContent: 'mock-theme-light-palette',
styleContent: './theme-light-palette.css?raw',
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ exports[`ThemeProvider setSelectedThemeKey: [ [Object] ] should change selected
<style
data-theme-key="default-light"
>
mock-theme-light-palette
./theme-light-palette.css?raw
</style>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -28,17 +28,17 @@ exports[`ThemeProvider setSelectedThemeKey: [ [Object] ] should change selected
<style
data-theme-key="default-dark"
>
mock-theme-dark-palette
mock-theme-dark-semantic
mock-theme-dark-semantic-editor
mock-theme-dark-semantic-grid
mock-theme-dark-components
./theme-dark-palette.css?raw
./theme-dark-semantic.css?raw
./theme-dark-semantic-editor.css?raw
./theme-dark-semantic-grid.css?raw
./theme-dark-components.css?raw
</style>
<style
data-theme-key="themeA"
/>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -55,7 +55,7 @@ exports[`ThemeProvider setSelectedThemeKey: null should change selected theme: d
<body>
<div>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -72,7 +72,7 @@ exports[`ThemeProvider setSelectedThemeKey: null should change selected theme: t
<body>
<div>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -91,17 +91,17 @@ exports[`ThemeProvider should load themes based on preload data or default: [ [O
<style
data-theme-key="default-dark"
>
mock-theme-dark-palette
mock-theme-dark-semantic
mock-theme-dark-semantic-editor
mock-theme-dark-semantic-grid
mock-theme-dark-components
./theme-dark-palette.css?raw
./theme-dark-semantic.css?raw
./theme-dark-semantic-editor.css?raw
./theme-dark-semantic-grid.css?raw
./theme-dark-components.css?raw
</style>
<style
data-theme-key="themeA"
/>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -120,14 +120,14 @@ exports[`ThemeProvider should load themes based on preload data or default: [ [O
<style
data-theme-key="default-dark"
>
mock-theme-dark-palette
mock-theme-dark-semantic
mock-theme-dark-semantic-editor
mock-theme-dark-semantic-grid
mock-theme-dark-components
./theme-dark-palette.css?raw
./theme-dark-semantic.css?raw
./theme-dark-semantic-editor.css?raw
./theme-dark-semantic-grid.css?raw
./theme-dark-components.css?raw
</style>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -144,7 +144,7 @@ exports[`ThemeProvider should load themes based on preload data or default: null
<body>
<div>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand All @@ -161,7 +161,7 @@ exports[`ThemeProvider should load themes based on preload data or default: null
<body>
<div>
<div
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 mock-dh-spectrum-palette mock-dh-spectrum-alias mock-dh-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
class="spectrum_b37d53 spectrum_2a241c spectrum--light_a40724 theme-spectrum-palette theme-spectrum-alias theme-spectrum-overrides spectrum--large_c40598 spectrum_9e130c spectrum--medium_9e130c spectrum--large_9e130c spectrum--darkest_9e130c spectrum--dark_9e130c spectrum--light_9e130c spectrum--lightest_9e130c"
dir="ltr"
lang="en-US"
style="isolation: isolate; color-scheme: light;"
Expand Down

0 comments on commit 8f22bf9

Please sign in to comment.