V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
santianban

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

  •  
  •   santianban · Apr 14, 2025 · 1569 views
    This topic created in 378 days ago, the information mentioned may be changed or developed.
    代码如下:
    function get_require(){
    return require;
    }


    get_require("xx.module.js");



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


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



    如何在不改变代码的情况下,使其能正确编译并执行?
    wildnode
        1
    wildnode  
       Apr 14, 2025
    试试 require.context
    Wxh16144
        2
    Wxh16144  
       Apr 14, 2025
    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
       Apr 14, 2025
    @Wxh16144 谢谢,我去研究下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3593 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 11:46 · PVG 19:46 · LAX 04:46 · JFK 07:46
    ♥ Do have faith in what you're doing.