【问题标题】:Webpack watch true only works on package.json, not on all filesWebpack watch true 仅适用于 package.json,不适用于所有文件
【发布时间】:2023-02-10 15:54:12
【问题描述】:

我让我的 webpack 使用 watch: true 观看我所有的文件webpack.config.js.

我通过这段代码使用 npm run build 运行 webpack包.json:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },

现在当我使用npm run build时,它只会在我每次保存时编译包.json.我如何更改它以便每次在我的所有文件夹中保存文件时编译它?

完整代码

包.json

{
  "name": "testproj",
  "version": "1.0.0",
  "description": "",
  "main": "code.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "Figma",
  "license": "MIT",
  "devDependencies": {
    "@figma/plugin-typings": "*",
    "@types/node": "^16.7.1",
    "css-loader": "^6.2.0",
    "html-webpack-inline-source-plugin": "0.0.10",
    "html-webpack-plugin": "^5.3.2",
    "style-loader": "^3.2.1",
    "ts-loader": "^9.2.5",
    "typescript": "^4.3.5",
    "url-loader": "^4.1.1",
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "@types/react": "^17.0.19",
    "@types/react-dom": "^17.0.9",
    "figma-plugin-ds": "^1.0.1",
    "react": "^17.0.2",
    "react-dev-utils": "^11.0.4",
    "react-dom": "^17.0.2"
  }
}

webpack.config.js

const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')

const path = require('path')
const webpack = require('webpack')

module.exports = (env, argv) => ({
  watch: true,
  watchOptions: {
    ignored: /node_modules/,
  },

  mode: argv.mode === 'production' ? 'production' : 'development',

  devtool: argv.mode === 'production' ? false : 'inline-source-map',

  entry: {
    ui: './src/ui.tsx',
    code: './src/code.ts',
  },

  module: {
    rules: [
      { 
        test: /\.tsx?$/, 
        use: 'ts-loader', 
        exclude: /node_modules/
      },

      { 
        test: /\.css$/, 
        use: ["style-loader", "css-loader"],
      },
      { 
        test: /\.svg/,
        type: 'asset/inline'
      }
    ]
  },

  resolve: { extensions: ['.tsx', '.ts', '.jsx', '.js'] },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'), 
  },

  plugins: [
    new webpack.DefinePlugin({
      'global': {}
    }),
    new HtmlWebpackPlugin({
      inject: "body",
      template: './src/ui.html',
      filename: 'ui.html',
      chunks: ['ui']
    }),
    new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/ui/]),
  ],
})

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

    使用 package.json 中的命令

    webpack --watch --config webpack.config.js
    

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多