简体   繁体   English

webpack开发中static文件放在哪里

[英]Where to put static files in webpack development

Where do you put static files in a webpack project.您将 static 文件放在 webpack 项目中的什么位置。 I have my webpack setup to where the output is going to build folder.我将 webpack 设置到 output 将要构建文件夹的位置。

  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "../build"),
  },

When I'm in development mode I want to be able to fetch some json files from the build folder but when I put my json files in there its not able to fetch them.当我处于开发模式时,我希望能够从构建文件夹中获取一些 json 文件,但是当我将 json 文件放在那里时,它无法获取它们。

In my app I try to fetch the json file like this.在我的应用程序中,我尝试像这样获取 json 文件。

  useEffect(() => {
    fetch(`json/test.json`)
      .then((res) => res.json())
  }, []); 

In the output section of your webpack config file add a publicPath field pointing to your build folder.在 webpack 配置文件的 output 部分中,添加指向构建文件夹的 publicPath 字段。

output: {
  publicPath: '../build/'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM