-
Notifications
You must be signed in to change notification settings - Fork 163
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 asset/resource module incorrectly handling client-side extensions using require
(ReferenceError: require is not defined
)
#1617
Comments
I just noticed that there is a processed asset of I'm not that familiar with minimisation (I prefer not to look at minimised code!) but I think I can piece together some parts of the bundled output (
In non-minimized form the relevant code is: var map = {
"./config": 6348,
"./config.json": 6348,
"./languageSelector": 3795,
"./languageSelector.js": 3795,
"./navbar": 9696,
"./navbar.js": 9696,
"./nextstrain-logo-small.png": 85976,
"./splash": 58230,
"./splash.js": 58230
};
...
var logoPNG = require("./nextstrain-logo-small.png");
...
react__WEBPACK_IMPORTED_MODULE_0__.createElement("img", {
alt: "splashPage",
width: "40px",
src: logoPNG
})
...
/***/ 85976:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
"use strict";
module.exports = __webpack_require__.p + "13fec0d41212fe68ae22.png";
/***/ }), I can fix the 🐛 via either of the following ways - the second being the typical way webpack "requires" assets and the way it is doing it for images within the auspice src tree. P.S. make sure to gzip the bundle to see the change - var logoPNG = require("./nextstrain-logo-small.png");
+ var logoPNG = __webpack_require__.p + "13fec0d41212fe68ae22.png" - var logoPNG = require("./nextstrain-logo-small.png");
+ var logoPNG = __webpack_require__(85976) I don't think i'll do any more today (friday afternoon). I'm unsure if this is a webpack bug or a configuration bug on our side. |
@jameshadfield sorry you had to go down this rabbit hole! Nice dissection of the minimized code. Without doing anything on the Auspice side, this error can be patched by adapting the downstream code to work (in the nextstrain.org example, replacing However, I agree that this seems like a Webpack bug since |
Using Note that
|
There's a hint in the webpack output of Before nextstrain/nextstrain.org@8b0c656 (with
After nextstrain/nextstrain.org@8b0c656 (with
The only difference is the I currently don't know enough about webpack to understand this output. Might be a good reason to go through the documentation/tutorials more thoroughly. |
This is a fair point, but I want to understand better. How does Auspice differentiate between non-customised vs. customised code? From what I can tell, customised code defined by the
Are those the only differences between the non-customised and customised // from auspice/src/components/navBar/content.js
// non-customised, works fine
const logoPNG = require("../../images/logo-light.svg");
// from nextstrain.org/auspice-client/customisations/navbar.js
// customised through auspice build --extend, does not work
const logoPNG = require("./nextstrain-logo-small.png"); |
Yup, that's right. Couple of notes that may or may not help:
|
require
(ReferenceError: require is not defined
)
I've updated the issue description with more current repro steps and the known workaround. I've also categorized on the project board as:
Since I've hit a brick wall at this point, and given the categorization, I'm going to backlog this and work on some other things for now. |
Current Behavior
The nextstrain.org-customised version of auspice is broken as a png resource there is not correctly transformed by auspice's webpack configuration.
How to reproduce
Open a shell prompt in the Auspice root directory.
Run:
Load http://localhost:4000/zika -- the app crashes immediately with a console error
Uncaught (in promise) ReferenceError: require is not defined
Workaround
Use
import
instead ofrequire
(example).What's going on (I think)
Update -- see next post, but the following may be useful context
The bundles (in
auspice-client/dist/
) contain onerequire()
statement coming from this line in the customisation code. These commonJSrequire()
statements should be handled by (auspice's) webpack - in webpack v4 this was handled via a file-loader but v5 now uses asset modules. The relevant part of our webpack config is:auspice/webpack.config.js
Lines 248 to 251 in a4487b5
Since the non-customised auspice code includes plenty of
require
statements which are correctly handled, this strongly implies the asset-module is not considering the customised code which lives outside ofnode_modules/auspice
A long time ago we had to specify which directories we wanted to include for the file-loader, this was removed by c93e97b ~3 years ago. I think the solution is something similar - we have to specify to the asset-module to specifically include the files which are part of the customised code, and thus not in the auspice tree.
Solutions I've tried which don't work (otherwise this would be a PR)
I've found it's easier to debug this within the context of nextstrain.org (see above) by modifying the
<nextstrain.org>/node_modules/auspice/webpack.config.js
filedirectoriesToTransform
:This results in webpack warnings indicating that assets outside auspice's
src
aren't being included here such when they should (e.g.ERROR in ../typeface-lato/files/lato-latin-900italic.woff2 1:4 ... You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
)(same error)
Attempted to recreate what we removed in c93e97b -- same error (unsurprised by this stage)
I tried undoing 7892c59 and reverting to the file-loader (which is soon to be deprecated). I thought this would work, but same
require
error 🤔What we're trying to do in auspice is slightly off the beaten track, and I remember this was tricky to get working. I can't explain why the new version of webpack worked for me in testing last week -- recreating that now results in the same
require
error.The text was updated successfully, but these errors were encountered: