3

create-react-app根据 Tailwind Docs 设置 Craco 后,我在我的应用程序中设置了响应式变体。这些在开发构建中完美运行,但在生产构建中没有加载图像。我在这里想念什么?

我已经确定问题很可能出现在我的生产配置中。当我将任何 URL 直接“硬编码”background-image到我的 App.css 文件中作为测​​试时,它们会在生产构建中正确显示。

索引.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer utilities {
  @variants responsive {
    .test-XS {
      background-image: url('https://res.cloudinary.com/.../test-XS.png');
    }
    .test-SM {
      background-image: url('https://res.cloudinary.com/.../test-SM.png');
    }
    .test-MD {
      background-image: url('https://res.cloudinary.com/.../test-MD.png');
    }
    .test-LG {
      background-image: url('https://res.cloudinary.com/.../test-LG.png');
    }
    .test-XL {
      background-image: url('https://res.cloudinary.com/.../test-XL.png');
    }
  }
  /* There are about 20 more sets of these for different images */
}

包.json:

{
  "homepage": "http://xxx.github.io/yyy",
  "name": "color-portfolio",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@craco/craco": "^6.1.1",
    "@tailwindcss/postcss7-compat": "^2.0.2",
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.6.3",
    "autoprefixer": "^9.8.6",
    "postcss": "^7.0.35",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.2",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.0.2",
    "web-vitals": "^1.1.0"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "gh-pages": "^3.1.0"
  }
}

craco.config.js:

module.exports = {
  style: {
    postcss: {
      plugins: [
        require('tailwindcss'),
        require('autoprefixer'),
      ],
    },
  },
}

tailwind.config.js:

module.exports = {
  purge: ['./src/**/*.{js,jsx,ts,tsx,css}', './public/index.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

4

1 回答 1

3

问题在于我在我className的 s 中使用的串联。

Tailwind 的文档在此处进行了介绍。

“不要使用字符串连接来创建类名”

我做错了什么的一个例子:

<div className={`card bg-contain ${props.image}-XS sm:${props.image}-SM md:${props.image}-MD lg:${props.image}-LG xl:${props.image}-XL`}>

我现在部署我的项目的解决方法:更改它,以便 Tailwind 不会通过指定要在配置中清除哪些层来“摇树”实用程序层(更多信息在 Tailwind Docs 中

tailwind.config.js:

module.exports = {
  purge: {
    layers: ['base', 'components'],
    content: ['./src/**/*.{js,ts,tsx}', './public/index.html']
  },
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

我确信这会使我的构建大小不必要地变大,但至少我的项目已部署,直到我想出一个更好的方法来合并这些动态类变体。

于 2021-03-12T16:49:56.097 回答