简体   繁体   English

使用节点包客户端与requireJS?

[英]Using a node package client-side with requireJS?

I need to parse some CSS files client-side in an app I'm building. 我需要在正在构建的应用程序中解析客户端的一些CSS文件。 There is an excellent npm package that does just that: 有一个出色的npm软件包可以做到这一点:

https://github.com/NV/CSSOM https://github.com/NV/CSSOM

I'd like to take this package, smash it into a one file, and load it as a dependency in my requireJS implementation. 我想拿这个包,将其粉碎成一个文件,并作为它的依赖项加载到我的requireJS实现中。 Essentially, I'd like to use it like this: 本质上,我想这样使用它:

//A module in which I'll use CSSOM - I use CommonJS syntax almost exclusively
define( function(require) {
    var $ = require('jquery');
    var CSSOM = require('lib/CSSOM');
});

Full disclosure, I've never actually coded in node.js before (I'd just really like to use this module). 全面披露,我之前从未真正在node.js中进行编码(我真的很想使用此模块)。 What can I do to make this happen? 我该怎么做才能做到这一点?

Loading CSSOM as a non-AMD script 将CSSOM作为非AMD脚本加载

This will give you CSSOM in one file. 这将在一个文件中提供CSSOM。

CSSOM has instructions to build it as a browser script, from its github site : CSSOM指示从其github站点将其构建为浏览器脚本:

$ git clone https://github.com/NV/CSSOM.git
$ cd CSSOM
$ npm install -d
$ ./node_modules/.bin/jake

Based on what I see there, you'd then only need a shim configuration that states: 根据我在此处看到的内容,您只需要一个shim配置即可,说明如下:

CSSOM: {
    exports: 'CSSOM'
}

Using r.js 使用r.js

This will give a bunch of modules making up CSSOM. 这将提供大量组成CSSOM的模块。 If you'd want a final single file from this, you'd have to ask r.js to optimize the result of ding r.js -convert . 如果要从中获得最终的单个文件,则必须要求r.js优化ding r.js -convert的结果。

Use r.js to convert the code base of CSSOM : 使用r.js到的代码库转换CSSOM

$ r.js -convert in out

Where in is the source tree of CSSOM and out is where you want to result to be. inCSSOM的源代码树, out是您想要得到的结果树。 Because CSSOM says it is pure JS this conversion will probably work without having to do anything else, because I'd expect CSSOM to not use Node-specific modules like fs . 因为CSSOM表示它是纯JS,所以此转换可能无需进行任何其他操作即可工作,因为我希望CSSOM不使用像fs这样的特定于节点的模块。 But there could be some other problems. 但是可能还有其他问题。 If CSSOM , for instance, makes require calls that have a variable rather than a string literal as argument, the converter won't be able to convert it. 例如,如果CSSOM使require调用使用变量而不是字符串文字作为参数,则转换器将无法对其进行转换。 At any rate, you should have a look at the documentation for details of what could go wrong. 无论如何,您应该查看文档以获取可能出现问题的详细信息。

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

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