简体   繁体   English

如何在浏览器中将旧版JavaScript与NodeJS集成

[英]How to integrate legacy JavaScript with NodeJS in the browser

Hypothetical legacy scenario: 假设的遗留场景:

Say I have 4 scripts that simply get concatenated together as a single script for deployment to provide a utility library with a global namespace in the browser. 假设我有4个脚本,它们被简单地串联在一起作为一个单独的脚本进行部署,以在浏览器中为实用程序库提供一个全局名称空间。 I then have 1 separate application script that utilizes the library. 然后,我有1个利用该库的独立应用程序脚本。 Something like: 就像是:

<script src="library.js"></script>
<script src="app.js"></script>

In my app.js script, calls are made to the library methods through its namespace, like 在我的app.js脚本中,通过其命名空间对库方法进行了调用,例如

var id = lib.id(prefix);

I see that 1 of the 4 library scripts is a useful utility that I want to turn into a Node module. 我看到4个库脚本中的1个是有用的实用程序,我想将其转换为Node模块。 I copy it and create a package and publish it to npm to use in new development. 我复制它并创建一个包,然后将其发布到npm以用于新开发中。

Problem: Now I have two versions of this script to maintain - one for Node and one for the legacy library. 问题:现在,我需要维护此脚本的两个版本-一个用于Node,一个用于旧版库。

Is there a way to have one common file that I can include in both the Node module and the legacy library? 有没有一种方法可以在Node模块和旧版库中包含一个通用文件? Or am I stuck maintaining two versions of the file until we phase out the legacy code? 还是我要坚持维护文件的两个版本,直到我们逐步淘汰旧代码?

Additional info: 附加信息:

I looked at browserify and webpack, thinking they might be useful, but both suffer from a problem I don't know how to get around. 我查看了browserify和webpack,以为它们可能有用,但是都遇到了一个我不知道如何解决的问题。 Unless the end module defines global variables, I can't use the module in legacy code, as there is no require command available. 除非最终模块定义了全局变量,否则我无法在旧版代码中使用该模块,因为没有可用的require命令。 In other words, I can't browserify a Node module, then drop it into a legacy web page and use it in my existing app.js, because I can't call var mymodule = require('mymodule') . 换句话说,由于无法调用var mymodule = require('mymodule') ,因此无法浏览节点模块,然后将其放入旧版网页并在现有的app.js中使用它。 Is there any way to get browserify or webpack to define and expose a require function to let me access my new Node module from a legacy codebase? 有什么方法可以让browserify或webpack定义和公开require函数,以允许我从旧版代码库访问新的Node模块? Surely, this is not a unique scenario. 当然,这不是唯一的情况。

Your best bet is to use a UMD pattern to ensure that the module can be used both as a global and in commonJS (ie- node) systems. 最好的选择是使用UMD模式,以确保该模块既可以用作全局系统,也可以在commonJS(即节点)系统中使用。

The pattern that would fit best for you is returnExports . 最适合您的模式是returnExports This ensures that all exported properties are either applied to the global object (ie- window) or to the module object in node. 这样可以确保将所有导出的属性应用到全局对象(即window)或节点中的模块对象。

Since you are not using AMD, you can simplify the pattern somewhat and remove the first chunk of the if statements on lines 18 and 42. 由于您未使用AMD,因此可以稍微简化模式,并删除第18和42行上的if语句的第一块。

This will require you to reorganize your code so that it fits in with the pattern. 这将需要您重新组织代码,使其与模式相适应。

There are other patterns that you may use, such as commonJsStrictGlobal , but I personally prefer returnExports. 您还可以使用其他模式,例如commonJsStrictGlobal ,但我个人更喜欢returnExports。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM