-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack.NamedModulesPlugin() has been deprecated #134
Comments
Tell me how it goes. But should be fine 🤞 |
I still get errors that look like this: |
@theKashey do you have any update for this? I see the issue that looks similar to the issue I am facing, but is there any solution yet? Thank you! |
Also having the same issue. Pinging @theKashey in case there's something else to be done. I wonder if it has something to do with having Edit: nah, I reverted that change and it's still a problem. |
Any example replicating the problem, the one I can fix, would be very welcomed. If only you can prune your project to the state you can share it with me (including in private) 🙏 |
I decided to move on and try something else, but you could probably recreate it from my setup pretty easily. Just edit this webpack config (edited to permalink so this doesn't break when I push next time - branch name is In the meantime I worked around needing rewiremock altogether by setting |
Also it may be that I left the HMR plugin out of my config, as I was trying to see whether I could run my tests against a bundle that I felt comfortable shipping. I think running tests against a dev build is helpful when diagnosing issues, but I need to be able to test the exact bundle that I actually ship. |
HMR is what brings more or less "real module system" rewiremock can work with. Without it nothing could happen, but 🤔 initialization code should throw an error is something is not right |
What's HMR bringing to Webpack 5's module system that the following config doesn't already have? The below config needs a little bit of module ID transformation, but so far
|
HMR makes module flexible not hard-wired, so one can tap in and change what is required. |
By that do you mean that it allows you to manipulate |
me too |
Has anyone been able to get this working with Webpack 5? I re-wrote the plugin to fix the @theKashey is there anything we can do to get this working? |
😭 "Continue investigations". A small example I can play with and try to get fixed would be of great help. |
Thanks for getting on this. Let me know how I can help. |
Here's how far I got Reduced irrelevant transforms from babel
{
"presets": [
"@babel/typescript",
[
"@babel/env",
{
"modules": false,
"exclude": [
"@babel/plugin-transform-typeof-symbol",
"@babel/plugin-transform-regenerator",
"@babel/plugin-transform-async-to-generator"
]
}
]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"rewiremock/babel"
]
} Added logging to global['_REWIREMOCK_HOISTED_'] = global['_REWIREMOCK_HOISTED_'] || [];
console.log("unshifting webpack");
global['_REWIREMOCK_HOISTED_'].unshift( function (rewiremock, api) {
api.overrideEntryPoint(module);
rewiremock.addPlugin(api.plugins.nodejs);
}); Added logging to if (global['_REWIREMOCK_HOISTED_']) {
console.log("calling out", global['_REWIREMOCK_HOISTED_'].length);
global['_REWIREMOCK_HOISTED_'].forEach(function (cb) {
cb(API.mockModule, { plugins: _index2.default, overrideEntryPoint: overrideEntryPoint });
});
console.log("finished calling out", global['_REWIREMOCK_HOISTED_'].length);
global['_REWIREMOCK_HOISTED_'] = [];
} Added similar logging to Chrome Devtools- check "Show ignore-listed frames" Built and examined correct output from webpack 4 __webpack_require__.r(__webpack_exports__);
/* WEBPACK VAR INJECTION */(function(global, module) {/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "greet", function() { return greet; });
/** rejig MATCH */ (function rwrmck() {
global["_REWIREMOCK_HOISTED_"] = global["_REWIREMOCK_HOISTED_"] || [];
global["_REWIREMOCK_HOISTED_"].push(function (rewiremock) {
rewiremock("./src/calculator.ts").with({
add: (x, y) => {
console.log('[mocked]');
return 2;
}
});
rewiremock.enable();
});
})('rwrmck');/** rejig AFTER MATCH */ /* harmony import */ var _initRewiremock__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./initRewiremock */ "./src/initRewiremock.ts");
/* harmony import */ var _calculator__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./calculator */ "./src/calculator.ts");
console.log('[index.ts] prior');
console.log('[index.ts] [module] mock-initialized', module.i, module, 'require');
_initRewiremock__WEBPACK_IMPORTED_MODULE_0__["rewiremock"].disable();
global["_REWIREMOCK_HOISTED_"] = [];
var greet = async function greet() {
console.log('[index.ts] [greet] invoked');
var e = document.createElement('h1');
e.innerText = "".concat(Object(_calculator__WEBPACK_IMPORTED_MODULE_1__["add"])(5, 5));
document.body.appendChild(e);
return null;
};
greet();
/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../node_modules/webpack/buildin/global.js */ "./node_modules/webpack/buildin/global.js"), __webpack_require__(/*! ./../node_modules/webpack/buildin/harmony-module.js */ "./node_modules/webpack/buildin/harmony-module.js")(module)))
//# sourceURL=webpack:///./src/index.ts? Incorrect output from Webpack 5 (the Harmony import of __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ greet: () => (/* binding */ greet)
/* harmony export */ });
/* harmony import */ var _initRewiremock__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./initRewiremock */ "./src/initRewiremock.ts");
/* module decorator */ module = __webpack_require__.hmd(module);
(function rwrmck() {
__webpack_require__.g["_REWIREMOCK_HOISTED_"] = __webpack_require__.g["_REWIREMOCK_HOISTED_"] || [];
__webpack_require__.g["_REWIREMOCK_HOISTED_"].push(function (rewiremock) {
rewiremock("./src/calc-dep.ts").with({
extra: (x, y) => {
return 34;
}
});
rewiremock.enable();
});
})('rwrmck');
_initRewiremock__WEBPACK_IMPORTED_MODULE_0__.rewiremock.disable();
__webpack_require__.g["_REWIREMOCK_HOISTED_"] = [];
console.log('[index.ts] [module]', module, 'require', '6');
var greet = async function greet() {
var calculator = await __webpack_require__.e(/*! import() */ "src_calculator_ts").then(__webpack_require__.bind(__webpack_require__, /*! ./calculator */ "./src/calculator.ts"));
console.log('[index.ts] [greet] invoked');
var e = document.createElement('h1');
e.innerText = "".concat(calculator.add(5, 5));
document.body.appendChild(e);
return null;
};
greet();
//# sourceURL=webpack://explore-webpack-mocking/./src/index.ts? I think, the As well, /***/if (typeof __webpack_require__ !== 'undefined') {
try {
var rewiremockInterceptor = __webpack_require__('${file}');
if (rewiremockInterceptor && rewiremockInterceptor.default) {
__webpack_require__ = rewiremockInterceptor.default(__webpack_require__, typeof module === "undefined" ? __unused_webpack_module : module);
}
} catch (e) {debugger;} |
Here's the const {relative} = require("path");
const {ConcatSource} = require("webpack-sources");
const normalizePath = a => a[0] === '.' ? a : './' + a;
const file = normalizePath(relative(process.cwd(), __dirname + '/src/interceptor.js').replace(/\\/g, '/'));
const injectString = `/***/if(typeof __webpack_require__!=='undefined') {
try {
var rewiremockInterceptor = __webpack_require__('${file}');
if (rewiremockInterceptor && rewiremockInterceptor.default) {
__webpack_require__ = rewiremockInterceptor.default(__webpack_require__, module);
}
} catch (e) {}
}
`;
class RewiremockPlugin {
apply(compiler) {
compiler.hooks.compilation.tap('RewiremockPlugin', function (compilation) {
compilation.moduleTemplates.javascript.hooks.render.tap('RewiremockPlugin', function (moduleSource, module_) {
if (!module_.resource || module_.resource.includes("node_modules")) { return moduleSource; }
const source = new ConcatSource();
const src = moduleSource.source();
// and injection
if (src.indexOf('require') > 0) {
source.add(injectString);
}
// re-hoists mocks
const firstImport = src.indexOf('/* harmony import');
if (src.indexOf('rwrmck') > 0 && firstImport > 0) {
const match = src.match(/\(function rwrmck\(([\s\S]*)rwrmck'\);/g);
if (match && match.length) {
moduleSource = [
/** merely
eval("__webpack_require__.r(__webpack_exports__);\\n/* WEBPACK VAR INJECTION (function(global, module)
{/* harmony export (binding) __webpack_require__.d(__webpack_exports__, \\"greet\\", function()
{ return greet; });\\n'
*/
'/** rejig */ ',
src.substr(0, firstImport),
/** function rwrmck() {\\n global[\\"_REWIREMOCK_HOISTED_\\"] = global[\\"_REWIREMOCK_HOISTED_\\"] || [];\\n global[\\"_REWIREMOCK_HOISTED_\\"].push(function (rewiremock) {\\n rewiremock(\\"./src/calculator.ts\\").with({\\n add: (x, y) => {\\n console.log('[mocked]');\\n return 2;\\n }\\n });\\n rewiremock.enable();\\n });\\n})('rwrmck'); */
'/** rejig MATCH */ ',
match[0],
'/** rejig AFTER MATCH */ ',
src.substr(firstImport).replace(match[0], ''), // contains initRewriteMock import
'/** end rejig */ ',
].join('');
}
source.add(moduleSource);
} else {
source.add(moduleSource);
}
return source;
});
});
}
}
module.exports = RewiremockPlugin; |
Right we might have a fix |
To use rewiremock, according to the docs we need to use the
NamedModulesPlugin()
.But this plugin has been deprecated, and if we use it we get a compiler error that says "NamedModulesPlugin is not a constructor"
Can this be updated? Also can rewiremock be upgraded to make it compaible with webpack 5?
I have "rewiremock": "^3.11.1" in my package.json
The text was updated successfully, but these errors were encountered: