Saw the result of the webcam build. Where the code is divided into modules. This code calls require, and so on. And accordingly, it did not work directly in the browser. It was a SPA. But is it always like this? Those. all that collects webpack, works only on node.js?
1 answer
The webpack itself works only on the node.js machine.
Usually this is a developer machine or a build machine. On the production server webcam is not needed.
The result of the webpack is already working where intended to work.
We can assume that the webpack in the simplest case converts some js files (beautiful and convenient) to other js files (light ones). You use the resulting lightweight files as usual.
Well, that is, in the simplest case, you make an myApp.min.js file containing the entire application from the application files App.js, MyAwesomeComponent.js, MyAwesomeComponent2.js, and simply connect it to the page in the spirit of <script src = "./myApp.min.js"></script>
The code that you saw as a result of the assembly did not cause require from requirejs or from elsewhere, but some internal method called require, defined in this file itself. That is, if the source is self-sufficient - the result is also self-sufficient in most cases.
Of course, if the source is not designed to work in the browser (say runtime requires node.js), then they will not work in the browser. If any imports are used, the location of which is unknown to the webpack, then it will not work either (but he will usually warn about this).
- Thanks for the detailed answer! I realized that I was mistaken) But is it possible to require global modules? After all, the search is in the directory and above, etc. - A. Gusev
- @ A.Gusev. I did not understand the question. Are you looking for shima? github.com/webpack/docs/wiki/shimming-modules That is, if a module lives in a global variable, you can also work with it of course. But it is better not to keep extra global variables, most modern libraries have a normal modular version. - Duck Learns to Take Cover
- No, I’m talking about this: Well, that is, if you don’t have any modules and everything communicates through the global scoop, then there may be problems, as the webpack still wraps the modules in the closure. But this is the problem of the architecture of your application and not the webcam. - A. Gusev
- @ A.Gusev This I explained the words "in most cases." This is a working case, just a little more config will have to be written. I delete probably not to embarrass. - Duck Learns to Take Cover
- one@A.Gusev from the point of view of the OS,
node.jsis a command line utility that is needed to run js files. It is absolutely not necessary to use it only on the server. - Pavel Mayorov