Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel-plugin-react-css-modulesの導入方法を知る #20

Closed
syunto07ka opened this issue Apr 1, 2020 · 10 comments · Fixed by #57
Closed

babel-plugin-react-css-modulesの導入方法を知る #20

syunto07ka opened this issue Apr 1, 2020 · 10 comments · Fixed by #57
Assignees
Labels
enhancement New feature or request

Comments

@syunto07ka
Copy link
Owner

スクリーンショット 2020-04-02 0 06 50

スクリーンショット 2020-04-02 0 07 10

@syunto07ka syunto07ka changed the title babel-plugin-react-css-modulesの導入[応用] babel-plugin-react-css-modulesの導入 Apr 2, 2020
@syunto07ka syunto07ka added the enhancement New feature or request label Apr 2, 2020
@syunto07ka syunto07ka self-assigned this Apr 12, 2020
@syunto07ka
Copy link
Owner Author

@syunto07ka
Copy link
Owner Author

syunto07ka commented Apr 12, 2020

エラーなった

スクリーンショット 2020-04-12 14 04 14

解決策っぽいやつ?
gajus/babel-plugin-react-css-modules#41

@syunto07ka
Copy link
Owner Author

エラー変わった。

Failed to compile.

./src/index.tsx
Error: /Users/syunto/development/jewelry_box/src/index.tsx: Cannot find module 'postcss-scss'
Require stack:
- /Users/syunto/development/jewelry_box/node_modules/babel-plugin-react-css-modules/dist/requireCssModule.js
- /Users/syunto/development/jewelry_box/node_modules/babel-plugin-react-css-modules/dist/index.js
- /Users/syunto/development/jewelry_box/node_modules/@babel/core/lib/config/files/plugins.js
- /Users/syunto/development/jewelry_box/node_modules/@babel/core/lib/config/files/index.js
- /Users/syunto/development/jewelry_box/node_modules/@babel/core/lib/index.js
- /Users/syunto/development/jewelry_box/node_modules/babel-loader/lib/index.js
- /Users/syunto/development/jewelry_box/node_modules/loader-runner/lib/loadLoader.js
- /Users/syunto/development/jewelry_box/node_modules/loader-runner/lib/LoaderRunner.js
- /Users/syunto/development/jewelry_box/node_modules/webpack/lib/NormalModule.js
- /Users/syunto/development/jewelry_box/node_modules/webpack/lib/NormalModuleFactory.js
- /Users/syunto/development/jewelry_box/node_modules/webpack/lib/Compiler.js
- /Users/syunto/development/jewelry_box/node_modules/webpack/lib/webpack.js
- /Users/syunto/development/jewelry_box/scripts/start.js
 ~/development/jewelry_box  add_babel_plugin_react_css_modules●●  0m                                                                                                                                 

以下2つの設定ファイルをいじった

webpack.config.js

{
        loader: require.resolve('css-loader'),
        options: {
          ...cssOptions,
          localIdentName: "[path]___[name]__[local]___[hash:base64:5]",
          modules: true
        },
      },
package.json

"babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
				"react-css-modules",
				{
					"filetypes": {
            ".scss": {
              "syntax": "postcss-scss"
            }
					}
				}
			]
    ]
  },

@syunto07ka
Copy link
Owner Author

syunto07ka commented Apr 12, 2020

エラー変わった

スクリーンショット 2020-04-12 20 22 15

解決策これかな
rails/webpacker#2197

css-loaderのwebpack設定の方法が変わってるらしい

https://github.com/webpack-contrib/css-loader/blob/master/CHANGELOG.md#300-2019-06-11

スクリーンショット 2020-04-12 20 22 09

"css-loader": "3.4.2",

3.0.0以上だからビンゴっぽい

@syunto07ka
Copy link
Owner Author

上のに加えて、typescriptのerror出てた。

styleName型にないよって言われてて

npm install --save @types/react-css-modules

これやったら解決(まあそりゃそうだなと)

import React from 'react';
import logo from './logo.svg';
import './App.scss';

function App() {
  return (
    <div styleName="app">
      <header styleName="appHeader">
        <img src={logo} styleName="appLogo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          styleName="appLink"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

で無事表示

スクリーンショット 2020-04-12 20 34 17

@syunto07ka syunto07ka changed the title babel-plugin-react-css-modulesの導入 babel-plugin-react-css-modulesの導入方法を知る Apr 12, 2020
@syunto07ka
Copy link
Owner Author

#48 にて導入方法はわかったのでこのissueはclose

@piro0919
Copy link

まだ解決できていないのにcloseしないでくださーい 🙅‍♂️

@piro0919 piro0919 reopened this Apr 12, 2020
@syunto07ka
Copy link
Owner Author

🙇

@piro0919
Copy link

rel #54

@piro0919
Copy link

@syunto07ka

#54 でかなり理解されたと思うので、ここまできたら導入してしまって問題ないと思います。
素の css modules と違いきちんと linter に沿えるようになるため、導入して損はないかと。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants