Does webpack support split file?

14 views Asked by At

I would like to develop a loader or a plugin that support split some function from origin file. For example, I have a file 'index.js'

// index.js
function a() {
  console.log('111');
}

function b() {
  console.log('222');
}

function c() {
  if (xx) {
    a();
  } else {
    b();
  }
}
c();

I will split then into two chunks, and lazy import then

// chunk a.js
export function a() {
  console.log('111');
}

// chunk b.js

export function b() {
  console.log('222');
}

// index.js

function c() {
  if (xx) {
    import('./a.js').then((mod) => mod.a());
  } else {
    import('./b.js').then((mod) => mod.b());
  }
}
c();

I use compilation emitAsset to generate a.js and b.js, and modify index.js in a custom loader But will failed when build. It throw error 'can not find module './a.js'', How can I do to support this feature? Is there a good way that no need to change the origin source code?

File should be split in new chunks. And can be imported successfully in origin file.

0

There are 0 answers