首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Webpack在文件中写入输出

如何使用Webpack在文件中写入输出
EN

Stack Overflow用户
提问于 2022-05-26 03:27:27
回答 1查看 111关注 0票数 0

在使用开发服务器时,我试图在文件中写入输出,但是我遇到了一个错误-

代码语言:javascript
运行
复制
been initialized using a configuration object that does 
not match the API schema.
 - configuration has an unknown property 'devMiddleware'. These properties are valid:
   object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry?, experiments?, externals?, externalsPresets?, externalsType?, ignoreWarnings?, infrastructureLogging?, loader?, mode?, module?, name?, node?, optimization?, output?, parallelism?, performance?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, snapshot?, stats?, target?, watch?, watchOptions? }
   -> Options object as provided by the user.
   For typos: please correct them.
   For loader options: webpack >= v2.0.0 no longer allows custom properties in configuration.
     Loaders should be updated to allow passing options 
via loader options in module.rules.
     Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:
     plugins: [
       new webpack.LoaderOptionsPlugin({
         // test: /\.xxx$/, // may apply this only for some modules
         options: {
           devMiddleware: …
         }
       }) *

这是我的webpack.config.js

代码语言:javascript
运行
复制
const path = require('path');
module.exports={
 mode:"development",
 entry:'./src/app.js',
 output: {
   path: path.resolve(__dirname, 'dist'),
   filename: 'output.js'
 },

   module: {
     rules: [
       { test: /\.css$/,
        use:['style-loader','css-loader'] }
     ],
   },
 devServer: {
   static: {
     directory: path.join(__dirname, './dist'),
   },
   compress: true,
   port: 3200
 },
 devMiddleware: {
  writeToDisk: true,
}
} 

这是package.json

代码语言:javascript
运行
复制
{
  "name": "new-folder",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server --mode development --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "web-cli": "^1.0.0-prealpha",
    "webpack": "^5.72.1"
  },
  "devDependencies": {
    "css-loader": "^6.7.1",
    "style-loader": "^3.3.1",
    "webpack-cli": "^4.9.2",
    "webpack-dev-middleware": "^5.3.3",
    "webpack-dev-server": "^4.9.0"
  }
}

有人帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2022-05-26 05:32:31

将中间件配置移动到devServer配置对象中,如下所示:

代码语言:javascript
运行
复制
const path = require('path');

module.exports = {
  mode:'development',
  entry:'./src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'output.js'
  },

  module: {
    rules: [
      {
        test: /\.css$/,
        use:['style-loader','css-loader']
      }
    ],
  },

  devServer: {
    static: {
      directory: path.join(__dirname, './dist'),
    },
    compress: true,
    port: 3200,
    devMiddleware: {
      writeToDisk: true
    }
  }
} 

此外,理论上您不应该需要使用writeToDisk。您应该有一个单独的build命令,该命令生成将写入磁盘的生产生成。例如,您将有以下npm脚本对象:

代码语言:javascript
运行
复制
// package.json
{
  "scripts": {
    "start": "webpack serve",
    "build": "webpack"
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72386358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档