所以,我正在用node/express + jade combo编写一个应用程序.
我有client.js,在客户端加载.在该文件中,我有从其他JavaScript文件调用函数的代码.我的尝试是使用
var m = require('./messages');
Run Code Online (Sandbox Code Playgroud)
为了加载messages.js(就像我在服务器端一样)的内容,然后加载该文件的调用函数.但是,require没有在客户端定义,它会抛出窗体的错误Uncaught ReferenceError: require is not defined.
这些其他JS文件也在客户端的运行时加载,因为我将链接放在网页的标题上.因此,客户端知道从这些其他文件导出的所有函数.
如何从打开服务器套接字messages.js的主client.js文件中的其他JS文件(例如)中调用这些函数?
在浏览器中加载CommonJS模块作为客户端javascript的最佳方法是什么?
CommonJS模块将其功能放在module.exports命名空间中,通常包含require(pathToModule)在服务器端脚本中.在客户端加载它们不能以相同的方式工作(需要更换,需要考虑异步等)
我找到了模块加载器和其他解决方案:Browserify,RequireJS,yabble等,或者简单地改变模块的方法.您认为最好的方式和原因是什么?
好吧,我知道Webpack允许我们导入包,require这是Webpack的基础设施.
但是,当你不在require条目文件中使用时,这不是没用的吗?
我有这个test.js条目:
console.log('Test');
Run Code Online (Sandbox Code Playgroud)
和输出
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: …Run Code Online (Sandbox Code Playgroud) 当Anders Hejlsberg在下面的视频中在35:00左右谈论外部模块时......
...为什么文件server.ts需要对node.d.ts的三次斜杠引用,其中hello.ts不需要类似的引用server.ts?
特别是hello.ts他提到intellisense存在加上他可以使用从中导出的项目server.ts.那么三重斜杠引用还能增加什么呢?
我在不同的文件中有两个类:
export class ActionsCollection{
constructor(greeting :string){
this.greet(greeting);
}
public greet(greeting :string) {
return "<h1>"+greeting+"</h1>";
}
}
Run Code Online (Sandbox Code Playgroud)
和
import {ActionsCollection} from "./actionsCollection";
class Greeter extends ActionsCollection{
constructor(public greeting: string) {
super(greeting);
}
}
alert(new Greeter("Hello, world!"));
Run Code Online (Sandbox Code Playgroud)
Greeter在这样的文件中生成,其中存在require行("./ actionsCollection")。但是我想确保所有文件(* .ts)都只在一个文件main.js中生成,不需要require。我可以那样做吗?如果是这样,怎么办?
ps同时,对于装配,您可以使用标准的WebStorm工具和Gulp。而且,除了gulp的模块之外。
我想从Node.js生成客户端JavaScript.我找到了Browserify,但除了Browserify还有其他选择吗?
javascript ×4
node.js ×4
typescript ×2
browserify ×1
commonjs ×1
express ×1
module ×1
pug ×1
sockets ×1
webpack ×1