V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
santianban
V2EX  ›  前端开发

求个前段大神解惑一个 webpack 问题:

  •  
  •   santianban · 10 天前 · 541 次点击
    代码如下:
    function get_require(){
    return require;
    }


    get_require("xx.module.js");



    这段代码编译会提示无法找到插件,原因是 webpack 不能动态引入,get_require 里面会被编译为:


    function get_require(){
    return __webpack_require__(69);//这里直接返回空的方法
    }



    如何在不改变代码的情况下,使其能正确编译并执行?
    wildnode
        1
    wildnode  
       10 天前
    试试 require.context
    Wxh16144
        2
    Wxh16144  
       10 天前
    cjs 并不支持动态导入,另外 esm 支持动态导入,但是也需要一些配置。最好 google 一下吧。 关键词就那么几个 webpack dynamic import ...

    ref:

    1. https://nodejs.org/api/esm.html#import-expressions
    2. https://webpack.js.org/api/module-methods
    santianban
        3
    santianban  
    OP
       10 天前
    @Wxh16144 谢谢,我去研究下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5532 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 01:44 · PVG 09:44 · LAX 18:44 · JFK 21:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.