简体   繁体   English

编写和组织javascript插件的最佳做法是什么?

[英]What are the best practices for writing and organizing javascript plugins?

So I have a nice chunk of code that is the same across many different javascript files, but I occasionally have to update it due to path changes or other various conditions. 所以我有很多代码在许多不同的javascript文件中是相同的,但我偶尔会因路径变化或其他各种条件而更新它。 Now copy and pasting it all over to the new files works fine but is annoying to execute. 现在将它复制并粘贴到新文件上工作正常,但执行起来很烦人。 Is there a good way to maintain one javascript file with my "plugin" code and have it be accessible by other javascript files that use the plugin? 有没有一个很好的方法来维护一个javascript文件与我的“插件”代码,并让它可以访问使用该插件的其他JavaScript文件?

I am looking for both a good nodejs solution, and vanilla js solution. 我正在寻找一个好的nodejs解决方案和vanilla js解决方案。 If they could be mutually shared that'd be ideal, but not required by any means. 如果他们可以相互分享,那将是理想的,但不是任何方式所要求的。 Ideally, I'd like to host my workspace in workspace/ and have some folders, workspace/front-end-js/ and workspace/back-end-nodejs/ , be able to run code off a plugin in workspace/plugins/ so that I can execute things like MyPluginVar.Foo(); 理想情况下,我想在workspace/区中托管我的工作workspace/并且有一些文件夹, workspace/front-end-js/workspace/back-end-nodejs/ ,能够在workspace/plugins/插件中运行代码我可以执行像MyPluginVar.Foo();

I am aware of some systems, like the node's var foo = require('bar'); 我知道一些系统,比如节点的var foo = require('bar'); and the frontend browserified version, but really do not know all my options. 和前端浏览器版本,但真的不知道我的所有选项。 What's the best way of writing and organizing javascript plugins? 编写和组织javascript插件的最佳方法是什么?

-- -

Edit: I'm really trying to avoid npm, but it might be the best option. 编辑:我真的想避免npm,但它可能是最好的选择。

You typically add your shared libraries and plugins as dependencies to your project's package.json file and install them using npm. 您通常将共享库和插件作为依赖项添加到项目的package.json文件中,并使用npm进行安装。

CommonJS modules , which use the module.exports object and require function, are the de facto standard at the moment. CommonJS模块使用module.exports对象和require函数,是目前事实上的标准。

ES2015 modules , which use the export and import statements, are an emerging formal standard, but support for them is not yet universal. ES2015模块使用exportimport语句,是一种新兴的正式标准,但对它们的支持尚未普及。 They are a good option if your environment supports them. 如果您的环境支持它们,它们是一个不错的选择。

To load either type of module on the front end, you will need to use a bundler such as Webpack or Browserify. 要在前端加载任何类型的模块,您需要使用捆绑器,如Webpack或Browserify。

Older javascript modules typically publish to the global scope ( window ), and are a simple option for front end code. 较旧的javascript模块通常发布到全局范围( window ),并且是前端代码的简单选项。

You can also support multiple module systems if you like by using a UMD (Universal Module Definition) wrapper. 如果您愿意,也可以使用UMD(通用模块定义)包装器来支持多个模块系统。 Here's an example from the UMD Github repo that leverages CommonJS if supported, while falling back to a browser global: 以下是UMD Github仓库中的一个示例,如果支持,它会利用CommonJS,同时回退到浏览器全局:

(function (root, factory) {
    if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
        // CommonJS
        factory(exports, require('b'));
    } else {
        // Browser globals
        factory((root.commonJsStrictGlobal = {}), root.b);
    }
}(this, function (exports, b) {
    // b represents some dependency

    // attach properties to the exports object to define
    // the exported module properties.
    exports.action = function () {};
}));

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

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