@midwayjs/view 3.0.2 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/view",
3
- "version": "3.0.2",
3
+ "version": "3.0.5",
4
4
  "description": "Midway Component for render view",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -20,18 +20,17 @@
20
20
  "files": [
21
21
  "dist/**/*.js",
22
22
  "dist/**/*.d.ts",
23
- "index.d.ts",
24
- "dist/**/*.js.map"
23
+ "index.d.ts"
25
24
  ],
26
25
  "engines": {
27
26
  "node": ">=12"
28
27
  },
29
28
  "license": "MIT",
30
29
  "devDependencies": {
31
- "@midwayjs/core": "^3.0.2",
32
- "@midwayjs/decorator": "^3.0.2",
33
- "@midwayjs/koa": "^3.0.2",
34
- "@midwayjs/mock": "^3.0.2"
30
+ "@midwayjs/core": "^3.0.4",
31
+ "@midwayjs/decorator": "^3.0.4",
32
+ "@midwayjs/koa": "^3.0.5",
33
+ "@midwayjs/mock": "^3.0.5"
35
34
  },
36
- "gitHead": "ca77247d229978a736e79bb208579c014ed226fc"
35
+ "gitHead": "9b17898f10faeef2e7f1011eaea67f1d0b085261"
37
36
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.default.js","sourceRoot":"","sources":["../../src/config/config.default.ts"],"names":[],"mappings":";;AAAA,+BAA4B;AAE5B,kBAAe,OAAO,CAAC,EAAE,CAAC,CAAC;IACzB;;;;;;;;OAQG;IACH,IAAI,EAAE;QACJ,KAAK,EAAE,IAAI;QACX,gBAAgB,EAAE,OAAO;QACzB,iBAAiB,EAAE,EAAE;QACrB,OAAO,EAAE,EAAE;QACX,OAAO,EAAE;YACP,OAAO,EAAE,IAAA,WAAI,EAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC;SACtC;KACF;CACF,CAAC,CAAC","sourcesContent":["import { join } from 'path';\n\nexport default appInfo => ({\n /**\n * view default config\n * @member Config#view\n * @property {String} [rootDir=${appDir}/view] - give a path to find the file, object.values() got array and find for view file\n * @property {Boolean} [cache=true] - whether cache the file's path\n * @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`\n * @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.\n * @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`\n */\n view: {\n cache: true,\n defaultExtension: '.html',\n defaultViewEngine: '',\n mapping: {},\n rootDir: {\n default: join(appInfo.appDir, 'view'),\n },\n },\n});\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.local.js","sourceRoot":"","sources":["../../src/config/config.local.ts"],"names":[],"mappings":";;;AAAa,QAAA,IAAI,GAAG;IAClB,KAAK,EAAE,KAAK;CACb,CAAC","sourcesContent":["export const view = {\n cache: false,\n};\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"configuration.js","sourceRoot":"","sources":["../src/configuration.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAA4D;AAC5D,yDAAyD;AACzD,qDAAqD;AACrD,+CAA4C;AAC5C,yCAA0D;AAC1D,+CAA4C;AAW5C,IAAa,iBAAiB,GAA9B,MAAa,iBAAiB;IAI5B,KAAK,CAAC,OAAO,CAAC,SAAS;QACrB,IAAI,CAAC,kBAAkB;aACpB,eAAe,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;aACvC,OAAO,CAAC,CAAC,GAAQ,EAAE,EAAE;YACpB,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,OAAO,EAAE;gBACnC;;;mBAGG;gBACH,MAAM,EAAE;oBACN,KAAK,EAAE,KAAK,WAAW,GAAG,IAAI;wBAC5B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,QAAQ,CACpD,yBAAW,CACZ,CAAC;wBACF,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;4BAC7C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;wBACnB,CAAC,CAAC,CAAC;oBACL,CAAC;iBACF;gBAED;;;mBAGG;gBACH,UAAU,EAAE;oBACV,KAAK,EAAE,KAAK,WAAW,GAAG,IAAI;wBAC5B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,QAAQ,CACpD,yBAAW,CACZ,CAAC;wBACF,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;oBACrC,CAAC;iBACF;gBAED;;;mBAGG;gBACH,YAAY,EAAE;oBACZ,KAAK,EAAE,KAAK,WAAW,GAAG,IAAI;wBAC5B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,QAAQ,CACpD,yBAAW,CACZ,CAAC;wBACF,OAAO,WAAW,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC;oBAC3C,CAAC;iBACF;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEL,MAAM,SAAS,CAAC,QAAQ,CAAC,yBAAW,CAAC,CAAC;IACxC,CAAC;CACF,CAAA;AApDC;IADC,IAAA,kBAAM,GAAE;8BACW,+BAAwB;6DAAC;AAFlC,iBAAiB;IAT7B,IAAA,yBAAa,EAAC;QACb,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE;YACb;gBACE,OAAO,EAAE,aAAa;gBACtB,KAAK,EAAE,WAAW;aACnB;SACF;KACF,CAAC;GACW,iBAAiB,CAsD7B;AAtDY,8CAAiB","sourcesContent":["import { Inject, Configuration } from '@midwayjs/decorator';\nimport * as DefaultConfig from './config/config.default';\nimport * as LocalConfig from './config/config.local';\nimport { ViewManager } from './viewManager';\nimport { MidwayApplicationManager } from '@midwayjs/core';\nimport { ContextView } from './contextView';\n\n@Configuration({\n namespace: 'view',\n importConfigs: [\n {\n default: DefaultConfig,\n local: LocalConfig,\n },\n ],\n})\nexport class ViewConfiguration {\n @Inject()\n applicationManager: MidwayApplicationManager;\n\n async onReady(container) {\n this.applicationManager\n .getApplications(['koa', 'egg', 'faas'])\n .forEach((app: any) => {\n Object.defineProperties(app.context, {\n /**\n * Render a file, then set to body, the parameter is same as {@link @ContextView#render}\n * @return {Promise} result\n */\n render: {\n value: async function (...args) {\n const contextView = await this.requestContext.getAsync(\n ContextView\n );\n return contextView.render(...args).then(body => {\n this.body = body;\n });\n },\n },\n\n /**\n * Render a file, same as {@link @ContextView#render}\n * @return {Promise} result\n */\n renderView: {\n value: async function (...args) {\n const contextView = await this.requestContext.getAsync(\n ContextView\n );\n return contextView.render(...args);\n },\n },\n\n /**\n * Render template string, same as {@link @ContextView#renderString}\n * @return {Promise} result\n */\n renderString: {\n value: async function (...args) {\n const contextView = await this.requestContext.getAsync(\n ContextView\n );\n return contextView.renderString(...args);\n },\n },\n });\n });\n\n await container.getAsync(ViewManager);\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"contextView.js","sourceRoot":"","sources":["../src/contextView.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,+CAA4C;AAC5C,mDAAqE;AACrE,iCAAiC;AACjC,+BAA0C;AAG1C;;;;;GAKG;AAEH,IAAa,WAAW,GAAxB,MAAa,WAAW;IAUtB,KAAK,CAAC,MAAM,CACV,IAAY,EACZ,MAA4B,EAC5B,OAAuB;QAEvB,qDAAqD;QACrD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACtD,OAAO,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;QACxB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QACpB,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAA,gBAAS,EAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAC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ClB,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC9C,MAAM,CAAC,UAAU,EAAE,0BAA0B,IAAI,GAAG,CAAC,CAAC;QAEtD,4BAA4B;QAC5B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QAClE,qFAAqF;QACrF,IAAI,MAAM,CAAC,MAAM,EAAE;YACjB,MAAM,CAAC,MAAM,GAAG,iBAAK,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;SACtD;QACD,IAAI,MAAM,CAAC,YAAY,EAAE;YACvB,MAAM,CAAC,YAAY,GAAG,iBAAK,CAAC,eAAe,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;SAClE;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,SAAS,CAAC,MAAM;QACtB,OAAO,MAAM,CAAC,MAAM,CAClB;YACE,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,OAAO;SAC1B,EACD,IAAI,CAAC,GAAG,CAAC,MAAM,EACf,MAAM,CACP,CAAC;IACJ,CAAC;CACF,CAAA;AAhFC;IADC,IAAA,kBAAM,GAAE;8BACI,yBAAW;gDAAC;AAGzB;IADC,IAAA,kBAAM,EAAC,MAAM,CAAC;;+CACJ;AAGX;IADC,IAAA,kBAAM,GAAE;;wCACL;AARO,WAAW;IADvB,IAAA,mBAAO,GAAE;GACG,WAAW,CAkFvB;AAlFY,kCAAW","sourcesContent":["import { ViewManager } from './viewManager';\nimport { Provide, Inject, Config, Utils } from '@midwayjs/decorator';\nimport * as assert from 'assert';\nimport { normalize, extname } from 'path';\nimport { IViewEngine, RenderOptions } from './interface';\n\n/**\n * View instance for each request.\n *\n * It will find the view engine, and render it.\n * The view engine should be registered in {@link ViewManager}.\n */\n@Provide()\nexport class ContextView implements IViewEngine {\n @Inject()\n viewManager: ViewManager;\n\n @Config('view')\n viewConfig;\n\n @Inject()\n ctx;\n\n async render(\n name: string,\n locals?: Record<string, any>,\n options?: RenderOptions\n ): Promise<string> {\n // retrieve fullpath matching name from `config.root`\n const filename = await this.viewManager.resolve(name);\n options = options ?? {};\n options.name = name;\n options.root = filename.replace(normalize(name), '').replace(/[/\\\\]$/, '');\n options.locals = locals;\n\n // get the name of view engine,\n // if viewEngine is specified in options, don't match extension\n let viewEngineName = options.viewEngine;\n if (!viewEngineName) {\n const ext = extname(filename);\n viewEngineName = this.viewManager.extMap.get(ext);\n }\n // use the default view engine that is configured if no matching above\n if (!viewEngineName) {\n viewEngineName = this.viewConfig.defaultViewEngine;\n }\n assert(viewEngineName, `Can't find viewEngine for ${filename}`);\n\n // get view engine and render\n const view = await this.getViewEngine(viewEngineName);\n return await view.render(filename, this.setLocals(locals), options);\n }\n\n async renderString(\n tpl: string,\n locals?: Record<string, any>,\n options?: RenderOptions\n ): Promise<string> {\n options = options ?? {};\n const viewEngineName =\n options.viewEngine ?? this.viewConfig.defaultViewEngine;\n assert(viewEngineName, \"Can't find viewEngine\");\n\n // get view engine and render\n const view = await this.getViewEngine(viewEngineName);\n return await view.renderString(tpl, this.setLocals(locals), options);\n }\n\n private async getViewEngine(name) {\n // get view engine\n const ViewEngine = this.viewManager.get(name);\n assert(ViewEngine, `Can't find ViewEngine \"${name}\"`);\n\n // use view engine to render\n const engine = await this.ctx.requestContext.getAsync(ViewEngine);\n // wrap render and renderString to support both async function and generator function\n if (engine.render) {\n engine.render = Utils.toAsyncFunction(engine.render);\n }\n if (engine.renderString) {\n engine.renderString = Utils.toAsyncFunction(engine.renderString);\n }\n return engine;\n }\n\n private setLocals(locals) {\n return Object.assign(\n {\n ctx: this.ctx,\n request: this.ctx.request,\n },\n this.ctx.locals,\n locals\n );\n }\n}\n"]}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,iDAAqE;AAA5D,8GAAA,iBAAiB,OAAiB;AAC3C,8CAA4B;AAC5B,gDAA8B;AAC9B,gDAA8B","sourcesContent":["export { ViewConfiguration as Configuration } from './configuration';\nexport * from './interface';\nexport * from './contextView';\nexport * from './viewManager';\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"interface.js","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"","sourcesContent":["export interface RenderOptions {\n name?: string;\n root?: string;\n locals?: Record<string, any>;\n viewEngine?: string;\n}\n\nexport interface IViewEngine {\n /**\n * Render a file by view engine, then set to body\n * @param {String} name - the file path based on root\n * @param {Object} [locals] - data used by template\n * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine\n * @return {Promise<String>} result - return a promise with a render result\n */\n render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;\n\n /**\n * Render a template string by view engine\n * @param {String} tpl - template string\n * @param {Object} [locals] - data used by template\n * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine\n * @return {Promise<String>} result - return a promise with a render result\n */\n renderString(\n tpl: string,\n locals?: Record<string, any>,\n options?: RenderOptions\n ): Promise<string>;\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"viewManager.js","sourceRoot":"","sources":["../src/viewManager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAO6B;AAC7B,iCAAiC;AACjC,6BAA6B;AAC7B,2BAAqD;AAKrD,IAAa,WAAW,GAAxB,MAAa,WAAY,SAAQ,GAAG;IAApC;;QASE,WAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACnB,YAAO,GAAG,IAAI,GAAG,EAAE,CAAC;IA8EtB,CAAC;IA3EC,IAAI;QACF,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC;QAC9B,MAAM,OAAO,GAAgB,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC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,EAAE,cAAS,CAAC,IAAI,CAAC,CAAC;gBAChD,IAAI,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,EAAE;oBACzB,OAAO,QAAQ,CAAC;iBACjB;aACF;YAAC,OAAO,GAAG,EAAE;gBACZ,SAAS;aACV;SACF;KACF;AACH,CAAC;AAED,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG;IACzB,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {\n App,\n Config,\n Init,\n Provide,\n Scope,\n ScopeEnum,\n} from '@midwayjs/decorator';\nimport * as assert from 'assert';\nimport * as path from 'path';\nimport { constants, existsSync, promises } from 'fs';\nimport { IViewEngine } from './interface';\n\n@Provide()\n@Scope(ScopeEnum.Singleton)\nexport class ViewManager extends Map {\n @App()\n app;\n\n @Config('view')\n viewConfig;\n\n config;\n\n extMap = new Map();\n fileMap = new Map();\n\n @Init()\n init() {\n this.config = this.viewConfig;\n const rootSet: Set<string> = new Set(Object.values(this.config.rootDir));\n if (this.config.root) {\n this.config.root.split(/\\s*,\\s*/g).forEach(filepath => {\n rootSet.add(filepath);\n });\n }\n\n this.config.root = Array.from(rootSet.values()).filter(filepath => {\n return existsSync(filepath);\n });\n this.extMap = new Map();\n this.fileMap = new Map();\n for (const ext of Object.keys(this.config.mapping)) {\n this.extMap.set(ext, this.config.mapping[ext]);\n }\n }\n\n /**\n * This method can register view engine.\n *\n * You can define a view engine class contains two method, `render` and `renderString`\n *\n * ```js\n * class View {\n * render() {}\n * renderString() {}\n * }\n * ```\n * @param {String} name - the name of view engine\n * @param {Object} viewEngine - the class of view engine\n */\n use(name: string, viewEngine: new (...args) => IViewEngine): void {\n assert(name, 'name is required');\n assert(!this.has(name), `${name} has been registered`);\n\n assert(viewEngine, 'viewEngine is required');\n assert(\n viewEngine.prototype.render,\n 'viewEngine should implement `render` method'\n );\n assert(\n viewEngine.prototype.renderString,\n 'viewEngine should implement `renderString` method'\n );\n\n this.set(name, viewEngine);\n }\n\n /**\n * Resolve the path based on the given name,\n * if the name is `user.html` and root is `view` (by default),\n * it will return `view/user.html`\n * @param {String} name - the given path name, it's relative to config.root\n * @return {String} filename - the full path\n */\n async resolve(name: string): Promise<string> {\n const config = this.config;\n\n // check cache\n let filename = this.fileMap.get(name);\n if (config.cache && filename) return filename;\n\n // try find it with default extension\n filename = await resolvePath(\n [name, name + config.defaultExtension],\n config.root\n );\n assert(filename, `Can't find ${name} from ${config.root.join(',')}`);\n\n // set cache\n this.fileMap.set(name, filename);\n return filename;\n }\n}\n\nasync function resolvePath(names, root) {\n for (const name of names) {\n for (const dir of root) {\n const filename = path.join(dir, name);\n try {\n await promises.access(filename, constants.R_OK);\n if (inpath(dir, filename)) {\n return filename;\n }\n } catch (err) {\n // ignore\n }\n }\n }\n}\n\nfunction inpath(parent, sub) {\n return sub.indexOf(parent) > -1;\n}\n"]}