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

[Feature]: Support loaderContext._module.issuer #8102

Closed
luhc228 opened this issue Oct 14, 2024 · 6 comments
Closed

[Feature]: Support loaderContext._module.issuer #8102

luhc228 opened this issue Oct 14, 2024 · 6 comments
Assignees
Labels
feat New feature or request pending triage The issue/PR is currently untouched.

Comments

@luhc228
Copy link
Contributor

luhc228 commented Oct 14, 2024

What problem does this feature solve?

To align Webpack loader context.

What does the proposed API of configuration look like?

// loader.js
export default function loader(loaderContext, content) {
  console.log(loaderContext._module.issuer)
}
@luhc228 luhc228 added feat New feature or request pending triage The issue/PR is currently untouched. labels Oct 14, 2024
@hardfist
Copy link
Contributor

@luhc228 can you add more usage context about this api?

@luhc228
Copy link
Contributor Author

luhc228 commented Oct 14, 2024

@luhc228 can you add more usage context about this api?

Yes.

In my implement, I want to get the entry of current module because I need to collect some information about the entry. For example:

const entryInfo = new Map()

// loader.js
export default function loader(loaderContext, content) {
  // get info, for example
  const __HAS_COMPONENT__ = content.includes('components: {}')
 
  // get the entry module
  const entryModule = getEntryModule(loaderContext._module)
  
  // collect
  entryInfo.set(entryModule.name, { __HAS_COMPONENT__ })  
} 

function getEntryModule(_module) {
  if (!_module.issuer) {
    return _module
  }
  
 return getEntryModule(_module.issuer)
}

@hardfist
Copy link
Contributor

hardfist commented Oct 14, 2024

since a module can be imported by multi entries, I'm not sure you can get entryModule by issuer this way?

@hardfist
Copy link
Contributor

you may need module-graph api to find all the entrypoint contains the specfic module and you may need to do it in plugin other than loader

 {
      /**
       * 
       * @param {import('webpack').Compiler} compiler 
       */
      apply(compiler){

        compiler.hooks.done.tap('s', (stats) => {
          const entries = [];
          stats.compilation.entrypoints.forEach(entry => {
            entry.chunks.forEach(chunk => {
              chunk.getModules().forEach(m =>{
                 if(m.identifier() == your_module.identifier()){
                          entries.push(entry);
                 }
              })
            })
          })
        })
      }
    }

@luhc228
Copy link
Contributor Author

luhc228 commented Oct 15, 2024

you may need module-graph api to find all the entrypoint contains the specfic module and you may need to do it in plugin other than loader

Thanks for your advices! It seems that I don't need the loaderContext._module.issuer now.

@hardfist
Copy link
Contributor

close since it is not needed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

3 participants