Skip to content

module.unsafeCache is undocumented #6045

@EECOLOR

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:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions