require

PHP include/require inside functions

橙三吉。 提交于 2019-12-18 04:34:16
问题 Having functions that are quite big and they are loading every time the page is loaded, would be better to write function foo(){ include(.../file_with_function's_code); return; } to minimize the size of the functions script? Or it doesn't matter because when a function is loaded (but not executed) also is loaded the content even if it is into an include? Thank you. (Edit: my question is not about if it's possible or not) 回答1: While @Luceos answer is technically correct (the best kind of

好用的打包工具webpack

筅森魡賤 提交于 2019-12-18 01:44:36
<什么是webpack> webpack是一个模块打包器,任何静态资源(js、css、图片等)都可以视作模块,然后模块之间也可以相互依赖,通过webpack对模块进行处理后,可以打包成我们想要的静态资源。 gulp的打包是将js、css、图片等分开打包的,但是webpack是将所有的静态资源打包到一起,因此一个请求就可以了。 <webpack的特点> ·支持CommonJs(require的写法)和AMD模块,也就是说基本可以无痛迁移旧项目 ·支持模块加载器和插件机制,可对模块灵活定制,特别是babel-loader,有效支持es6 ·可以通过配置,打包成多个文件。有效利用浏览器的缓存功能提升性能。将公用的东西抽离出来,比如jQuery等 ·将样式文件和图片等静态资源视为模块进行打包。配合loader加载器,支持sass、less等css预处理器 ·内置有source map,即使打包在一起也方便调试 <webpack的安装> 1,先全局安装 npm install webpack -g ·webpack -w 提供watch方法,实时进行打包更新 ·webpack -p 对打包后的文件进行压缩 ·webpack -d 提供sourcemap,方便调试 ·webpack --config 以某个config作为打包 ·webpack --help 更多命令 2,再本地安装 npm

cannot redeclare block scoped variable (typescript)

孤街浪徒 提交于 2019-12-18 01:18:37
问题 I'm building a node app, and inside each file in .js used to doing this to require in various packages. let co = require("co"); But getting etc. So using typescript it seems there can only be one such declaration/require across the whole project? I'm confused about this as I thought let was scoped to the current file. I just had a project that was working but after a refactor am now getting these errors all over the place. Can someone explain? 回答1: Regarding the error itself, let is used to

electron 5.0.0 “Uncaught ReferenceError: require is not defined”

纵然是瞬间 提交于 2019-12-17 23:30:00
问题 I had initially been using electron stable (4.x.x), and was able to use require in both my browser and renderer processes. I upgraded to electron beta (5.0.0) because I needed a newer version of node and encountered this error message in my renderer process, Uncaught ReferenceError: require is not defined . Googling and looking through the electron docs, I found comments saying the error could be caused by setting webPreferences.nodeIntegration to false when initializing the BrowserWindow ; e

How does require work with new operator in node.js?

梦想的初衷 提交于 2019-12-17 12:25:16
问题 Let's have a file.js with this code: module.exports.func = function(txt) { this.a = 1; this.b = 2; console.log(txt, this); return this; } Now we have another JS file where we do following: var r1 = new (require('./file')).func('r1'); var r2 = new require('./file').func('r2'); In r1 case it works as intended - r1 contains reference to the newly created object. In r2 case it does not work - r2 gets reference to module.exports from within the file.js. The intention was to create a new object by

Ruby 'require' error: cannot load such file

自古美人都是妖i 提交于 2019-12-17 10:11:34
问题 I've one file, main.rb with the following content: require "tokenizer.rb" The tokenizer.rb file is in the same directory and its content is: class Tokenizer def self.tokenize(string) return string.split(" ") end end If i try to run main.rb I get the following error: C:\Documents and Settings\my\src\folder>ruby main.rb C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- tokenizer.rb (LoadError) from C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb

Is 'require(…)' a common javascript pattern or a library function?

柔情痞子 提交于 2019-12-17 06:46:41
问题 I usually find this as the first line in node.js scripts/modules as well as phantomJS, casperJS etc. I'm curious, if this is a common pattern for server-side javascript (SSJS) (similar to #include in C/C++ or import in Java) or is it a a library like RequireJS or LabJS that is being called for this inclusion (neither of which I have gotten a chance to use in practice, as yet)? e.g. var http = require('http') or var casper = require('casper').create() I'm curious if this is a pattern that has

Webpack and external libraries

强颜欢笑 提交于 2019-12-17 06:04:08
问题 I’m trying out webpack (http://webpack.github.io/) and it looks really nice, however I’m kind of stuck here. Say that I’m using a CDN for a library, f.ex jQuery. Then in my code, I want the require('jquery') to automatically point to the global jquery instance, instead of trying to include it from my modules. I’ve tried using plugins like the IgnorePlugin : new webpack.IgnorePlugin(new RegExp("^(jquery|react)$")) This works for ignoring the library, but it still says that the required module

is there a require for json in node.js

怎甘沉沦 提交于 2019-12-17 02:42:26
问题 I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file. If I wanted to include another JavaScript file I could simply use require . Now I'm using readFileSync and __dirname to get the JSON, which I think is an ugly way to do it. Is there something similar for require that enables me to load a JSON file? 回答1: As of node v0.5.x yes you can require your JSON just as you would require a js file. var someObject = require('.

is there a require for json in node.js

落爺英雄遲暮 提交于 2019-12-17 02:42:05
问题 I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file. If I wanted to include another JavaScript file I could simply use require . Now I'm using readFileSync and __dirname to get the JSON, which I think is an ugly way to do it. Is there something similar for require that enables me to load a JSON file? 回答1: As of node v0.5.x yes you can require your JSON just as you would require a js file. var someObject = require('.