Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webpack] 第2042天 webpack能动态加载require引入的模块吗? #6008

Open
haizhilin2013 opened this issue Nov 16, 2024 · 1 comment
Labels

Comments

@haizhilin2013
Copy link
Collaborator

第2042天 webpack能动态加载require引入的模块吗?

3+1官网

我也要出题

@Elylicery
Copy link

Elylicery commented Nov 27, 2024

能,Webpack 支持动态加载通过 require 引入的模块。这种功能通常使用动态 import() 函数实现,这样在构建时 Webpack 就能够将这些模块分割为单独的块,从而实现懒加载。

可以使用requre.ensure()

require.ensure([], function(require) {
    const moduleA = require('./moduleA');
    // 使用moduleA
});

但这是旧版webpack的方案,现在更推荐使用import()加载

const loadModuleA = async () => {
    const moduleA = await import('./moduleA');
    // 使用moduleA
};

总结来说,Webpack 可以动态加载 require 引入的模块,但使用动态 import() 是更现代的做法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants