question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Options to keep `require.resolve` as external

See original GitHub issue

Continuation of https://github.com/webpack/webpack/issues/8636 (closed because of inactivity) Related: https://github.com/webpack/webpack/issues/9393 (closed because of inactivity)

versions

webpack 5.54.0
webpack-cli 4.8.0

Feature request

// webpack.config.js
const path = require('path');

/** @type {import('webpack').Configuration} */
module.exports = {
  mode: 'none',
  entry: './src/entry.js',

  externals: [
    ({request}, cb) => request.startsWith('extpack') ? cb(null, 'extpack', 'commonjs') : cb(),
  ],

  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].js",
  },
};
// src/entry.js
console.log(require('extpack'));
console.log(require.resolve('extpack/some/file.txt'))

It produces following dist/main.js

// ...
/* 1 */
/***/ ((module) => {

"use strict";
module.exports = require("extpack");

/***/ })
// ...
console.log(__webpack_require__(1));
console.log(/*require.resolve*/(1))
// ...

What is the expected behavior?

With some options to produce following.

// ...
console.log(__webpack_require__(1));
// OR
// console.log(require('extpack'))
console.log(require.resolve('extpack/some/file.txt'))
// ...

What is motivation or use case for adding/changing the behavior?

Entrust all resolutions about some packages to node. It may seem no need to use webpack, but I’d like to use this combined with vercel/next.js.

Also it seems no matter to bundle, but some packages are using node native binaries (nodejs/node-gyp) like require('./build/some.node') or require.resolve('./build/some.node'). They should be resolved in unbundled environment. *.node can’t be run only with itself, so I can’t use type: 'asset/resource' for this. It can be seen as *.node is out of webpack resolution/depedants detection.

How should this be implemented in your opinion?

  • New import type for externals, like "commonjs-require-resolve"
  • And add new property like originalType in context arg to know whether it is require.resolve’d in original source.
// ...
externals: [
  ({request, originalType}, cb) => request.startsWith('extpack') ? cb(null, 'extpack', originalType === 'commonjs-require-resolve' ? originalType : 'commonjs') : cb(),
],
// ...

I’m not passing originalType itself because to correctly transform import ... for example.

Some type of imports are not used as output, so they should be something like "unknown", null, "input-import", etc…

EDIT:

  1. or add new types ("native-commonjs").
// ...
externals: [
  ({request}, cb) => request.startsWith('extpack') ? cb(null, 'extpack', 'native-commonjs') : cb(),
],
// ...

Are you willing to work on this yourself? a little but I’m sorry I can’t work it on soon. It may be after Dec 2021

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
The-x-Theoristcommented, Oct 25, 2021

I want to work on this issue, but I need little help in this before starting.

0reactions
alexander-akaitcommented, Oct 7, 2022

@LitileXueZha Hash used internally not only for contenthash, and for perf/comparation/predictable output. Why you need absolute path?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve | webpack
A list of directories where requests of server-relative URLs (starting with '/') are resolved, defaults to context configuration option. On non-Windows systems ...
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved...
Read more >
How to Polyfill node core modules in webpack 5
Resolved by making the following changes to webpack.config.js ... const NodePolyfillPlugin = require("node-polyfill-webpack-plugin") ...
Read more >
Modules - Grafana k6
In a JavaScript project running NodeJS, modules are imported using either import or require(), using the node module resolution algorithm.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found