【发布时间】:2020-02-28 15:48:08
【问题描述】:
我正在开发一个使用纯 JS(无框架)和 SCSS 并将其与 Webpack 捆绑在一起的项目。
结构大致是这样的:
│ index.html
│ index.js
│ webpack.config.js
│
├───build
│ app.bundle.js
│ bundle.css
│ error.bundle.js
│ index.html
│
└───src
├───components
│ │ App.js
│ │
│ └───ErrorMessage
│ error.scss
│ ErrorMessage.js
└───style
footer.scss
header.scss
index.scss
reset.scss
我唯一的入口点是初始化应用程序的index.js。 index.scss 导入到App.js。
在我将 ErrorMessage 的样式移到单独的组件之前,一切都运行良好。
这是我的 webpack 配置:
const path = require('path');
const autoprefixer = require('autoprefixer');
module.exports = [{
entry: {
app: ['@babel/polyfill', './index.js']
},
output: {
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
path: path.resolve(__dirname, 'build')
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-proposal-class-properties'],
presets: ['@babel/preset-env']
}
}
]
},
{
test: /\.scss$/,
use: [
{
loader: 'file-loader',
options: {
name: 'bundle.css',
},
},
{ loader: 'extract-loader' },
{ loader: 'css-loader' },
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer()]
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['./node_modules']
}
}
}
]
}
]
},
}];
我要做的是将该组件的样式直接导入ErrorMessage.js,但仍将其编译为bundle.css 并保留单个入口点。但是,当我运行 webpack 构建时,它只会将 error.css 编译为 bundle.css 文件。
有没有办法像这样解析所有 .scss 文件?
【问题讨论】:
-
我的回答是否解决了您的问题,如果是,请将我的回答标记为已接受。谢谢
-
抱歉,我评论说它对我不起作用,因为我想尝试解决这个问题而不创建额外的入口点。
标签: javascript node.js webpack sass webpack-4