express-tsx
Advanced tools
Comparing version 2.0.2 to 2.0.3
"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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12158
8
204
5
12
1
+ Addedconfig-extend@^0.1.1
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedconfig-extend@0.1.1(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.2(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.6(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.12(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addedtypescript@2.9.2(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)
- Removedreact@^15.4.2
- Removedreact-dom@^15.4.2