Skip to content

Commit

Permalink
Merge pull request #101 from nvh95/sass-enhancement
Browse files Browse the repository at this point in the history
Sass enhancement
  • Loading branch information
nvh95 authored May 29, 2022
2 parents ca4066d + 5aeae73 commit b55fe71
Show file tree
Hide file tree
Showing 32 changed files with 258 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.2.4

## Fixes

- Sass enhancement

# 0.2.3

## Fixes
Expand Down
1 change: 1 addition & 0 deletions config/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ jestPreviewConfigure({
externalCss: ['demo/global.css', 'demo/assets/_scss/global-style.scss'],
publicFolder: 'demo/public',
autoPreview: true,
sassLoadPaths: ['demo/assets/_scss/loadPathsExample'],
});

window.matchMedia = (query) => ({
Expand Down
11 changes: 11 additions & 0 deletions demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components';
import { css } from '@emotion/react';
import emotionStyled from '@emotion/styled';
import { styled as stichesStyled } from '@stitches/react';
import '~animate.css/animate.css';

import logo2 from './assets/images/logo.svg';
import './App.css';
Expand Down Expand Up @@ -46,6 +47,16 @@ function App() {
{/* Reference: https://developer.chrome.com/blog/css-in-js/ */}
{/* https://wicg.github.io/construct-stylesheets/ */}
<StichesP>Styled by Stiches</StichesP>
<p className="load-path-sass">
This text is styled by SASS from load paths
</p>
<p className="animate__animated animate__bounce">
An animated element style using @use ~
</p>
<div className="animated fadeIn">
<p>An animated element style using import ~</p>
<p>Watch me fade in!</p>
</div>
<p>
<button
data-testid="increase"
Expand Down
2 changes: 1 addition & 1 deletion demo/__tests__/transform.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('transform', () => {
it('should generate snapshots correctly in different OS', () => {
render(<App />);
expect(document.body.outerHTML).toMatchInlineSnapshot(
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><img src=\\"/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\"><img src=\\"/demo/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Hello Vite + React!</p><p class=\\"sc-bczRLJ dgihId\\">This text is styled by styled-components</p><p class=\\"global-css\\">This text is styled by global css which is not imported to App.tsx</p><p class=\\"_cssModule_16r0j_1\\">This text is styled by CSS Modules</p><p class=\\"global-configured-sass\\">This text is styled by global configured SASS</p><p class=\\"imported-sass\\">This text is styled by imported SASS</p><button class=\\"css-s689uo-App\\">Hover to change color.</button><p class=\\"css-2m18qq\\">Styled by Emotion</p><p class=\\"c-gqdJwI\\">Styled by Stiches</p><p><button data-testid=\\"increase\\" type=\\"button\\">count is: <div data-testid=\\"count\\">0</div></button></p><p>Edit <code>App.tsx</code> and save to test HMR updates.</p><p><a class=\\"App-link\\" href=\\"https://reactjs.org\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Learn React</a> | <a class=\\"App-link\\" href=\\"https://vitejs.dev/guide/features.html\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Vite Docs</a></p></header></div></div></body>"`,
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><img src=\\"/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\"><img src=\\"/demo/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Hello Vite + React!</p><p class=\\"sc-bczRLJ dgihId\\">This text is styled by styled-components</p><p class=\\"global-css\\">This text is styled by global css which is not imported to App.tsx</p><p class=\\"_cssModule_16r0j_1\\">This text is styled by CSS Modules</p><p class=\\"global-configured-sass\\">This text is styled by global configured SASS</p><p class=\\"imported-sass\\">This text is styled by imported SASS</p><button class=\\"css-s689uo-App\\">Hover to change color.</button><p class=\\"css-2m18qq\\">Styled by Emotion</p><p class=\\"c-gqdJwI\\">Styled by Stiches</p><p class=\\"load-path-sass\\">This text is styled by SASS from load paths</p><p class=\\"animate__animated animate__bounce\\">An animated element style using @use ~</p><div class=\\"animated fadeIn\\"><p>An animated element style using import ~</p><p>Watch me fade in!</p></div><p><button data-testid=\\"increase\\" type=\\"button\\">count is: <div data-testid=\\"count\\">0</div></button></p><p>Edit <code>App.tsx</code> and save to test HMR updates.</p><p><a class=\\"App-link\\" href=\\"https://reactjs.org\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Learn React</a> | <a class=\\"App-link\\" href=\\"https://vitejs.dev/guide/features.html\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Vite Docs</a></p></header></div></div></body>"`,
);
});
});
5 changes: 5 additions & 0 deletions demo/assets/_scss/loadPathsExample/load-path.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.load-path-sass {
color: red;
}
}
4 changes: 4 additions & 0 deletions demo/assets/_scss/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@use 'partials/partial-style'; // Example using @use
@use 'load-path'; // Example of loadPath config

$use-fadeIn: true;
@import '~animate-sass/animate'; // Example of import ~

header {
.imported-sass {
Expand Down
5 changes: 5 additions & 0 deletions examples/create-react-app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Use SASS_PATH causes CRA throw 1 warning, it looks like this is an issue of CRA
Reference: https://github.com/facebook/create-react-app/issues/12329 -->

SASS_PATH=src/assets/\_scss/loadPathsExample
NODE_PATH=node_modules/
4 changes: 3 additions & 1 deletion examples/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
]
},
"devDependencies": {
"animate-sass": "^0.8.2",
"jest-preview": "file:../..",
"npm-run-all": "^4.1.5"
"npm-run-all": "^4.1.5",
"sass": "^1.50.0"
}
}
12 changes: 12 additions & 0 deletions examples/create-react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styles from './style.module.css';

import './App.css';
import './assets/css/App.css';
import './assets/_scss/style.scss';

function App() {
const [count, setCount] = useState(0);
Expand All @@ -17,6 +18,17 @@ function App() {
<img src={logo2} className="logo2" alt="logo2" />
<p>Create React App example</p>
<p className={styles.textOrange}>Styled by CSS Modules</p>
<p className="global-configured-sass">
This text is styled by global configured SASS
</p>
<p className="imported-sass">This text is styled by imported SASS</p>
<p className="load-path-sass">
This text is styled by SASS from load paths
</p>
<div className="animated fadeIn">
<p>An animated element style using @import ~</p>
<p>Watch me fade in!</p>
</div>
<button
data-testid="increase"
type="button"
Expand Down
2 changes: 1 addition & 1 deletion examples/create-react-app/src/__tests__/transform.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('transform', () => {
render(<App />);

expect(document.body.outerHTML).toMatchInlineSnapshot(
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><svg class=\\"svg-component\\">/src/logo.svg</svg><img src=\\"/src/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\"><img src=\\"/src/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Create React App example</p><p class=\\"_textOrange_1gpw2_1\\">Styled by CSS Modules</p><button data-testid=\\"increase\\" type=\\"button\\">count is: <div data-testid=\\"count\\">0</div></button><a class=\\"App-link\\" href=\\"https://reactjs.org\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Learn React</a></header></div></div></body>"`,
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><svg class=\\"svg-component\\">/src/logo.svg</svg><img src=\\"/src/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\"><img src=\\"/src/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Create React App example</p><p class=\\"_textOrange_1gpw2_1\\">Styled by CSS Modules</p><p class=\\"global-configured-sass\\">This text is styled by global configured SASS</p><p class=\\"imported-sass\\">This text is styled by imported SASS</p><p class=\\"load-path-sass\\">This text is styled by SASS from load paths</p><div class=\\"animated fadeIn\\"><p>An animated element style using @import ~</p><p>Watch me fade in!</p></div><button data-testid=\\"increase\\" type=\\"button\\">count is: <div data-testid=\\"count\\">0</div></button><a class=\\"App-link\\" href=\\"https://reactjs.org\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">Learn React</a></header></div></div></body>"`,
);
});
});
7 changes: 7 additions & 0 deletions examples/create-react-app/src/assets/_scss/global-style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import 'partials/partial-global-style'; // Example using @import

header {
.global-configured-sass {
color: yellow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.load-path-sass {
color: red;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.global-configured-sass {
text-transform: uppercase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.imported-sass {
text-transform: uppercase;
}
}
11 changes: 11 additions & 0 deletions examples/create-react-app/src/assets/_scss/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@use 'partials/partial-style'; // Example using @use
@use 'load-path'; // Example of loadPath config

$use-fadeIn: true;
@import '~animate-sass/animate'; // Example of import ~

header {
.imported-sass {
color: pink;
}
}
3 changes: 2 additions & 1 deletion examples/create-react-app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './assets/_scss/global-style.scss';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
document.getElementById('root'),
);

// If you want to start measuring performance in your app, pass a function
Expand Down
3 changes: 2 additions & 1 deletion examples/create-react-app/src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import '@testing-library/jest-dom';
import { jestPreviewConfigure } from 'jest-preview';

jestPreviewConfigure({
externalCss: ['src/index.css'],
externalCss: ['src/index.css', 'src/assets/_scss/global-style.scss'],
sassLoadPaths: ['src/assets/_scss/loadPathsExample'],
});

window.matchMedia = (query) => ({
Expand Down
1 change: 1 addition & 0 deletions examples/vite-react/config/jest/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { jestPreviewConfigure } from 'jest-preview';
jestPreviewConfigure({
externalCss: ['src/index.css', 'src/assets/_scss/global-style.scss'],
autoPreview: true,
sassLoadPaths: ['src/assets/_scss/loadPathsExample'],
});

window.matchMedia = (query) => ({
Expand Down
2 changes: 2 additions & 0 deletions examples/vite-react/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = {
'^react-native$': 'react-native-web',
// Used to dedupe `styled-component` when run `npm link` in development
'^styled-components$': '<rootDir>/node_modules/styled-components',
// Support import ~
'^~(.*)': '<rootDir>/node_modules/$1',
},
moduleFileExtensions: [
// Place tsx and ts to beginning as suggestion from Jest team
Expand Down
2 changes: 2 additions & 0 deletions examples/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10",
"@vitejs/plugin-react": "^1.0.7",
"animate-sass": "^0.8.2",
"animate.css": "^4.1.1",
"jest": "^27.5.1",
"jest-preview": "file:../..",
"jest-watch-typeahead": "^1.0.0",
Expand Down
11 changes: 11 additions & 0 deletions examples/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from 'react';
import styled from 'styled-components';
import '~animate.css/animate.css';

import logo2 from './assets/images/logo.svg';

Expand All @@ -23,6 +24,16 @@ function App() {
This text is styled by global configured SASS
</p>
<p className="imported-sass">This text is styled by imported SASS</p>
<p className="load-path-sass">
This text is styled by SASS from load paths
</p>
<p className="animate__animated animate__bounce">
An animated element style using @use ~
</p>
<div className="animated fadeIn">
<p>An animated element style using import ~</p>
<p>Watch me fade in!</p>
</div>
<p>
<button
data-testid="increase"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
header {
.load-path-sass {
color: red;
}
}
4 changes: 4 additions & 0 deletions examples/vite-react/src/assets/_scss/style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@use 'partials/partial-style'; // Example using @use
@use 'load-path'; // Example of loadPath config

$use-fadeIn: true;
@import '~animate-sass/animate'; // Example of import ~

header {
.imported-sass {
Expand Down
17 changes: 17 additions & 0 deletions examples/vite-react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,22 @@ export default defineConfig(({ mode }) => {
// If `envWithProcessPrefix` is an empty object, `process.env` will be undefined and the app cannot be loaded
// Caveat: Cannot access `process.env` in build mode, always use `process.env.VARIABLE_NAME`
define: envWithProcessPrefix,
// Support tilde import
resolve: {
alias: {
'~animate-sass': path.resolve(__dirname, 'node_modules/animate-sass'),
'~animate.css': path.resolve(__dirname, 'node_modules/animate.css'),
},
},
// Support loadPaths for scss
css: {
preprocessorOptions: {
scss: {
includePaths: [
path.resolve(__dirname, 'demo/assets/_scss/loadPathsExample'),
],
},
},
},
};
});
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = {
modulePaths: ['<rootDir>/demo'],
moduleNameMapper: {
'^react-native$': 'react-native-web',
// Support import ~
'^~(.*)': '<rootDir>/node_modules/$1',
},
moduleFileExtensions: [
// Place tsx and ts to beginning as suggestion from Jest team
Expand Down
30 changes: 28 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-preview",
"version": "0.2.3",
"version": "0.2.4-alpha.0",
"description": "Preview your HTML code while using Jest",
"keywords": [
"testing",
Expand Down Expand Up @@ -78,6 +78,8 @@
"@types/styled-components": "^5.1.24",
"@vitejs/plugin-react": "^1.3.0",
"cross-env": "^7.0.3",
"animate-sass": "^0.8.2",
"animate.css": "^4.1.1",
"jest": "^27.5.1",
"jest-watch-typeahead": "^1.0.0",
"nodemon": "^2.0.15",
Expand Down
Loading

0 comments on commit b55fe71

Please sign in to comment.