uiw icon indicating copy to clipboard operation
uiw copied to clipboard

直接使用create-react-app 在yarn add uiw 没有样式

Open quiet9929 opened this issue 8 years ago • 5 comments

我是直接用create-react-app 创建项目, 然后用yarn 添加uiw依赖, 在项目引用uiw组件, 怎么没有样式

quiet9929 avatar Jul 09 '18 01:07 quiet9929

这是因为 create-react-app 默认处理 css modules,导致样式名字被编译成了 hash name,

可通过下面方式,将在css-loader 中添加 getLocalIdent 配置方法,过滤 uiw 样式名字处理。

{
  test: /\.(css|less)$/,
  use: [
    require.resolve('style-loader'),
    {
      loader: require.resolve('css-loader'),
      options: {
        root: '.',
        modules: true,
        // minimize: true,
        localIdentName: '[local]',
        importLoaders: 1,
        getLocalIdent: (context, localIdentName, localName) => {
          // 过滤 uiw 组件库,因为 modules=true 参数,会将 className替换成Hash,导致uiw样式无法加载
          const hash = loaderUtils.getHashDigest(context.resourcePath + localIdentName, 'md5', 'base64', 5);
          const uiwpath = path.join(process.cwd(), 'node_modules', 'uiw');
          const codemirrorpath = path.join(process.cwd(), 'node_modules', 'codemirror');
          if (
            (new RegExp(`^${uiwpath}`)).test(context.resourcePath) ||
            (new RegExp(`^${codemirrorpath}`)).test(context.resourcePath)
          ) {
            return localName;
          }
          return localName + hash;
        },
      },
    },
    {
      loader: require.resolve('postcss-loader'),
      options: {
        // Necessary for external CSS imports to work
        // https://github.com/facebookincubator/create-react-app/issues/2677
        ident: 'postcss',
        plugins: () => [
          require('postcss-flexbugs-fixes'), // eslint-disable-line
          autoprefixer({
            browsers: [
              '>1%',
              'last 4 versions',
              'Firefox ESR',
              'not ie < 9', // React doesn't support IE8 anyway
            ],
            flexbox: 'no-2009',
          }),
        ],
      },
    },
    require.resolve('less-loader'),
  ],
}

jaywcjlove avatar Jul 09 '18 01:07 jaywcjlove

谢谢啦~

quiet9929 avatar Jul 09 '18 02:07 quiet9929

@quiet9929 可以通过 kkt

jaywcjlove avatar Jul 09 '18 03:07 jaywcjlove

用上面的方法改react-scripts/config/webpack.config.dev.js文件,但是只要yarn add 增一个新的模块,这个文件就会重置。另外用kkt怎么对缩进还有要求,多一个空格都报错。

jay4900 avatar Jul 16 '18 10:07 jay4900

kkt 里面内置了一个严格的ESLint 代码检测。 react-scripts 什么毛病不太清楚,我用 npm install 没有毛病呢。

@jay4900

jaywcjlove avatar Jul 16 '18 10:07 jaywcjlove