繁体   English   中英

将node_modules合并为单个文件以在parse.com上的托管Express js中使用

[英]Merge node_modules to single file to use in hosted express js on parse.com

我正在使用Parse.com和托管的云代码。 我有一个非常简单的express.js网站,但我不想对web socketsexpress-ws包)提供一些简单的支持。 但是由于Parse对于node_modules文件夹中的模块不能很好地处理require(),所以我遇到了问题。

我正在寻找一种将所有相关模块合并到单个文件中并更新所有require语句的方法。

我已经看了一些解决方案,起初看起来前途无量,像mcjsbnabrowserify ,但我遇到了与每一个问题。

有没有人遇到过类似的问题并找到了解决方案?

一个好的第一步是运行npm dedupe ,它可以:

搜索本地软件包树,并尝试通过将依赖关系进一步移到树上来简化整体结构,在此树中,多个依赖软件包可以更有效地共享它们。

接下来,记下去重复过程报告中的任何“不可避免的冲突”。

这些将是在多个地方使用的模块版本,这些版本无法合并为一个模块(在不同模块中引用了不同的不兼容版本)。 如果可能,您可以通过升级或降级一个或另一个来摆脱它们,以便它们可以共享。

因此,例如, request需要form-data@0.2.0superagent要求form-data@0.1.3 如果可能的话,你就需要通过升级或者解决这个矛盾superagent使用form-data@0.2.0或降级的情况下request使用form-data@0.1.3

最后,您需要将其余模块打包成某种形式,以便您可以从当前脚本中访问它们,就像从文件系统中加载它们一样。

实现此目的的一种方法是使用ender ,该ender专注于处理浏览器的程序包管理,可以轻松地将多个模块组合到一个文件中,以便在云托管环境中使用。

我尚未尝试使用ender ,因此无法说出达到目的的难易程度。 我确实知道它会生成一个自运行的函数,它将向默认范围(nodejs中的global对象)添加函数/功能。

它覆盖的功能之一是节点的require()函数,因此,如果需要在代码中使用它,则需要保留一个副本。

因此,假设您已将应用程序封装到其自己的模块“ Foo”中,该模块以标准方式“需要”多个模块,则首先需要在模块的根目录中运行ender build函数,然后将其包含在类似:

var window={}; // required by ender.

/* 
 * if desired, preserve a copy of node's own require function 
 * note that this is only useful if you expect to require other
 * modules in the "usual" way after ender.js has been evaluated.
 */
var origRequire=require;

/* insert ender.js source (produced by `ender build`) here */

/* invoke ender's version of require to load your own module. */
var Foo=require('Foo');

/* invoke your module's entry point */
Foo.run();

同样,我没有测试过,但是相信它应该可以工作。

更新

嗯...好吧,事实证明express-ws需要编译节点扩展,而该扩展不能与ender或任何模块管理策略一起使用,因为节点扩展必须从源代码针对本地系统体系结构进行编译。

但是,假设我们可以按预期方式加载express-ws ,则在新模块目录中运行ender build ,并将expressexpress-ws作为需求报告:

Robs-MacBook-Pro:foo raisch$ ender build
Installing packages: "ender-core ender-commonjs ."...
ender-core@2.0.0 node_modules/ender-core
ender-commonjs@1.0.8 node_modules/ender-commonjs
Successfully finished installing packages
Assembling build...
Your current build command is: ender build
Your current build size is: 317.9 kB raw, 98.8 kB minified and 30 kB gzipped

Active packages:
├── ender-core@2.0.0 - core client library of Ender
├── ender-commonjs@1.0.8 - commonjs module support for ender
└─┬ foo@1.0.0 - 
  ├─┬ express@4.12.4 - Fast, unopinionated, minimalist web framework
  │ ├─┬ accepts@1.2.7 - Higher-level content negotiation
  │ │ ├─┬ mime-types@2.0.11 - The ultimate javascript content-type utility.
  │ │ │ └── mime-db@1.9.1 - Media Type Database
  │ │ └── negotiator@0.5.3 - HTTP content negotiation
  │ ├── content-disposition@0.5.0 - Create and parse Content-Disposition header
  │ ├── content-type@1.0.1 - Create and parse HTTP Content-Type header
  │ ├── cookie@0.1.2 - cookie parsing and serialization
  │ ├── cookie-signature@1.0.6 - Sign and unsign cookies
  │ ├─┬ debug@2.2.0 - small debugging utility
  │ │ └── ms@0.7.1 - Tiny ms conversion utility
  │ ├── depd@1.0.1 - Deprecate all the things
  │ ├── escape-html@1.0.1 - Escape HTML entities
  │ ├─┬ etag@1.6.0 - Create simple ETags
  │ │ └── crc@3.2.1 - Various CRC JavaScript implementations
  │ ├─┬ finalhandler@0.3.6 - Node.js final http responder
  │ │ ├─┬ debug@2.2.0 - small debugging utility
  │ │ │ └── ms@0.7.1 - Tiny ms conversion utility
  │ │ ├── escape-html@1.0.1 - Escape HTML entities
  │ │ └─┬ on-finished@2.2.1 - Execute a callback when a request closes, finishes, or errors
  │ │   └── ee-first@1.1.0 - return the first event in a set of ee/event pairs
  │ ├── fresh@0.2.4 - HTTP response freshness testing
  │ ├── merge-descriptors@1.0.0 - Merge objects using descriptors
  │ ├── methods@1.1.1 - HTTP methods that node supports
  │ ├─┬ on-finished@2.2.1 - Execute a callback when a request closes, finishes, or errors
  │ │ └── ee-first@1.1.0 - return the first event in a set of ee/event pairs
  │ ├── parseurl@1.3.0 - parse a url with memoization
  │ ├── path-to-regexp@0.1.3 - Express style path to RegExp utility
  │ ├─┬ proxy-addr@1.0.8 - Determine address of proxied request
  │ │ ├── forwarded@0.1.0 - Parse HTTP X-Forwarded-For header
  │ │ └── ipaddr.js@1.0.1 - A library for manipulating IPv4 and IPv6 addresses in JavaScript.
  │ ├── qs@2.4.2 - A querystring parser that supports nesting and arrays, with a depth limit
  │ ├── range-parser@1.0.2 - Range header field string parser
  │ ├─┬ send@0.12.3 - Better streaming static file server with Range and conditional-GET support
  │ │ ├─┬ debug@2.2.0 - small debugging utility
  │ │ │ └── ms@0.7.1 - Tiny ms conversion utility
  │ │ ├── depd@1.0.1 - Deprecate all the things
  │ │ ├── destroy@1.0.3 - destroy a stream if possible
  │ │ ├── escape-html@1.0.1 - Escape HTML entities
  │ │ ├─┬ etag@1.6.0 - Create simple ETags
  │ │ │ └── crc@3.2.1 - Various CRC JavaScript implementations
  │ │ ├── fresh@0.2.4 - HTTP response freshness testing
  │ │ ├── mime@1.3.4 - A comprehensive library for mime-type mapping
  │ │ ├── ms@0.7.1 - Tiny ms conversion utility
  │ │ ├─┬ on-finished@2.2.1 - Execute a callback when a request closes, finishes, or errors
  │ │ │ └── ee-first@1.1.0 - return the first event in a set of ee/event pairs
  │ │ └── range-parser@1.0.2 - Range header field string parser
  │ ├─┬ serve-static@1.9.3 - Serve static files
  │ │ ├── escape-html@1.0.1 - Escape HTML entities
  │ │ ├── parseurl@1.3.0 - parse a url with memoization
  │ │ ├─┬ send@0.12.3 - Better streaming static file server with Range and conditional-GET support
  │ │ │ ├─┬ debug@2.2.0 - small debugging utility
  │ │ │ │ └── ms@0.7.1 - Tiny ms conversion utility
  │ │ │ ├── depd@1.0.1 - Deprecate all the things
  │ │ │ ├── destroy@1.0.3 - destroy a stream if possible
  │ │ │ ├── escape-html@1.0.1 - Escape HTML entities
  │ │ │ ├─┬ etag@1.6.0 - Create simple ETags
  │ │ │ │ └── crc@3.2.1 - Various CRC JavaScript implementations
  │ │ │ ├── fresh@0.2.4 - HTTP response freshness testing
  │ │ │ ├── mime@1.3.4 - A comprehensive library for mime-type mapping
  │ │ │ ├── ms@0.7.1 - Tiny ms conversion utility
  │ │ │ ├─┬ on-finished@2.2.1 - Execute a callback when a request closes, finishes, or errors
  │ │ │ │ └── ee-first@1.1.0 - return the first event in a set of ee/event pairs
  │ │ │ └── range-parser@1.0.2 - Range header field string parser
  │ │ └── utils-merge@1.0.0 - merge() utility function
  │ ├─┬ type-is@1.6.2 - Infer the content-type of a request.
  │ │ ├── media-typer@0.3.0 - Simple RFC 6838 media type parser and formatter
  │ │ └─┬ mime-types@2.0.11 - The ultimate javascript content-type utility.
  │ │   └── mime-db@1.9.1 - Media Type Database
  │ ├── vary@1.0.0 - Manipulate the HTTP Vary header
  │ └── utils-merge@1.0.0 - merge() utility function
  └─┬ express-ws@0.2.6 - WebSocket endpoints for express applications
    ├── url-join@0.0.1 - Join urls and normalize as in path.join.
    └─┬ ws@0.4.32 - simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455
      ├── commander@2.1.0 - the complete solution for node.js command-line programs
      ├── nan@1.0.0 - Native Abstractions for Node.js: C++ header for Node 0.8->0.12 compatibility
      ├── tinycolor@0.0.1 - a to-the-point color module for node
      └── options@0.0.6 - A very light-weight in-code option parsers for node.js.

在处理我的两个顶级需求并将所有需求都添加到第n层时,它看起来非常不错。

包装ender.js文件,构建过程在以下位置生成:

var window={},
    util=require('util'),
    origRequire=require;

// insert ender.js here

var express=require('express'); // using ender's require

console.log(util.inspect(express,{depth:null}));

并运行它会产生一个与缺少的mime-db/db.json文件相关的错误,这向我表明mime-db/db.json无法遵循accepts模块使用的mime-types所需的mime-db的正确包含。

因此,即使express-ws问题可以解决,您似乎也需要遍历所有必需的模块才能看到哪些模块(如accepts )需要特殊处理。

暂无
暂无

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

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