简体   繁体   English

添加样式到 TypeScript,反应 NPM package

[英]Adding styling to TypeScript, React NPM package

I have a TypeScript(TS), React project which I'm using to hold all my React components.我有一个 TypeScript(TS) React 项目,我用它来保存我所有的 React 组件。 I'm creating an NPM package out of this project which I'm then consuming in separate React projects.我正在从这个项目中创建一个 NPM package ,然后我在单独的 React 项目中使用它。 I'm running into a problem where I don't know how to export the styling along with the TS components.我遇到了一个问题,我不知道如何将样式与 TS 组件一起导出。

Using components from the NPM package result in errors: Module not found: Can't resolve '../css/footer.css'使用 NPM package 中的组件会导致错误: Module not found: Can't resolve '../css/footer.css'

When I create my directory for packaging(dist) all my components are there but none of the CSS.当我为打包(dist)创建目录时,我的所有组件都在那里,但没有 CSS。 I'm running tsc to create this directory.我正在运行tsc来创建这个目录。 All my components and CSS lie within the same src folder.我所有的组件和 CSS 都位于同一个 src 文件夹中。

Here is my tsconfig.json file:这是我的 tsconfig.json 文件:

{
  "compilerOptions": {
    "outDir": "dist",
    "target": "esnext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Do I need to move my styling out of css files and into ts files that make use of the <style jsx> tag or is there another 'better' way of achieving the desired result.我是否需要将我的样式从 css 文件中移到使用<style jsx>标签的 ts 文件中,或者是否有另一种“更好”的方式来实现所需的结果。 The desired result being an NPM package that contains all styling and components rolled into one.所需的结果是 NPM package,其中包含所有样式和组件。

tsc is the command for compiling TypeScript files to Javascript. tsc是将 TypeScript 文件编译为 Javascript 的命令。 CSS files are not Typescript files which is why they aren't included in the compilation. CSS 文件不是 Typescript 文件,这就是它们不包含在编译中的原因。

If you want to import CSS (or any other asset types) and output it alongside your other compiled files then you'll probably want to use a bundler like Webpack or Rollup .如果您想将 CSS(或任何其他资产类型)和 output 与其他编译文件一起导入,那么您可能需要使用像ZC64E0F7D4E01E48E693C9C5638B5E8F这样的捆绑程序 There are even tools like Microbundle which provide sensible bundler configuration so that you can focus on writing your library.甚至还有像Microbundle这样的工具,它们提供了合理的捆绑器配置,这样你就可以专注于编写你的库。

Alternatively, you could write your styles in TypeScript too, with a library like styled-jsx .或者,您也可以在 TypeScript 中编写您的 styles ,使用styled-jsx 之类的库。

Even if you're not interested in using one, it's worth taking a look at the various templates for TypeScript + React npm packages to see how they work.即使您对使用其中一个不感兴趣,也值得看看 TypeScript + React npm 包的各种模板,看看它们是如何工作的。

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

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