最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Refactoring a userscript to use javascript modules - Stack Overflow

programmeradmin4浏览0评论

I'm working on a userscript - in particular this userscript - which has been designed to encapsulate functionality in modules. In order to be able to do some automated testing I would like to split the modules into their own files and use node.js's module exporting and require functions to bine into one file for use in Greasemonkey or simple browser extensions.

My first thought was to just copy the modules into their own files as such

module.js

var exportedModule = (function (){  
    var Module = {  
        // public functions and members  
    };  

    //private functions and members  

    return Module;  
}());  

module.exports = exports = exportedModule;  

And then have a central file that requires each of these modules, perhaps piling them with something like Browserify.

script.js

var importedModule = require(./module);

importedModule.init();

Is this possible?

I'm working on a userscript - in particular this userscript - which has been designed to encapsulate functionality in modules. In order to be able to do some automated testing I would like to split the modules into their own files and use node.js's module exporting and require functions to bine into one file for use in Greasemonkey or simple browser extensions.

My first thought was to just copy the modules into their own files as such

module.js

var exportedModule = (function (){  
    var Module = {  
        // public functions and members  
    };  

    //private functions and members  

    return Module;  
}());  

module.exports = exports = exportedModule;  

And then have a central file that requires each of these modules, perhaps piling them with something like Browserify.

script.js

var importedModule = require(./module);

importedModule.init();

Is this possible?

Share Improve this question edited Feb 26, 2013 at 0:15 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Feb 25, 2013 at 21:12 cat dadcat dad 3184 silver badges12 bronze badges 2
  • Note that require is a keyword in userscripts. The old title was a bit confusing. ... PS: You should be using @require for this kind of thing AMAP, but it's only fully supported in Greasemonkey and Tampermonkey, at the moment. – Brock Adams Commented Feb 26, 2013 at 0:17
  • @BrockAdams Thanks for the clarification edit. We were using require but are working to bundle this script into browser extensions. I'm mostly looking into things like nodejs and requirejs for ways to improve our development process. – cat dad Commented Feb 26, 2013 at 21:05
Add a ment  | 

2 Answers 2

Reset to default 3

It seems to me that you would be better off using Requirejs, which uses AMD style modules and is inherently more browser friendly. Node monjs-style modules are synchronous and do not fit the browser model very well.

Of course, using requirejs will change your scripts a bit.

It's possible, and Browserify makes it easy:

browserify src/my.user.js -o dist/my.user.js

The metadata in the source file may get moved, but it's still parsed correctly (by Greasemonkey at least).

For a more plex example which piles various assets, including CSS and images, see here.

发布评论

评论列表(0)

  1. 暂无评论