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

Allow webpackChunkName to be specified when using require.context #9184

Closed
DaniJG opened this issue May 23, 2019 · 14 comments
Closed

Allow webpackChunkName to be specified when using require.context #9184

DaniJG opened this issue May 23, 2019 · 14 comments

Comments

@DaniJG
Copy link

DaniJG commented May 23, 2019

Feature request

What is the expected behavior?

When using require.context it should be possible to specify the chunk name, same as when using magic comments with dynamic import statements.

Ie it should be possible to write (assuming a 5th argument is added to require.context for the chunk name):

require.context(
  '@/views', 
  true, 
  /\.async\.vue$/, 
  'lazy',
  '[request]')

which should generate the same chunks as when using

import(
  /* webpackChunkName: "[request]" */
  `@/views/${name}.async.vue`
);

What is motivation or use case for adding/changing the behavior?
I would like to automatically generate a chunk for every page in a Vue app that meets a certain convention. Ideally the chunk name would be based on the page name.

How should this be implemented in your opinion?
A 5th argument could be added to the RequireContextDependencyParserPlugin to extract the chunk name and pass it into the RequireContextDependency options.

Are you willing to work on this yourself?
yes

@PerspectivesLab
Copy link

that would be nice, as require.context doesnt generate any chunk

@JasKang
Copy link

JasKang commented Jul 12, 2019

const children = context.keys().map(key => {
    const path = key
      .split('/')
      .map(str => str.replace(/\.vue/g, '').replace(/\./g, ''))
      .filter(str => str !== '');
    return {
      component: () => context(/* webpackChunkName: "[request]" */ key),
      name: `${ctxKey}-${path.join('-')}`,
      path: `${ctxKey}/` + path.join('/')
    };
  });

@DaniJG
I hope so

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@alexander-akait
Copy link
Member

bump

@falcon03
Copy link

up. This issue is still relevant, especially considering that dynamic imports don't offer a feature similar to require.context

@Losses
Copy link

Losses commented Aug 27, 2020

bump this issue

@Rhilip
Copy link

Rhilip commented Jan 25, 2021

bump this issue

@dukhevych
Copy link

bump this issue

@sokra
Copy link
Member

sokra commented Apr 7, 2021

This has been implemented since years

@sokra sokra closed this as completed Apr 7, 2021
@sanmaozhao
Copy link

Where can I find the documentation or example?
Thanks!

@sokra sokra reopened this Apr 9, 2021
@sokra
Copy link
Member

sokra commented Apr 9, 2021

Oh I misread this as require.ensure instead of require.context.

No this is not implemented, but it's not that difficult to add.

Send a PR. see RequireContextDependencyParserPlugin.js, add chunkName property to options argument of new RequireContextDependency.

@Tofandel
Copy link

Tofandel commented May 4, 2021

Okay, I'm starting a PR

I believe we should have control over the chunkName, just request would generate a chunk per file, I'd want to generate grouped request chunks (eg: require context a folder and the subfolder would be the chunk name) but that's an entirely new issue (since it would require a deeper and different change)

@Tofandel
Copy link

Tofandel commented May 5, 2021

PR implements the same features as import with comments

So now it would be possible to do

require.context(
 /* webpackMode: "lazy" */
 /* webpackChunkName: "[request]" */
  '@/views', 
  true, 
  /\.async\.vue$/
)

@vankop
Copy link
Member

vankop commented Mar 23, 2022

please use import.meta.webpackContext for that #15463

@vankop vankop closed this as completed Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.