Skip to content

module.unsafeCache is undocumented #6045

Closed

Description

@EECOLOR

Do you want to request a feature or report a bug?
Report a bug, or maybe I'm using it wrong, at the least it's unexpected behavior and the documentation is missing.

What is the current behavior?
A module was cached when it should not have been cached.

If the current behavior is a bug, please provide the steps to reproduce.
I have a plugin that hooks into create-module of NormalModuleFactory, the result is a new raw module with the dependencies from a module of a parent compilation.

The code:

...
webCompiler.plugin('compilation', (webCompilation, { normalModuleFactory }) => {
  normalModuleFactory.plugin('create-module', data => {
    if (!data.resourceResolveData.path.endsWith('.js')) {
      const parentCompilationModule = compilation.findModule(data.request)
      if (parentCompilationModule) {
        // mutation in webpack internals is a minefield, tread carefully
        const dependencies = parentCompilationModule.dependencies.slice()
        const parentSource = new ReplaceSource(parentCompilationModule.originalSource())

        const { dependencyTemplates, outputOptions, moduleTemplate: { requestShortener } } = webCompilation

        // from NormalModule.sourceDependency
        dependencies.forEach(dependency => {
          const template = dependencyTemplates.get(dependency.constructor)
          if(!template) throw new Error('No template for dependency: ' + dependency.constructor.name)
          template.apply(dependency, parentSource, outputOptions, requestShortener, dependencyTemplates)
        })

        const result = new RawModule(parentSource.source())
        result.dependencies = dependencies
        return result
      }
    }
  })
})

The module.unsafeCache option is by default true and setting it to false caused this code to be executed on every change.

What is the expected behavior?
I understand this use case is non-typical, but the cache is implemented in a very ad-hoc fashion by mutating the dependency:

// retrieval
const cacheEntry = dependencies[0].__NormalModuleFactoryCache
...
// setting
dependencies.forEach(d => d.__NormalModuleFactoryCache = module)

I would expect the module.unsafeCache to be false by default. Or, if it is true to be more sophisticated and not implemented by mutating all dependencies and grabbing the first dependency.

But, more importantly, have the documentation explain what module.unsafeCache does. I could for example provide a function that checks the module and test if it's one of these special ones. For now I have set it to false but since the documentation is missing I am not sure of the performance penalty.

If this is a feature request, what is motivation or use case for changing the behavior?
Not sure, I think it's mostly a bug report about missing documentation.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
None are relevant for this issue. The context in which we discovered the problems might be:

Activity

  1. sokra commented on Nov 30, 2017

    @sokra
    Member

    Or, if it is true to be more sophisticated and not implemented by mutating all dependencies and grabbing the first dependency.

    A WeakMap would be better here. Send a PR if you like to.

  2. added
    help wanted
    on Feb 21, 2018
  3. FadySamirSadek commented on Mar 24, 2018

    @FadySamirSadek

    @sokra I would love to take this one , but I need more details about the suggested solution as I do not have a clear understanding how it should be implemented.

  4. webpack-bot commented on Sep 22, 2018

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

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

  5. added
    status: stale
    on Sep 22, 2018
  6. EECOLOR commented on Sep 22, 2018

    @EECOLOR
    ContributorAuthor

    @webpack-bot This issue is still relevant

  7. removed
    status: stale
    on Sep 22, 2018
  8. sokra commented on Sep 24, 2018

    @sokra
    Member

    The mutating thing was fixed.

  9. sokra commented on Sep 24, 2018

    @sokra
    Member

    @webpack-bot move to webpack/webpack.js.org

  10. webpack-bot commented on Sep 24, 2018

    @webpack-bot
    Contributor

    I've moved it to webpack/webpack.js.org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions