question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[tsdx-config] Css module build fail

See original GitHub issue

Hi guys, I just installed tsdx to try out the new functionality of the config file. The step I followed are:

  1. in the root of the project I created a file called tsdx.config.js:
const postcss = require('rollup-plugin-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');

module.exports = {
  rollup(config, options) {
    config.plugins.push(
      postcss({
        plugins: [
          autoprefixer(),
          cssnano({
            preset: 'default',
          }),
        ],
        inject: false,
        // only write out CSS for the first bundle (avoids pointless extra files):
        extract: !!options.writeMeta,
        modules: true,
      })
    );
    return config;
  },
};
  1. I installed the 3 dependencies that are required;
  2. in the src folder I create a file called: style.module.css
  3. in the index.tsx I imported it:
import * as React from 'react';
import * as style './style.module.css';
// Delete me
export const Thing = () => {
  return (
    <div className={style.test}>the snozzberries taste like snozzberries</div>
  );
};
  1. running yarn start
✖ Failed to compile
(typescript) Error: /Users/daniele/Desktop/testLibTsDx/src/index.tsx(2,19): syntax error TS1005: 'from' expected.
Error: /Users/daniele/Desktop/testLibTsDx/src/index.tsx(2,19): syntax error TS1005: 'from' expected.
    at error (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup/dist/rollup.js:9429:30)
    at throwPluginError (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup/dist/rollup.js:15701:12)
    at Object.error (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup/dist/rollup.js:15756:24)
    at Object.error (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup/dist/rollup.js:16148:38)
    at RollupContext.error (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:17187:30)
    at lodash_3 (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24954:23)
    at arrayEach (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:532:11)
    at forEach (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:9360:14)
    at printDiagnostics (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24927:5)
    at Object.transform (/Users/daniele/Desktop/testLibTsDx/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:26754:17)

✨  Done in 2.18s.

Did I miss something?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:29 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
daniele-zuricocommented, Aug 30, 2019

For all the people are struggling to add css modules to tsdx this is the solution:

  1. Create tsdx.config.js
const postcss = require('rollup-plugin-postcss');
module.exports = {
  rollup(config, options) {
    config.plugins.push(
      postcss({
        modules: true,
      })
    );
    return config;
  },
};
  1. install rollup-plugin-postcss
 yarn add rollup-plugin-postcss -dev
  1. in the src folder create a file called typings.d.ts:
declare module '*.css' {
  const content: { [className: string]: string };
  export default content;
}
  1. in the src folder create a file called style.module.css
.test {
  color: red;
}
  1. in the index.tsx import it:
import * as React from 'react';
import styles from './style.module.css';
// Delete me
export const Thing = () => {
  return (
    <div className={styles.test}>the snozzberries taste like snozzberries</div>
  );
};

@jaredpalmer feel free to close it or set as fixed or I can create a PR to your readme to add is an example. Up to you and thanks again for the great work!

2reactions
Ajamuarcommented, Oct 17, 2019

Hi, I am facing the same problem. I need a .css file in my project and I am trying to import it but I am continuously getting the same error. I tried the code given by @daniele-zurico and I am still facing the same problem.

image

Any help would be appreciated. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customization - TSDX
Build production ready TypeScript packages. The world's leading companies use TSDX to build and test TypeScript packages.
Read more >
CSS Module gets bundled but is not referenced using TSDX ...
This causes my styles to not show up at all. Here's my complete tsdx.config.js : const postcss = require('rollup-plugin-postcss ...
Read more >
How to use CSS Modules with TypeScript and webpack
You'll get an error: “Cannot find module './Button.css'”. There are several ways to fix that. The easy way. You can bypass TypeScript import...
Read more >
How to import CSS and style TSX React components
In this video I'll show you how to make your React application more reliable. Your components and class names will always be consistent....
Read more >
Rollup Config for React Component Library With TypeScript + ...
I am just sharing the current Rollup configurations that I use to build my own ... '@rollup/plugin-typescript'; import postcss from "rollup-plugin-postcss"; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found