我在 Python 和 Flask 方面有超过 10 年的经验,主要关注后端和很少的 HTML + JavaScript,但是零 CSS。大约 6 周前,我开始使用 React 和 JavaScript,并使用不需要太多样式的 react 制作了简单的应用程序。但是,现在我需要使用索环进行样式设计,使用 React 做一个体面的项目。
我从 react-boilerplate 1开始。在阅读了 react-boilerplate scss 文档和 //github.com/grommet/grommet-standalone 自述文件后,我的理解是,我需要更改 webpack 配置以使 scss 加载程序正常工作。
我的环境是 win8.1 x64,节点 6.4.0
这是我改变的。
都安装了
sass-loader
&node-sass
在
internals\webpack\webpack.dev.babel.js
改变
cssLoaders: 'style-loader!css-loader?localIdentName=[local]__[path][name]__[hash:base64:5]&modules&importLoaders=1&sourceMap!postcss-loader',
至
cssLoaders: 'style-loader!css-loader?localIdentName=[local]__[path][name]__[hash:base64:5]&modules&importLoaders=1&sourceMap!postcss-loader!sass-loader',
注意额外的 !sass-loader 根据 //github.com/mxstbr/react-boilerplate/blob/master/docs/css/sass.md
- 将以下内容添加到
internals\webpack\webpack.base.babel.js
{ test: /\.scss$/, loader: 'style!css!sass?outputStyle=compressed' }
@ 第 40 行就在 css 块下方,并且
sassLoader: {
includePaths: [
'./node_modules',
// this is required only for NPM < 3.
// Dependencies are flat in NPM 3+ so pointing to
// the internal grommet/node_modules folder is not needed
'./node_modules/grommet/node_modules'
]
}
@line 62[ 第 58 行,如果你在 master 分支中查看 @github 源代码 //github.com/mxstbr/react-boilerplate/blob/master/internals/webpack/webpack.base.babel.js,+4 是因为为 scss 块添加了 4 行]
'.scss'
在resolve.extenstions
数组中添加的同一个文件中。现在,在这些更改之后,我运行 npm start 并将以下行添加到
app\containers\App\index.js
Now 在这些更改之后,我运行 npm start 并将以下行添加到 app\containers\App\index.jsimport 'grommet/scss/vanilla/index';
根据此处的文档 //github.com/grommet/grommet-standalone
在 cmd.exe 控制台中,我收到以下错误
ERROR in ./app/containers/App/index.js
Module not found: Error: Can't resolve 'grommet/scss/vanilla/index' in 'C:\Users\coder\frontend\app\containers\App'
@ ./app/containers/App/index.js 57:0-36
@ ./app/app.js
@ multi main
Child html-webpack-plugin for "index.html":
不用说已经安装了索环,并且文件存在于带有 scss 扩展的所需位置。我当然可以使用扩展名导入,因为import 'grommet/scss/vanilla/index.scss
我收到以下错误
ERROR in ./~/css-loader!./~/style-loader!./~/css-loader!./~/sass-loader?outputStyle=compressed!./~/grommet/scss/vanilla/index.scss Module build failed: Unknown word (5:1)
3 | // load the styles 4 | var content = require("!!./../../../css-loader/index.js!./../../../sass-loader/index.js?outputStyle=compressed!./index.scss");
> 5 | if(typeof content === 'string') content = [[module.id, content, '']];
| ^ 6 | // add the styles to the DOM 7 | var update = require("!./../../../style-loader/addStyles.js")(content, {}); 8 | if(content.locals) module.exports = content.locals;
@ ./~/grommet/scss/vanilla/index.scss 4:14-189 13:2-17:4 14:20-195
我在这里遗漏了一些非常明显的东西吗?看起来好像 scss 加载程序不工作。如果您已成功使用带有 react-boilerplate 的扣眼,请发布您所做的更改。
PS:Stack Overflow 告诉我,我不能发布两个以上的链接,所以我修改了链接以省略 https: 从 url,请手动添加。