Did the svelte.config.js file replace the rollup.config.js file in SvelteKit? - sveltekit

I'm trying to get PostCSS set up in my SvelteKit project to start playing with container queries. I installed 'svelte-preprocess' in my svelte.config.js file and made a postcss.config.cjs file (see below)
svelte.config.js
import adapter from '#sveltejs/adapter-auto';
import sveltePreprocess from 'svelte-preprocess';
/** #type {import('#sveltejs/kit').Config} */
const config = {
preprocess: sveltePreprocess({ postcss: true }),
kit: {
adapter: adapter(),
},
};
export default config;
postcss.config.cjs
const postcssPresetEnv = require('postcss-preset-env');
const config = {
plugins: [
postcssPresetEnv({
stage: 3,
features: {
'nesting-rules': true,
'custom-media-queries': true,
'media-query-ranges': true,
},
}),
],
};
module.exports = config;
I get the following error at my root level +page.svelte file:
*")" is expected
If you expect this syntax to work, here are some suggestions:
If you use less/SCSS with svelte-preprocess, did you add lang="scss"/lang="less" to your style tag? If you use SCSS, it may be necessary to add the path to your NODE runtime to the setting svelte.language-server.runtime, or use sass instead of node-sass.
Did you setup a svelte.config.js?
See https://github.com/sveltejs/language-tools/tree/master/docs#using-with-preprocessors for more info.*
Here's my +page.svelte file html and css (I'm trying to learn the container query syntax)
<div class="displayContainer">
<div class="queryWrapper">
<div class="imgWrapper">
//custom image component goes here
</div>
<div class="summaryContainer">
<h2>sample header text</h2>
<p>
sample paragraph text
</p>
</div>
</div>
</div>
<style lang="postcss">
.displayContainer {
container: homeDisplay / inline-size;
}
.queryWrapper {
#container homeDisplay (min-width: 450px) { /*line with error*/
display: grid;
grid-template-column: 350px 1fr;
gap: 20px;
}
}
.summaryContainer {
max-width: 600px;
}
</style>
package.json file
{
"name": "algomancy-site",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"lint": "prettier --plugin-search-dir . --check . && eslint .",
"format": "prettier --plugin-search-dir . --write ."
},
"devDependencies": {
"#sveltejs/adapter-auto": "^1.0.0",
"#sveltejs/kit": "^1.0.0",
"#types/postcss-preset-env": "^8.0.0",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-svelte3": "^4.0.0",
"postcss-load-config": "^4.0.1",
"postcss-preset-env": "^8.0.1",
"prettier": "^2.8.0",
"prettier-plugin-svelte": "^2.8.1",
"svelte": "^3.54.0",
"svelte-preprocess": "^5.0.1",
"vite": "^4.0.0"
},
"type": "module"
}
It's expecting a ")" after min-width.
I searched the documentation and recommended GitHub repos for preprocess configuration. Many of these resources/blogs reference a rollup.config.js file.
I don't see a rollup.config.js file in my skeleton SvelteKit project or in the SvelteKit documentation.
Is my issue from lacking a rollup file or is there an error in my file setup above?

After further digging I found this is a known issue with Svelte's compiler has with the '#container' syntax and is being worked on. Github issue 6969
A simple work-around is to place container queries (#container ...) in a separate .css file and import it where needed.

Related

"Property does not exist on type" using styled-components in .tsx file

I'm attempting to incorporate Styled-Components into a React project using TypeScript. Constructing the component immediately highlights div with the error:
Property 'div' does not exist on type 'typeof import("/Users/admin/node_modules/style-components/dist/index")'.ts(2339)
This is my code:
// App.tsx
import React from 'react'
import './App.css'
import styled from 'style-components'
const StyledDiv = styled.div`
display: flex;
`
const App: React.FC = () => {
return (
<div className="App">
<StyledDiv>
<h1>Testing</h1>
</StyledDiv>
</div>
)
}
Strangely, if I create that styled component in its own .ts file, export it, and import it into .App.tsx it will work just fine. Are styled components only meant to be imported and not used directly in a .tsx file?
My package.json file for informational purposes.
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^5.3.6"
},
"devDependencies": {
"#types/react": "^18.0.24",
"#types/react-dom": "^18.0.8",
"#types/styled-components": "^5.1.26",
"#vitejs/plugin-react": "^2.2.0",
"typescript": "^4.6.4",
"vite": "^3.2.3"
}
}
I did more testing and created a new component file and immediately I was met with a type error. I started over with a new project folder and used Yarn instead of NPM (which is what I was previously used to). The problem hasn't shown up again.

React/Tailwindcss modules name obfuscation breaks postcss nesting

So after extensive research I am here, I have a difficult to search problem which I'm hoping can be solved.
I've booted up a new project with Vite/ReactJS/Tailwindcss and I'm trying to use postcss to create nested CSS rules, they seem to be partially working but when using the ampersand (as in SCSS) things get a bit weird.
I have a bog standard Button React component with the following Button.module.css:
.Button {
#apply bg-primary;
&--disabled {
#apply opacity-50;
}
}
Styles are applied like so:
import styles from './Button.module.css';
const Button = ({ label, disabled }) => {
return (
<button type="button" className={[
styles.Button,
disabled ? 'Button--disabled' : ''
]}>
{label}
</button>
);
};
export default Button;
postcss.config.cjs:
module.exports = {
plugins: {
'postcss-import': {},
'tailwindcss/nesting': {},
tailwindcss: {},
autoprefixer: {},
},
}
However in the browser the button looks like, note the obfuscated classes:
<button type="button" class="Button _Button_1fcg7_1 Button--disabled">Button</button>
While the (successfully, I note) compiled CSS looks like:
._Button_1fcg7_1 {
--tw-bg-opacity: 1;
background-color: rgb(29 161 242 / var(--tw-bg-opacity))
}
._Button--disabled_1fcg7_1 {
opacity: 0.5
}
Because the class name in the HTML compiles to .Button--disabled and the CSS selector gets obfuscated to ._Button--disabled_1fcg7_1, the styles are never applied. What gives?
Some further info as requested, package.json:
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"dependencies": {
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"postcss-nesting": "^10.1.10",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.53.0"
},
"devDependencies": {
"#babel/core": "^7.18.6",
"#storybook/addon-actions": "^6.5.9",
"#storybook/addon-essentials": "^6.5.9",
"#storybook/addon-interactions": "^6.5.9",
"#storybook/addon-links": "^6.5.9",
"#storybook/builder-vite": "^0.2.0",
"#storybook/react": "^6.5.9",
"#storybook/testing-library": "^0.0.13",
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"#vitejs/plugin-react": "^2.0.0",
"autoprefixer": "^10.4.7",
"babel-loader": "^8.2.5",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.6",
"typescript": "^4.6.4",
"vite": "^3.0.0"
}
}
vite.config.ts:
import { defineConfig } from 'vite'
import react from '#vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})
tailwind.config.cjs:
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {
colors: {
primary: '#1da1f2',
},
}
},
plugins: [],
}
It's funny because different people with similar setups have the opposite problem.
Is there a way to obfuscate TailwindCSS classnames using PostCSS for React project in Vitejs?
I think that your problem comes from another place.
Can you share your package.json, tailwind.config.js, vite.config.js and rollup.config.js (if you have this last file) ?
Maybe you have some settings or packages configured related to obfuscation.
I don't know the vite ecosystem, but there are some packages that generate the behavior you want to avoid:
vite-plugin-tailwind-ofuscar
rollup-plugin-postcss

Properly installed tailwind dosn't work in React app

I initialized React application using Vite, and know I'm trying to add tailwind to this app.
I follewed the steps straight from documentation, and simply the classes are not applied to my components.
https://tailwindcss.com/docs/guides/create-react-app
Couple of days ago I was initializing React app the same way, followed the same instructions, and tailwind was working just fine. Now it does not and I really can't find any difference.
Here is my tailwind.config.js:
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Here is package.json:
{
"name": "megak-group-project-frontend",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"#vitejs/plugin-react": "^1.0.7",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.6",
"tailwindcss": "^3.0.22",
"vite": "^2.8.0"
}
}
Here is index.css:
#tailwind base;
#tailwind components;
#tailwind utilities;
Here is component that doesn't work:
function App() {
return (
<div className="App">
<h1 className="text-3xl font-bold underline">
Hello world!
</h1>
</div>
)
}
export default App
After some time I find out that npx tailwindcss init did not initialize postcss.config.js.
The reason it was not warking was this file missing with content:
const tailwindcss = require("tailwindcss");
module.exports = {
plugins: [tailwindcss("./tailwind.config.js"), require("autoprefixer")],
};

Nextjs Cypress Only found unit test code coverage

I am trying to add code coverage to cypress following this tutorial
I added #cypress/code-coverage and babel-plugin-istanbul to my package.json file below
{
"name": "quiz",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"cypress": "cypress open"
},
"dependencies": {
"next": "12.0.10",
"react": "17.0.2",
"react-dom": "17.0.2",
"sass": "^1.49.7"
},
"devDependencies": {
"#cypress/code-coverage": "^3.9.12",
"#types/node": "17.0.15",
"#types/react": "17.0.39",
"babel-plugin-istanbul": "^6.1.1",
"cypress": "^9.4.1",
"eslint": "8.8.0",
"eslint-config-next": "12.0.10",
"typescript": "4.5.5"
}
}
I configured my cypress/support/index.js
// Import commands.js using ES2015 syntax:
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
import '#cypress/code-coverage/support'
I configured my cypress/plugins/index.js
/**
* #type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
require('#cypress/code-coverage/task')(on, config)
on('file:preprocessor', require('#cypress/code-coverage/use-babelrc'))
return config
}
My .babelrc
{
"presets": [
"next/babel"
],
"plugins": [
"istanbul"
]
}
After I run my test I get the following message
Only found unit test code coverage. [#cypress/code-coverage]
After I run my quiz.spec.js test, I run
npx nyc report --reporter=text-summary
And get an empty coverage summary
What am I doing wrong?
My github
I think it was just a node_modules problem, as your repo worked after download and initialization. I think babel can fail silently if it's dependencies are out of order.
I removed package.lock (because I use yarn to manage things) and then yarn to init the dependecies.

cannot import image with create-react-app

I'm using create-react-app, and I am struggling to load images. I am following the instructions as specified here, but I keep seeing the following error:
Failed to compile.
Error in ./src/components/Home/imageIndex.ts
(1,24): error TS2307: Cannot find module './party.png'
Does anyone know why when I run yarn start my app continues to fail to compile?
I have a Home component and this is how I am importing the file:
import photo from './party.png';
The component is located in src/components/Home/index.tsx and the png file is located in src/components/Home/party.png.
here is my package.json:
{
"name": "eai-reactjs-typescript-redux-starter",
"description": "A ReactJS/TypeScript + Redux starter template with a detailed README describing how to use these technologies together and constructive code comments.",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.5",
"redux": "^3.7.0",
"redux-logger": "^3.0.6"
},
"devDependencies": {
"#types/enzyme": "^2.8.0",
"#types/jest": "^19.2.3",
"#types/node": "^7.0.18",
"#types/react": "^15.0.24",
"#types/react-dom": "^15.5.0",
"#types/react-redux": "^4.4.40",
"#types/redux-logger": "^3.0.0",
"enzyme": "^2.8.2",
"react-addons-test-utils": "^15.5.1",
"react-scripts-ts": "1.4.0",
"redux-devtools-extension": "^2.13.2"
},
"scripts": {
"start": "react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject"
}
}
Are you sure you've got the correct path? Also when you import an image via css (as is done in the example you mentioned) you need to give a path relative to the public folder and not the src folder. I have run into this problem several times.
An example using import:
import Image from './img.png'
...
// react example
...
render() {
return (
<img src={Image}/> // notice the curly
...
and not
<img src="Image"/>
I had the same problem and was able to solve with a require statement.
const photo = require('./party.png');
and then in your component use that like:
render() {
return (
<img src={photo}/>
also see this post Displaying a static image using React, Typescript and Webpack
As #archae0pteryx proposed it is much better to separate pictures from the code, so the project public folder should be used instead of src.
Here is how it works with css:
copy 'img.bmp' into /public
inside .css file/class: background-image: url('/img.bmp');
You can simply use something like this:
render() {
// Note: this is an escape hatch and should be used sparingly!
// Normally we recommend using `import` for getting asset URLs
// as described in “Adding Images and Fonts” above this section.
return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;
}
Quoted directly from CRA docs
By default, Create-React-App uses url-loader for files with size 10000 bytes
that works fine in the development environment, but in production reduce error.
For a production environment, you should change the value for the limit of bytes in order to enforce webpack use file-loader instead.
If you have to eject your app modify webpack.config.prod.js file and change the limit of url-loader to 10 and will work as well
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10,
name: 'static/media/[name].[hash:8].[ext]',
},

Resources