Skip to content

Commit

Permalink
Merge pull request #253 from nvh95/less
Browse files Browse the repository at this point in the history
Support LESS
  • Loading branch information
nvh95 authored Aug 26, 2022
2 parents fca22c9 + 5943b72 commit ac207f8
Show file tree
Hide file tree
Showing 47 changed files with 691 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Features

- Inline `@import` statement
- Support LESS

# 0.3.0

Expand Down
2 changes: 1 addition & 1 deletion cli/configCra.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ try {
jestConfig.transform = {
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$':
'react-scripts/config/jest/babelTransform.js',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
};
Expand Down
3 changes: 3 additions & 0 deletions demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import './assets/css/App.css';
import './assets/_scss/style.scss';
import { cssModule } from './style.module.css';
import { scssModule } from './style.module.scss';
import './styles/less/style.less';

function App() {
const [count, setCount] = useState(0);
Expand All @@ -25,6 +26,8 @@ function App() {
</LogoWrapper>
<img src={logo2} className="logo2" alt="logo2" />
<p>Hello Vite + React!</p>
<p className="less-p">Less: Blue + Dynamic size (break point 400px)</p>
<p className="less-child-p">Less: Yellow</p>
<p className={scssModule}>Styled by SCSS Modules</p>
<StyledText>This text is styled by styled-components</StyledText>
<p className="global-css">
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 @@ -7,7 +7,7 @@ describe('transform', () => {
render(<App />);
// TODO: To refactor this test. It frequently need to be updated.
expect(document.body.outerHTML).toMatchInlineSnapshot(
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><div class=\\"sc-gsnTZi kykGJG\\"><img src=\\"/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\" height=\\"100\\"><svg xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"410\\" height=\\"100\\" fill=\\"none\\" viewBox=\\"0 0 410 404\\"><path fill=\\"url(#paint0_linear)\\" d=\\"M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z\\"></path><path fill=\\"url(#paint1_linear)\\" d=\\"M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z\\"></path><defs><linearGradient id=\\"paint0_linear\\" x1=\\"6\\" x2=\\"235\\" y1=\\"33\\" y2=\\"344\\" gradientUnits=\\"userSpaceOnUse\\"><stop stop-color=\\"#41D1FF\\"></stop><stop offset=\\"1\\" stop-color=\\"#BD34FE\\"></stop></linearGradient><linearGradient id=\\"paint1_linear\\" x1=\\"194.651\\" x2=\\"236.076\\" y1=\\"8.818\\" y2=\\"292.989\\" gradientUnits=\\"userSpaceOnUse\\"><stop stop-color=\\"#FFEA83\\"></stop><stop offset=\\"0.083\\" stop-color=\\"#FFDD35\\"></stop><stop offset=\\"1\\" stop-color=\\"#FFA800\\"></stop></linearGradient></defs></svg></div><img src=\\"/demo/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Hello Vite + React!</p><p class=\\"_scssModule_ujx1w_1\\">Styled by SCSS Modules</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_1a0qn_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><p class=\\"bg-yellow-400 font-bold m-5 text-red-500\\">This text is styled by Tailwind CSS</p><button class=\\"css-1b07vmx-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>"`,
`"<body><div><div class=\\"App\\"><header class=\\"App-header\\"><div class=\\"sc-gsnTZi kykGJG\\"><img src=\\"/logo.svg\\" class=\\"App-logo\\" alt=\\"logo\\" height=\\"100\\"><svg xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"410\\" height=\\"100\\" fill=\\"none\\" viewBox=\\"0 0 410 404\\"><path fill=\\"url(#paint0_linear)\\" d=\\"M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z\\"></path><path fill=\\"url(#paint1_linear)\\" d=\\"M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z\\"></path><defs><linearGradient id=\\"paint0_linear\\" x1=\\"6\\" x2=\\"235\\" y1=\\"33\\" y2=\\"344\\" gradientUnits=\\"userSpaceOnUse\\"><stop stop-color=\\"#41D1FF\\"></stop><stop offset=\\"1\\" stop-color=\\"#BD34FE\\"></stop></linearGradient><linearGradient id=\\"paint1_linear\\" x1=\\"194.651\\" x2=\\"236.076\\" y1=\\"8.818\\" y2=\\"292.989\\" gradientUnits=\\"userSpaceOnUse\\"><stop stop-color=\\"#FFEA83\\"></stop><stop offset=\\"0.083\\" stop-color=\\"#FFDD35\\"></stop><stop offset=\\"1\\" stop-color=\\"#FFA800\\"></stop></linearGradient></defs></svg></div><img src=\\"/demo/assets/images/logo.svg\\" class=\\"logo2\\" alt=\\"logo2\\"><p>Hello Vite + React!</p><p class=\\"less-p\\">Less: Blue + Dynamic size (break point 400px)</p><p class=\\"less-child-p\\">Less: Yellow</p><p class=\\"_scssModule_ujx1w_1\\">Styled by SCSS Modules</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_1a0qn_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><p class=\\"bg-yellow-400 font-bold m-5 text-red-500\\">This text is styled by Tailwind CSS</p><button class=\\"css-1b07vmx-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>"`,
);
});
});
3 changes: 3 additions & 0 deletions demo/styles/less/child.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.less-child-p {
color: yellow;
}
8 changes: 8 additions & 0 deletions demo/styles/less/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@min400: ~'(min-width: 400px)';
.less-p {
color: blue;
@media @min400 {
font-size: 2rem;
}
}
@import './child';
2 changes: 1 addition & 1 deletion examples/angular/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports =
],
transform: {
// '^.+\\.(ts|js|html)$': 'jest-preset-angular',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
2 changes: 1 addition & 1 deletion examples/create-react-app/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
'^.+\\.(js|jsx|mjs|cjs|ts|tsx)$':
'react-scripts/config/jest/babelTransform.js',
// Update the regex to support css and sass
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
2 changes: 1 addition & 1 deletion examples/jest-24/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
modulePaths: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': '@swc/jest',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
2 changes: 1 addition & 1 deletion examples/jest-28/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
modulePaths: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': '@swc/jest',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
2 changes: 1 addition & 1 deletion examples/jquery/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
modulePaths: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': '@swc/jest',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^.+\\.(css|scss|sass|less)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
Expand Down
24 changes: 24 additions & 0 deletions examples/less/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
16 changes: 16 additions & 0 deletions examples/less/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"sourceMaps": true,

"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
},

"transform": {
"react": {
"runtime": "automatic"
}
}
}
}
13 changes: 13 additions & 0 deletions examples/less/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
53 changes: 53 additions & 0 deletions examples/less/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** @type {import('@jest/types').Config.InitialOptions} */

module.exports = {
roots: ['<rootDir>/src'],
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/*.d.ts',
'!src/mocks/**',
],
coveragePathIgnorePatterns: [],
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
testEnvironment: 'jsdom',
modulePaths: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|js|tsx|jsx)$': '@swc/jest',
'^.+\\.(css|scss|sass)$': 'jest-preview/transforms/css',
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)':
'jest-preview/transforms/file',
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
],
modulePaths: ['<rootDir>/src'],
moduleNameMapper: {
'^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
// https://jestjs.io/docs/configuration#modulefileextensions-arraystring
'tsx',
'ts',
'web.js',
'js',
'web.ts',
'web.tsx',
'json',
'web.jsx',
'jsx',
'node',
],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
resetMocks: true,
// testEnvironmentOptions: {
// html: '<html><div id="root"></div></html>',
// },
};
38 changes: 38 additions & 0 deletions examples/less/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "less",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"jest-preview": "jest-preview",
"test": "jest --watch",
"test:ci": "jest",
"test:nc": "npm run test -- --no-cache",
"test:debug": "npm-run-all -p test jest-preview"
},
"dependencies": {
"less": "^4.1.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@swc/core": "^1.2.242",
"@swc/jest": "^0.2.22",
"@testing-library/dom": "^8.17.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.4.3",
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.6",
"@vitejs/plugin-react": "^2.0.1",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"jest-preview": "file:../..",
"jest-watch-typeahead": "^2.0.0",
"typescript": "^4.6.4",
"vite": "^3.0.7"
}
}
1 change: 1 addition & 0 deletions examples/less/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions examples/less/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
40 changes: 40 additions & 0 deletions examples/less/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useState } from 'react';
import reactLogo from './assets/react.svg';
import './App.css';
import './style.less';

function App() {
const [count, setCount] = useState(0);

return (
<div className="App">
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://reactjs.org" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<p className="less-p">Less: Blue + Dynamic size (break point 400px)</p>
<div className="card">
<button
data-testid="increase"
onClick={() => setCount((count) => count + 1)}
>
count is&nbsp;
<span data-testid="count">{count}</span>
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</div>
);
}

export default App;
28 changes: 28 additions & 0 deletions examples/less/src/__tests__/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { render, screen } from '@testing-library/react';

import userEvent from '@testing-library/user-event';
import App from '../App';
import preview from 'jest-preview';

describe('App', () => {
it('should work as expected', async () => {
const root = document.createElement('div');
root.id = 'root';
render(<App />, {
container: document.body.appendChild(root),
});
console.log(document.body.outerHTML);
await userEvent.click(screen.getByTestId('increase'));
await userEvent.click(screen.getByTestId('increase'));
await userEvent.click(screen.getByTestId('increase'));
await userEvent.click(screen.getByTestId('increase'));
await userEvent.click(screen.getByTestId('increase'));
await userEvent.click(screen.getByTestId('increase'));

// Open http://localhost:3336 to see preview
// Require to run `jest-preview` server before
preview.debug();

expect(screen.getByTestId('count')).toContainHTML('6');
});
});
Loading

0 comments on commit ac207f8

Please sign in to comment.