Compile modular Sass into css when building a component library

105 views Asked by At

I'm trying to build a React TS component library which is to be used in other projects. I currently use the following script to build the project.

"build": "rimraf dist && NODE_ENV=production npx babel --extensions .ts,.tsx ./src/lib -d ./dist --copy-files"

While this builds the project successfully, the Sass files are not compiled to css. Therefore, when used in a project which doesn't have Sass installed, it crashes.

Is there a way to build the scss files to css during the building process, or is there a way to install sass into the project as a dependency while the library is installed?

Folder Structure Folder Structure

Package JSON file.

{
  "name": "soft-inputs",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@fortawesome/free-solid-svg-icons": "^6.4.0",
    "@fortawesome/react-fontawesome": "^0.2.0",
    "formik": "^2.4.2",
    "framer-motion": "^10.13.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "yup": "^1.2.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.22.9",
    "@babel/preset-env": "^7.22.9",
    "@babel/preset-react": "^7.22.5",
    "@babel/preset-typescript": "^7.22.5",
    "rimraf": "^5.0.1",
    "typescript": "^4.9.5",
    "sass": "^1.63.6",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.38",
    "@types/react": "^18.2.15",
    "@types/react-dom": "^18.2.7"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "rimraf dist && NODE_ENV=production npx babel --extensions .ts,.tsx ./src/lib -d ./dist --copy-files",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Thank you in advance :)

I tried using Rollup, but failed due to a dependency issue.

1

There are 1 answers

0
hasathcharu On BEST ANSWER

As pointed out in the comments, I added sass as a peer dependency in package.json file, so that it would install when the library gets installed.

"peerDependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "sass": "^1.64.2",
 },

Thank you so much for your help!.