Feature Request: Options to keep `require.resolve` as external
See original GitHub issueContinuation 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
incontext
arg to know whether it isrequire.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:
- 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:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
I want to work on this issue, but I need little help in this before starting.
@LitileXueZha Hash used internally not only for contenthash, and for perf/comparation/predictable output. Why you need absolute path?