Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-tsx

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-tsx - npm Package Compare versions

Comparing version 2.0.2 to 2.0.3

lib/App.js

95

lib/index.js
"use strict";
var React = require("react");
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const React = require("react");
global.React = React;
var ReactDOM = require("react-dom/server");
const path = require("path");
const ReactDOM = require("react-dom/server");
const ts = require("typescript");
class Options {
constructor() {
/**模板热更新 */
this.hotload = false;
/**文档类型 */
this.doctype = '<!DOCTYPE html>';
/**服务器同构渲染 */
this.ssr = false;
/**编译选项 */
this.compilerOptions = {
allowJs: true,
module: ts.ModuleKind.AMD,
target: ts.ScriptTarget.ES5,
jsx: ts.JsxEmit.React,
outFile: 'bundle.js',
inlineSourceMap: true,
inlineSources: true,
};
this.ssrRender = (Render, data, filepath, compile) => __awaiter(this, void 0, void 0, function* () {
let app = React.createElement(Render, data);
let appModuleName = path.basename(filepath, path.extname(filepath));
let appDefineScript = `<script>${yield compile(filepath)}</script>`;
return App_1.WrapApp(ReactDOM.renderToString(app), data, [
appDefineScript,
App_1.BowserRender(appModuleName, data),
]);
});
}
}
exports.Options = Options;
/**默认配置 */
exports.option = {
hotload: false,
renderMethod: 'renderToStaticMarkup'
};
exports.render = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.renderMethod, renderMethod = _c === void 0 ? exports.option.renderMethod : _c, _d = _b.hotload, hotload = _d === void 0 ? exports.option.hotload : _d;
return function (filepath, options, cb) {
filepath = require.resolve(filepath);
if (hotload) {
delete require.cache[filepath];
}
var exports = require(filepath);
var Render = exports && exports.default || exports;
var jsx = React.createElement(Render, options);
var html = ReactDOM[renderMethod](jsx);
cb(null, html);
exports.defaultOptions = new Options();
const configExtend = require("config-extend");
exports.getCompile = (compilerOptions) => (file) => new Promise((resolve, reject) => {
ts.createProgram([file], compilerOptions).emit(undefined, (outFile, outputText) => {
resolve(outputText);
});
});
const App_1 = require("./App");
function render(options = exports.defaultOptions) {
const { hotload, doctype, ssr, compilerOptions, ssrRender } = configExtend({}, exports.defaultOptions, options);
let compile = exports.getCompile(compilerOptions);
return function (filepath, data, cb) {
return __awaiter(this, void 0, void 0, function* () {
filepath = require.resolve(filepath);
if (hotload) {
delete require.cache[filepath]; //粗暴地实现了热载
}
let exports = require(filepath);
let Render = exports && exports.default || exports;
let html = doctype;
if (ssr) {
html += yield ssrRender(Render, data, filepath, compile);
}
else {
html += ReactDOM.renderToStaticMarkup(React.createElement(Render, data));
}
cb(null, html);
});
};
};
exports.__express = exports.render;
}
exports.render = render;
exports.__express = render;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = exports.render;
exports.default = render;
//# sourceMappingURL=index.js.map
{
"name": "express-tsx",
"version": "2.0.2",
"version": "2.0.3",
"description": "express view tsx render",
"main": "lib/index.js",
"dependencies": {
"config-extend": "^0.1.1"
},
"peerDependencies": {
"react": "^15.4.2",
"express": "^4.15.2",
"typescript": "^2.2.2",
"react-dom": "^15.4.2"

@@ -15,3 +20,8 @@ },

"@types/react-dom": "^0.14.23",
"@types/requirejs": "^2.1.29",
"chokidar": "^1.6.1",
"express": "^4.15.2",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"require-dynamic-exec": "^1.1.4",
"ts-node": "^3.0.2",

@@ -32,2 +42,3 @@ "typescript": "^2.2.2"

"tsx",
"jsx",
"render",

@@ -34,0 +45,0 @@ "view",

@@ -1,31 +0,70 @@

import *as React from 'react';
import React = require('react');
(global as any).React = React
declare global {
var React:any
}
import *as ReactDOM from 'react-dom/server';
export type option = {
declare global { var React:any }
import path = require('path')
import ReactDOM = require('react-dom/server');
import ts = require('typescript')
export class Options {
/**模板热更新 */
hotload?:boolean
/**服务器渲染函数 */
renderMethod?:'renderToString'|'renderToStaticMarkup'
hotload?:boolean = false
/**文档类型 */
doctype?:string = '<!DOCTYPE html>'
/**服务器同构渲染 */
ssr?:boolean = false
/**编译选项 */
compilerOptions?:ts.CompilerOptions = {
allowJs:true,
module:ts.ModuleKind.AMD,
target:ts.ScriptTarget.ES5,
jsx:ts.JsxEmit.React,
outFile:'bundle.js',
inlineSourceMap:true,
inlineSources:true,
}
ssrRender ?= async(Render,data,filepath:string,compile:(filepath:string)=>Promise<string>)=>{
let app = React.createElement(Render,data)
let appModuleName = path.basename(filepath,path.extname(filepath))
let appDefineScript = `<script>${await compile(filepath)}</script>`
return WrapApp(ReactDOM.renderToString(app),data,[
appDefineScript,
BowserRender(appModuleName,data),
])
}
}
/**默认配置 */
export let option:option = {
hotload : false,
renderMethod : 'renderToStaticMarkup'
}
export let render = ({ renderMethod=option.renderMethod, hotload=option.hotload, }:option = {})=>
(filepath:string,options:any,cb)=>{
filepath = require.resolve(filepath)
if( hotload ){
delete require.cache[filepath]
export let defaultOptions:Options = new Options()
import configExtend = require("config-extend")
export const getCompile = (compilerOptions:ts.CompilerOptions)=>(file:string):Promise<string>=>
new Promise((resolve,reject)=>{
ts.createProgram([file],compilerOptions).emit(undefined,(outFile,outputText)=>{
resolve(outputText)
})
})
import { BowserRender, WrapApp } from './App'
export function render(options:Options=defaultOptions){
const { hotload, doctype, ssr, compilerOptions, ssrRender }:Options = configExtend({},defaultOptions,options)
let compile = getCompile(compilerOptions)
return async function(filepath:string, data:any, cb){
filepath = require.resolve(filepath)
if( hotload ){
delete require.cache[filepath] //粗暴地实现了热载
}
let exports = require(filepath)
let Render = exports && exports.default || exports
let html:string = doctype
if(ssr){
html += await ssrRender(Render,data,filepath,compile)
}else{
html += ReactDOM.renderToStaticMarkup(
React.createElement(Render,data)
)
}
cb(null,html)
}
let exports = require(filepath)
let Render = exports && exports.default || exports
let jsx = React.createElement(Render,options)
let html = ReactDOM[renderMethod](jsx)
cb(null,html)
}
export let __express = render
export default render

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc