We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我意识到可能有初学者希望组织他们的代码。现在是 2022,如果您正在考虑开发模块化 JS 应用,那么您应该立即开始使用 npm 和 Webpack。
以下是一些简单的入门步骤:
npm init -y
来初始化 npm 项目npm install webpack webpack-cli
>特别注意
_bundle.js
文件 - 这将是webpack生成的最终JS文件,你不会直接修改它(继续阅读)。/app.js
,您将在其中导入其他模块:print-hello.js
模块:< ;project-root>/webpack.config.js
并复制粘贴以下内容:在上面的代码中,有两点:
app.js
是您编写 JS 的位置代码。它将导入其他模块,如上所示。_bundle.js
是 webpack 生成的最终包。这就是您的 html 最后将看到的内容。package.json
,并将scripts
替换为以下命令:app.js
并生成_bundle.js
文件,运行:npm start
。I realize there may be beginners looking to organize their code. This is 2022, and if you're considering a modular JS app, you should get started with npm and Webpack right now.
Here are a few simple steps to get started:
npm init -y
to initialize an npm projectnpm install webpack webpack-cli
Pay special attention to
_bundle.js
file - this will be a final JS file generated by webpack, you will not modify it directly (keep reading).<project-root>/app.js
in which you will import other modules:print-hello.js
module:<project-root>/webpack.config.js
and copy-paste the following:In the code above, there are 2 points:
app.js
is where you will write your JS code. It will import other modules as shown above._bundle.js
is your final bundle generated by webpack. This is what your html will see at the end.package.json
, and replacescripts
with the following command:app.js
and generate the_bundle.js
file by running:npm start
.查看 ender。它做了很多这样的事情。
另外, browserify 也相当不错。我已经使用了 require-kiss1 并且它有效。可能还有其他人。
我不确定 RequireJS。它只是与节点不一样。从其他位置加载时可能会遇到问题,但它可能会起作用。只要有provide方法或者可以调用的东西就可以了。
TL;DR - 我建议使用 browserify 或 require-kiss。
更新:
1:require-kiss现已死亡,作者已将其删除。从那时起我就一直使用 RequireJS 没有任何问题。 require-kiss 的作者写了 pakmanager 和 pakman。完全披露,我与开发商合作。
我个人更喜欢 RequireJS。它更容易调试(您可以在开发中拥有单独的文件,在生产中拥有单个部署的文件)并且构建在可靠的“标准”之上。
Check out ender. It does a lot of this.
Also, browserify is pretty good. I've used require-kiss¹ and it works. There are probably others.
I'm not sure about RequireJS. It's just not the same as node's. You may run into problems with loading from other locations, but it might work. As long as there's a provide method or something that can be called.
TL;DR- I'd recommend browserify or require-kiss.
Update:
1: require-kiss is now dead, and the author has removed it. I've since been using RequireJS without problems. The author of require-kiss wrote pakmanager and pakman. Full disclosure, I work with the developer.
Personally I like RequireJS better. It is much easier to debug (you can have separate files in development, and a single deployed file in production) and is built on a solid "standard".
我写了一个小脚本,允许异步和同步加载 Javascript 文件,这可能在这里有用。它没有依赖关系,并且与 Node.js 和 Node.js 兼容。 CommonJS。安装非常简单:
然后只需将以下行添加到 HTML 中即可加载主模块:
在主模块(当然还有任何子模块)中,您可以使用
require()
正如您从 CommonJS/NodeJS 中知道的那样。完整的文档和代码可以在 GitHub 上找到。I wrote a small script which allows asynchronous and synchronous loading of Javascript files, which might be of some use here. It has no dependencies and is compatible to Node.js & CommonJS. The installation is pretty easy:
Then just add the following lines to your HTML to load the main-module:
Inside your main-module (and any sub-module, of course) you can use
require()
as you know it from CommonJS/NodeJS. The complete docs and the code can be found on GitHub.Ilya Kharlamov 很棒的答案的变体,其中包含一些代码,使其可以与 Chrome 开发人员工具完美配合。
A variation of Ilya Kharlamov great answer, with some code to make it play nice with chrome developer tools.
由于阻塞,最好不要在生产中使用。 (在 Node.js 中,require() 是一个阻塞调用,这很好)。
Better not to be used in production because of the blocking. (In node.js, require() is a blocking call is well).
Require-stub — 在浏览器中提供符合节点要求的
require
,同时解决这两个问题模块和相对路径。使用类似于 TKRequire (XMLHttpRequest) 的技术。生成的代码是完全可浏览器化的,因为
require-stub
可以替代watchify
。Require-stub — provides node-compliant
require
in browser, resolves both modules and relative paths. Uses technic similar to TKRequire (XMLHttpRequest).Resulting code is fully browserifyable, in that
require-stub
can serve as a replacement forwatchify
.Webmake 将 Node 风格的模块捆绑到浏览器中,尝试一下。
Webmake bundles Node-style modules to Browser, give it a try.