简体   繁体   English

使用 Parcel 从 node_modules 导入 CSS

[英]Importing CSS from node_modules with Parcel

I am currently migrating as React project built with Webpack into Parcel as an experiment to see what the hype is all about.我目前正在将使用 Webpack 构建的 React 项目迁移到 Parcel 作为实验,以了解炒作的全部内容。 The project uses Reactstrap, which has Bootstrap as a dependency.该项目使用 Reactstrap,它具有 Bootstrap 作为依赖项。

I have a main.tsx file that is the "shell" for most of the other pages in the app, and it imports Bootstrap CSS from node_modules like so:我有一个main.tsx文件,它是应用程序中大多数其他页面的“外壳”,它从 node_modules 导入 Bootstrap CSS,如下所示:

import * as React from "react";
import 'bootstrap/dist/css/bootstrap.min.css'; // <-- There it is!
...

This works fine with Webpack, and when I build a file that imports main.tsx (and presumably the Boostrap CSS that comes with it) with Parcel, there are no errors.这适用于 Webpack,并且当我构建一个导入main.tsx (以及它附带的 Boostrap CSS)的文件时,没有错误。 However, there is no Bootstrap CSS on the page, so it looks wack.但是,页面上没有 Bootstrap CSS,所以看起来很古怪。

Although Parcel bills itself as "zero configuration," is there some configuration I need to add for Parcel to process and import that CSS?尽管 Parcel 将自己标榜为“零配置”,但是否需要为 Parcel 添加一些配置来处理和导入该 CSS?

First you need to have the following structure in your React/Parcel project :首先,您的 React/Parcel 项目中需要具有以下结构:

root
  |- node_modules
  |- src
  |   |- styles.scss
  |   |_ main.tsx
  |
  |- .sassrc
  |_ package.json

Define includePaths in the .sassrc :.sassrc 中定义includePaths

// file : .sassrc
{
  "includePaths": [
    "node_modules/",
    "src/"
  ]
}

Import bootstrap in styles.scssstyles.scss中导入bootstrap

// file : styles.scss
@import '../node_modules/bootstrap/scss/bootstrap';

Then import your styles.scss in main.tsx然后在main.tsx中导入你的styles.scss

// file : main.tsx
import * as React from 'react';
import '~/styles.scss';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM