简体   繁体   English

如何修改延迟加载的 Webpack 模块路径?

[英]How can I modify a lazy loaded Webpack module path?

I have a Vue app which uses Webpack and dynamic imports:我有一个使用 Webpack 和动态导入的 Vue 应用程序:

// App.vue
<template>
  <div>
    <button @click="isBtnClicked = true">Load lazy component</button>
    <LazyComponent v-if="isBtnClicked" />
  </div>
</template>

<script>
export default {
  name: 'App',
  components: {
    LazyComponent: () => import('./components/LazyComponent'),
  },
  data: () => {
    return {
      isBtnClicked: false,
    }
  }
}
</script>
// components/LazyComponent.vue
<template>
    <p>Hello from lazy component</p>
</template>

When the button in the app is clicked, the Webpack runtime dynamically creates a <script> tag and appends it to the head of the document.单击应用程序中的按钮时,Webpack 运行时会动态创建<script>标签并将其附加到文档的head

Is there a way to modify the src attribute of this generated <script> tag?有没有办法修改这个生成的<script>标签的src属性? I would like to add a dynamic query parameter to it appends the element to the DOM.我想添加一个动态查询参数,将元素附加到 DOM。

The currently generated tag looks something like:当前生成的标签类似于:

<script charset="utf-8" src="/js/0.js"></script>

And I would like it to look like:我希望它看起来像:

<script charset="utf-8" src="/js/0.js?mytoken=12345"></script>

where mytoken and 12345 are generated at runtime.其中mytoken12345在运行时生成。

I am using webpack 4.44.0, vue 2.6.11, and vue-loader 15.9.3.我正在使用 webpack 4.44.0、vue 2.6.11 和 vue-loader 15.9.3。

According to this and this it doesn't look like you can do this in webpack 4. I'm not entirely sure, but it looks like this pull request may allow it to be done in webpack 5.根据这个这个看起来你不能在 webpack 4 中做到这一点。我不完全确定,但看起来这个拉取请求可能允许它在 webpack 5 中完成。

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

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