require

require.main.require works but not inside Mocha test

天涯浪子 提交于 2019-12-05 03:42:10
I have written a global function for requiring certain files of my app/framework: global.coRequireModel = function(name) { // CRASH happens here return require.main.require('./api/_co' + name + '/_co' + name + '.model'); } This module is in /components/coGlobalFunctions. It is required in my main app app.js like this: require('./components/coGlobalFunctions'); Then in other modules using "something" from the framework I use: var baseScheme = coRequireModel('Base'); This works but not in the Mocha tests which give me a "Error: Cannot find module" right before the require.main.require call. It

Node.js global require

最后都变了- 提交于 2019-12-05 03:14:39
how can i require a module globally so i can use it in different modules without having to require it again? or do i just have to do that everytime? is there any best practice for this? heres an example of what i am talking about. lets say i have an index.js like this: var a = require('a.js'), utils = require('utils.js'); var str = 'hello this is a test'; str = a.change(str); utils.return(str); a.js var utils = require('utils.js'); exports.change = function(str) { str = str.replace('test', 'example'); utils.return('i changed test to example!'); return str; } utils.js exports.return = function

vue搭建多页面开发环境

孤人 提交于 2019-12-05 03:14:03
vue搭建多页面开发环境 自从习惯开发了单页面应用,对多页面的页面间的相互跳转间没有过渡效果、难维护极度反感。但是最近公司技术老大说,当一个应用越来越大的时候单页面模式应付不来,但是没讲怎么应付不来,所以还得自己去复习一遍这两者的区别: 这样对比的话,单页面的优势确实很大,但当我自己去打开某宝,某东的移动端页面时,确实它们都是多页面应用。为什么?我能想到的就几点: 1.单页面使用的技术对低版本的浏览器不友好,大公司还得兼顾使用低版本浏览器的用户啊 2.功能模块开发来说,比如说单页面的业务公用组件,有时候你都不知道分给谁开发 3.seo优化吧(PS:既然是大应用应该很多人都知道,为什么还要做搜索引擎优化) --------------------------------------------------华丽分割线------------------------------------------------------------------------------------ 公司开发移动端使用的技术是vue,其实老大在要求使用多页面开发的时候,已经搭了一个vue多页面的脚手架供给我们去使用,但是我去看了看源码的时候写得很一般,所以决定自己重新去写过。 思路: 由于vue-cli已经写好了单页面的webpack文件,不去改动之前是它默认的一个页面引用打包的资源。既然是多页面

前端模块化规范

邮差的信 提交于 2019-12-05 02:55:27
一、js模块化 命名空间 commonJS AMD/CMD/UMD ES6 module 二、命名空间 库名.类别名.方法名 var NameSpace = {} NameSpace.type = NameSpace.type || {} NameSpace.type.method = function () { } 三、commonJS规范 一个文件为一个模块,通过module.export暴露快接口,通过require引入模块,同步执行 commonJS 文档 示例: const Router = require('./router/route') export = module.exports = createApplication; 四、AMD规范 Async Module Definition 使用define定义模块 使用require加载模块 RequireJS 依赖前置,提前执行 AMD规范文档 示例: define( // 模块名字 "alpha", // 模块输出 ["require", "exports", "beta"], // 模块输出 function (require, exports, beta) { exports.verb = function () { return beta.verb(); return require("beta")

koa2的学习

China☆狼群 提交于 2019-12-05 02:39:49
安装koa的以及koa-router及全局的glob(可使用*号遍历所有文件) npm install koa --save npm install glob --save npm install koa-router --savenpm install koa2-cors --save 引入koa const Koa = require('koa') const app = new Koa() const Router=require('koa-router') const mongoose = require("mongoose") const {connect,initSchemas} = require("./database/init") const cors=require("koa2-cors")//跨域需求 app.use(cors())//注意位置 let entry = require("./appApi/entry.js") let router = new Router(); router.use('/entry',entry.routes())//路由配置项 app.use(router.routes()) //注意 app.use(router.allowedMethods())//注意 ;(async()=>{ await connect()//连接

使用SeaJS实现模块化JavaScript开发

限于喜欢 提交于 2019-12-05 02:26:50
前言 SeaJS 是一个遵循 CommonJS 规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制。与 jQuery 等JavaScript框架不同,SeaJS不会扩展封装语言特性,而只是实现JavaScript的模块化及按模块加载。SeaJS的主要目的是令JavaScript开发模块化并可以轻松愉悦进行加载,将前端工程师从繁重的JavaScript文件及对象依赖处理中解放出来,可以专注于代码本身的逻辑。SeaJS可以与jQuery这类框架完美集成。使用SeaJS可以提高JavaScript代码的可读性和清晰度,解决目前JavaScript编程中普遍存在的依赖关系混乱和代码纠缠等问题,方便代码的编写和维护。 SeaJS的作者是淘宝前端工程师 玉伯 。 SeaJS本身遵循KISS(Keep It Simple, Stupid)理念进行开发,其本身仅有个位数的API,因此学习起来毫无压力。在学习SeaJS的过程中,处处能感受到KISS原则的精髓——仅做一件事,做好一件事。 本文首先通过一个例子直观对比传统JavaScript编程和使用SeaJS的模块化JavaScript编程,然后详细讨论SeaJS的使用方法,最后给出一些与SeaJS相关的资料。 传统模式 vs SeaJS模块化 假设我们现在正在开发一个Web应用TinyApp

使用SeaJS实现模块化JavaScript开发

半世苍凉 提交于 2019-12-05 02:26:38
前言 SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制。与jQuery等JavaScript框架不同,SeaJS不会扩展封装语言特性,而只是实现JavaScript的模块化及按模块加载。SeaJS的主要目的是令JavaScript开发模块化并可以轻松愉悦进行加载,将前端工程师从繁重的JavaScript文件及对象依赖处理中解放出来,可以专注于代码本身的逻辑。SeaJS可以与jQuery这类框架完美集成。使用SeaJS可以提高JavaScript代码的可读性和清晰度,解决目前JavaScript编程中普遍存在的依赖关系混乱和代码纠缠等问题,方便代码的编写和维护。 SeaJS的作者是淘宝前端工程师玉伯。 SeaJS本身遵循KISS(Keep It Simple, Stupid)理念进行开发,其本身仅有个位数的API,因此学习起来毫无压力。在学习SeaJS的过程中,处处能感受到KISS原则的精髓——仅做一件事,做好一件事。 本文首先通过一个例子直观对比传统JavaScript编程和使用SeaJS的模块化JavaScript编程,然后详细讨论SeaJS的使用方法,最后给出一些与SeaJS相关的资料。 传统模式 vs SeaJS模块化 假设我们现在正在开发一个Web应用TinyApp

vue根据不同环境进行编译打包

我与影子孤独终老i 提交于 2019-12-05 02:22:13
工作中我们在开发过程中,有很多的开发环境,如果我们不进行统一配置,那么我们只能手动进行更改,这样会给我们带来诸多不便,所以我们要配置根据不同的环境来进行编译打包. 先看一下我的项目目录: 在config文件内新建test.env.js文件: 1. 'use strict' module.exports = { NODE_ENV: '"testing"', ENV_CONFIG:'"test"' } 修改config内的prod.env.js文件: 2. 'use strict' module.exports = { NODE_ENV: '"production"', ENV_CONFIG:'"prod"' } 对build中webpack.prod.conf.js做如下修改: 3. const env = config.build[process.env.env_config+'Env'] config中的index.js 文件中build部分代码修改如下: 4. build: { prodEnv: require('./prod.env'), testEnv: require('./test.env'), // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // ···

上传代码

和自甴很熟 提交于 2019-12-05 02:21:00
upload const fs = require('fs'); const glob = require('glob'); const request = require('request'); var config = require('../package.json'); const formData = {}; glob('build/**', {nodir: true}, function (err, files) { files.forEach((file) => { const fullPath = `/export/www/html/${config.ftpServer}${file.replace(/^build/, '')}`; formData[fullPath] = fs.createReadStream(file); }); request.post({ url: 'http://xxxxx:3000', formData: formData }, (err, res) => { if (err) { console.log(err); } console.log(`上传文件成功`); }) }) 来源: https://www.cnblogs.com/xiaozhumaopao/p/11897399.html

How to use JS require() without Node.js

无人久伴 提交于 2019-12-05 01:54:25
This is probably a lame question, but how can you achieve the same thing as require() (Node.js) in regular JavaScript? Some help would really be appreciated. There are several services that let you do this. The most popular is Browserify . Basically, it entails going through the file reading through the syntax tree and converting it to the a style similar to what RequireJS does. Note that this requires an extra compilation step. (We will eventually get modules in ES6 though so there's that :) ) http://requirejs.org/docs/start.html A module loader called RequireJS exists for in-browser use