3

Is there a way to transform a file path of a resource in Webpack before Webpack attempts to fetch the resource?

My JS module needs to import standard libraries from node_modules like, for example, Bootstrap's Dropdown:

node_modules/bootstrap/js/dist/dropdown.js

I don't have possibility to duplicate those libraries, perform transformations on the duplicates and save as new files for my needs. I nearly have a Webpack loader that does transformations on the content, but I need a generic way to mark such files in my modules.

So I decided to reference them with non-existent extension in my modules like:

import 'bootstrap/js/dist/dropdown.js.newextension';

And then reference this extension with webpack.config:

{
  test: /\.newextension$/,
  use: [
    { loader: path.resolve('path-to-custom-loader.js') },
    'babel-loader'
  ]
}

but Webpack throws: Module not found: Error: Can't resolve 'bootstrap/js/dist/dropdown.js.newextension' that means it tries to fetch the file before I can cut .newextension off in my loader, fetch proper file and do my transformations.

So, how do I step into the process of Webpack before it attempts to fetch a resource to transform the path to file?

spliter
  • 12,321
  • 4
  • 33
  • 36
  • I created an issue about this: https://github.com/webpack/webpack/issues/9468 I believe this is a bug but I hope to be proven wrong by Webpack maintainers. – Eric MORAND Jul 24 '19 at 22:45

0 Answers0