简体   繁体   English

Webpack - 在 index.html 中更改 javascript 文件的“src”路径

[英]Webpack - Change 'src' path of javascript files in index.html

I am facing an issue after building my strapi application (using webpack).构建我的strapi应用程序(使用webpack)后我遇到了一个问题。

After i executed yarn build , my index.html imports javascript files using the <script> tag.在我执行yarn build后,我的 index.html 使用<script>标签导入 javascript 文件。

The issue comes here, in my build folder, there are all of my files (images, js files etc...), and 0 subfolder.问题出现在这里,在我的构建文件夹中,有我的所有文件(图像、js 文件等......)和 0 子文件夹。

But inside my index.html, it imports from an invalid path:但在我的 index.html 中,它从无效路径导入:

<script src="admin/file.js"></script>

I want to change this src with Webpack to look like that:我想用 Webpack 改变这个 src 看起来像这样:

<script src="./file.js"></script>

Anybody got a solution?有人有解决方案吗?

Thank you.谢谢你。

Finally, i found the fix.最后,我找到了解决方法。

I am using Strapi for this web app.我正在为这个 web 应用程序使用 Strapi。

Strapi automatically fills webpack with publicPath "/admin/". Strapi 自动使用 publicPath "/admin/" 填充 webpack。

However, webpack doesn't understand and put every files (js, img, html) in the same directory, at the same level (no subfolders).但是,webpack 不理解并将每个文件(js、img、html)放在同一目录、同一级别(无子文件夹)中。

So inside src/admin/ , remove the word "example" from the file name "webpack.config.example.js"所以在src/admin/中,从文件名“webpack.config.example.js”中删除“example”这个词

Open this file and insert this code打开此文件并插入此代码

'use strict';
module.exports = (config, _webpack) => {
  config = {
    ...config,
    output: { ...config.output, publicPath: './'}
   }
   
  return config;
};

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

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