This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+14
View File
@@ -0,0 +1,14 @@
/**
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare module '@ioc:Adonis/Core/Application' {
import { ViewContract } from '@ioc:Adonis/Core/View';
interface ContainerBindings {
'Adonis/Core/View': ViewContract;
}
}
+8
View File
@@ -0,0 +1,8 @@
/**
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+25
View File
@@ -0,0 +1,25 @@
/**
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Decorate context
*/
declare module '@ioc:Adonis/Core/HttpContext' {
import { ViewRendererContract } from '@ioc:Adonis/Core/View';
interface HttpContextContract {
view: ViewRendererContract;
}
}
/**
* Decorate router
*/
declare module '@ioc:Adonis/Core/Route' {
interface BriskRouteContract {
render: (template: string, data?: any) => Exclude<this['route'], null>;
}
}
+8
View File
@@ -0,0 +1,8 @@
/**
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+3
View File
@@ -0,0 +1,3 @@
/// <reference path="view.d.ts" />
/// <reference path="context.d.ts" />
/// <reference path="container.d.ts" />
+11
View File
@@ -0,0 +1,11 @@
/*
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="./view.ts" />
/// <reference path="./context.ts" />
/// <reference path="./container.ts" />
+10
View File
@@ -0,0 +1,10 @@
declare module '@ioc:Adonis/Core/View' {
import { EdgeContract, EdgeRendererContract } from 'edge.js';
export interface ViewContract extends EdgeContract {
}
export interface ViewRendererContract extends EdgeRendererContract {
}
export { TagContract, ParserContract, EdgeBufferContract, TagTokenContract, TemplateConstructorContract, } from 'edge.js';
const View: ViewContract;
export default View;
}
+8
View File
@@ -0,0 +1,8 @@
/*
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+53
View File
@@ -0,0 +1,53 @@
/// <reference types="@adonisjs/application/build/adonis-typings" />
import type { ApplicationContract } from '@ioc:Adonis/Core/Application';
/**
* View provider to register view to the application
*/
export default class ViewProvider {
protected app: ApplicationContract;
constructor(app: ApplicationContract);
/**
* Add globals for resolving routes
*/
private addRouteGlobal;
/**
* Share application reference, a config and env variable with the
* templates.
*/
private addGlobals;
/**
* Copy globals exposed by Edge
*/
private copyEdgeGlobals;
/**
* Registering the brisk route to render view directly from the route.
*/
private registerBriskRoute;
/**
* Registering the http context getter to access an isolated
* view instance with the request and route.
*/
private registerHTTPContextGetter;
/**
* Decide whether or not to cache views. If a user opts to remove
* the valdation, then `CACHE_VIEWS` will be a string and not
* a boolean, so we need to handle that case
*/
private shouldCacheViews;
/**
* Register repl binding
*/
private defineReplBindings;
/**
* Define assets manager bindings
*/
private defineAssetsManagerBindings;
/**
* Register view binding
*/
register(): void;
/**
* Setup view on boot
*/
boot(): void;
}
+161
View File
@@ -0,0 +1,161 @@
"use strict";
/*
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* View provider to register view to the application
*/
class ViewProvider {
constructor(app) {
this.app = app;
}
/**
* Add globals for resolving routes
*/
addRouteGlobal(View, Route) {
/**
* Adding `route` global
*/
View.global('route', (routeIdentifier, params, options) => {
return Route.makeUrl(routeIdentifier, params, options);
});
/**
* Adding `signedRoute` global
*/
View.global('signedRoute', (routeIdentifier, params, options) => {
return Route.makeSignedUrl(routeIdentifier, params, options);
});
}
/**
* Share application reference, a config and env variable with the
* templates.
*/
addGlobals(View, Application) {
const Config = Application.container.resolveBinding('Adonis/Core/Config');
const Env = Application.container.resolveBinding('Adonis/Core/Env');
const Drive = Application.container.resolveBinding('Adonis/Core/Drive');
View.global('app', Application);
View.global('config', (key, defaultValue) => Config.get(key, defaultValue));
View.global('env', (key, defaultValue) => Env.get(key, defaultValue));
View.global('driveUrl', (location, disk) => {
return disk ? Drive.use(disk).getUrl(location) : Drive.getUrl(location);
});
View.global('driveSignedUrl', (location, disk) => {
return disk ? Drive.use(disk).getSignedUrl(location) : Drive.getSignedUrl(location);
});
}
/**
* Copy globals exposed by Edge
*/
copyEdgeGlobals(View) {
const { GLOBALS } = require('edge.js');
Object.keys(GLOBALS).forEach((key) => View.global(key, GLOBALS[key]));
}
/**
* Registering the brisk route to render view directly from the route.
*/
registerBriskRoute(Route) {
Route.BriskRoute.macro('render', function renderView(template, data) {
return this.setHandler(({ view }) => {
return view.render(template, data);
}, 'render');
});
}
/**
* Registering the http context getter to access an isolated
* view instance with the request and route.
*/
registerHTTPContextGetter(HttpContext, View) {
HttpContext.getter('view', function () {
return View.share({ request: this.request });
}, true);
}
/**
* Decide whether or not to cache views. If a user opts to remove
* the valdation, then `CACHE_VIEWS` will be a string and not
* a boolean, so we need to handle that case
*/
shouldCacheViews() {
let cacheViews = this.app.container.resolveBinding('Adonis/Core/Env').get('CACHE_VIEWS');
if (typeof cacheViews === 'string') {
cacheViews = cacheViews === 'true';
}
return cacheViews;
}
/**
* Register repl binding
*/
defineReplBindings() {
if (this.app.environment !== 'repl') {
return;
}
this.app.container.withBindings(['Adonis/Addons/Repl'], (Repl) => {
const { defineReplBindings } = require('../src/Bindings/Repl');
defineReplBindings(this.app, Repl);
});
}
/**
* Define assets manager bindings
*/
defineAssetsManagerBindings(View, AssetsManager) {
const { defineAssetsManagerBindings } = require('../src/Bindings/AssetsManager');
defineAssetsManagerBindings(View, AssetsManager);
}
/**
* Register view binding
*/
register() {
this.app.container.singleton('Adonis/Core/View', () => {
const { Edge } = require('edge.js');
const { Supercharged } = require('edge-supercharged');
const cacheViews = this.shouldCacheViews();
const edge = new Edge({ cache: cacheViews });
/**
* Mount the views directory
*/
edge.mount(this.app.viewsPath());
/**
* Enable recurring mode when not caching views, so that the
* edge supercharged can re-scan components on each
* render call
*/
edge.use(new Supercharged().wire, { recurring: !cacheViews });
return edge;
});
}
/**
* Setup view on boot
*/
boot() {
const View = this.app.container.resolveBinding('Adonis/Core/View');
const Route = this.app.container.resolveBinding('Adonis/Core/Route');
const HttpContext = this.app.container.resolveBinding('Adonis/Core/HttpContext');
const AssetsManager = this.app.container.resolveBinding('Adonis/Core/AssetsManager');
/**
* Repl and Assets manager bindings
*/
this.defineReplBindings();
this.defineAssetsManagerBindings(View, AssetsManager);
/**
* Registering globals
*/
this.addRouteGlobal(View, Route);
this.addGlobals(View, this.app);
this.copyEdgeGlobals(View);
/**
* Registering the brisk route
*/
this.registerBriskRoute(Route);
/**
* Registering isolated view renderer with the HTTP context
*/
this.registerHTTPContextGetter(HttpContext, View);
}
}
exports.default = ViewProvider;
+7
View File
@@ -0,0 +1,7 @@
/// <reference types="@adonisjs/core" />
import { ViewContract } from '@ioc:Adonis/Core/View';
import { AssetsManagerContract } from '@ioc:Adonis/Core/AssetsManager';
/**
* Registers the asset manager tags and globals with the template engine
*/
export declare function defineAssetsManagerBindings(View: ViewContract, AssetsManager: AssetsManagerContract): void;
+60
View File
@@ -0,0 +1,60 @@
"use strict";
/*
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineAssetsManagerBindings = void 0;
const edge_error_1 = require("edge-error");
/**
* Registers the asset manager tags and globals with the template engine
*/
function defineAssetsManagerBindings(View, AssetsManager) {
View.global('assetsManager', AssetsManager);
View.global('asset', (filename) => AssetsManager.assetPath(filename));
View.registerTag({
tagName: 'entryPointStyles',
seekable: true,
block: false,
compile(parser, buffer, token) {
/**
* Ensure an argument is defined
*/
if (!token.properties.jsArg.trim()) {
throw new edge_error_1.EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {
filename: token.filename,
line: token.loc.start.line,
col: token.loc.start.col,
});
}
const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
const entrypointName = parser.utils.stringify(parsed);
buffer.outputExpression(`state.assetsManager.entryPointStyleTags(${entrypointName})`, token.filename, token.loc.start.line, false);
},
});
View.registerTag({
tagName: 'entryPointScripts',
seekable: true,
block: false,
compile(parser, buffer, token) {
/**
* Ensure an argument is defined
*/
if (!token.properties.jsArg.trim()) {
throw new edge_error_1.EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {
filename: token.filename,
line: token.loc.start.line,
col: token.loc.start.col,
});
}
const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
const entrypointName = parser.utils.stringify(parsed);
buffer.outputExpression(`state.assetsManager.entryPointScriptTags(${entrypointName})`, token.filename, token.loc.start.line, false);
},
});
}
exports.defineAssetsManagerBindings = defineAssetsManagerBindings;
+9
View File
@@ -0,0 +1,9 @@
/// <reference types="@adonisjs/application/build/adonis-typings" />
/// <reference types="@adonisjs/repl" />
import { ReplContract } from '@ioc:Adonis/Addons/Repl';
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
/**
* Define repl bindings. The method must be invoked when application environment
* is set to repl.
*/
export declare function defineReplBindings(app: ApplicationContract, Repl: ReplContract): void;
+24
View File
@@ -0,0 +1,24 @@
"use strict";
/*
* @adonisjs/view
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineReplBindings = void 0;
/**
* Define repl bindings. The method must be invoked when application environment
* is set to repl.
*/
function defineReplBindings(app, Repl) {
Repl.addMethod('loadView', (repl) => {
repl.server.context.View = app.container.use('Adonis/Core/View');
repl.notify(`Loaded View module. You can access it using the "${repl.colors.underline('View')}" variable`);
}, {
description: 'Load view provider and save reference to the "View" variable',
});
}
exports.defineReplBindings = defineReplBindings;