Ber*_*egt 4 webpack webpack-dev-server
我正在尝试为我要建立的网站设置Webpack配置。我想将SASS编译为CSS并将其放入dist文件夹。当我运行npm run build它时,它工作正常,但是当我运行npm run watch触发Webpack-dev-server时,它不会将index.js编译为bundle.js,并且不会出现在dist文件夹中。我的webpack.config.js出问题了吗?
index.html
<html>
<head>
<title>Getting Started</title>
<link rel="stylesheet" href="dist/css/main.min.css">
</head>
<body>
test
<script src="dist/scripts/bundle.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractSass = new ExtractTextPlugin({
filename: "../css/main.min.css",
disable: process.env.NODE_ENV === "development"
});
module.exports = {
entry: './src/scripts/index.js',
output: {
path: path.resolve(__dirname, 'dist/scripts'),
filename: 'bundle.js',
publicPath: 'dist/scripts'
},
module: {
rules: [
{
test: /\.scss$/,
use: extractSass.extract({
use: [{
loader: "css-loader"
}, {
loader: "sass-loader"
}],
fallback: "style-loader"
})
}
]
},
plugins: [
extractSass
]
};
Run Code Online (Sandbox Code Playgroud)
package.json
{
"name": "project1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --optimize-minimize",
"watch": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^3.0.2",
"node-sass": "^4.7.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.20.2",
"webpack": "^3.11.0",
"webpack-dev-server": "^2.11.1"
},
"dependencies": {}
}
Run Code Online (Sandbox Code Playgroud)
开发服务器有选项 writeToDisk
module.exports = { //... devServer: { writeToDisk: true } };
检查这个:https : //webpack.js.org/configuration/dev-server/#devserverwritetodisk-
您可以通过webpack-dev-server将生成的URL查找到该URL中。
http:// localhost:8080 / webpack-dev-server(根据需要更改主机和端口)
webpack-dev-server不会将文件写入磁盘,但是您可以在其中看到它们。
| 归档时间: |
|
| 查看次数: |
4423 次 |
| 最近记录: |