@spin-io/app-compiler
Advanced tools
Comparing version 0.9.0 to 0.9.2
@@ -10,73 +10,70 @@ "use strict"; | ||
}; | ||
const expect = require("expect"); | ||
const cheerio = require("cheerio"); | ||
const _1 = require("./"); | ||
describe('AppCompiler', () => { | ||
it('should compile', () => __awaiter(this, void 0, void 0, function* () { | ||
const compiler = new _1.default({ | ||
name: 'test', | ||
appProps: { | ||
test: 'hello' | ||
}, | ||
appSource: ` | ||
(function () { | ||
var e = SpinSDK.createElement; | ||
var withStyles = SpinSDK.withStyles; | ||
var css = SpinSDK.css; | ||
function createStyles(theme) { | ||
return { | ||
color: theme.primaryColor | ||
}; | ||
} | ||
function App(props) { | ||
return e('div', { id: 'test', css: props.styles }, props.test); | ||
} | ||
return { | ||
default: withStyles(createStyles)(App) | ||
}; | ||
})() | ||
`, | ||
themeProps: { | ||
primaryColor: 'blue' | ||
}, | ||
themeSource: ` | ||
(function () { | ||
var e = SpinSDK.createElement; | ||
var withTheme = SpinSDK.withTheme; | ||
var ThemeProvider = SpinSDK.ThemeProvider; | ||
function createTheme(props) { | ||
return { | ||
primaryColor: props.primaryColor | ||
}; | ||
} | ||
function Theme(props) { | ||
return e(ThemeProvider, { theme: props.theme }, props.children); | ||
} | ||
return { | ||
default: withTheme(createTheme)(Theme) | ||
}; | ||
})() | ||
`, | ||
prerender: true | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t; | ||
return { next: verb(0), "throw": verb(1), "return": verb(2) }; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var _this = this; | ||
var expect = require("expect"); | ||
var cheerio = require("cheerio"); | ||
var _1 = require("./"); | ||
describe('AppCompiler', function () { | ||
it('should compile', function () { return __awaiter(_this, void 0, void 0, function () { | ||
var compiler, compilation, html, $; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
compiler = new _1.default({ | ||
name: 'test', | ||
appProps: { | ||
test: 'hello' | ||
}, | ||
appSource: "\n (function () {\n var e = SpinSDK.createElement;\n var withStyles = SpinSDK.withStyles;\n var css = SpinSDK.css;\n\n function createStyles(theme) {\n return {\n color: theme.primaryColor\n };\n }\n\n function App(props) {\n return e('div', { id: 'test', css: props.styles }, props.test);\n }\n\n return {\n default: withStyles(createStyles)(App)\n };\n })()\n ", | ||
themeProps: { | ||
primaryColor: 'blue' | ||
}, | ||
themeSource: "\n (function () {\n var e = SpinSDK.createElement;\n var withTheme = SpinSDK.withTheme;\n var ThemeProvider = SpinSDK.ThemeProvider;\n\n function createTheme(props) {\n return {\n primaryColor: props.primaryColor\n };\n }\n\n function Theme(props) {\n return e(ThemeProvider, { theme: props.theme }, props.children);\n }\n\n return {\n default: withTheme(createTheme)(Theme)\n };\n })()\n ", | ||
prerender: true | ||
}); | ||
return [4 /*yield*/, compiler.compile()]; | ||
case 1: | ||
compilation = _a.sent(); | ||
expect(compilation.files).toBeA('array'); | ||
expect(compilation.files.length).toEqual(2); | ||
expect(compilation.files[0].name).toEqual('index.html'); | ||
expect(compilation.files[0].type).toEqual('text/html'); | ||
expect(compilation.files[0].contents).toBeA('string'); | ||
html = compilation.files[0].contents.toString(); | ||
$ = cheerio.load(html); | ||
expect($('#test').text()).toEqual('hello'); | ||
expect(compilation.files[1].name).toEqual('main.js'); | ||
expect(compilation.files[1].type).toEqual('application/javascript'); | ||
expect(compilation.files[1].contents).toBeA('string'); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
const compilation = yield compiler.compile(); | ||
expect(compilation.files).toBeA('array'); | ||
expect(compilation.files.length).toEqual(2); | ||
expect(compilation.files[0].name).toEqual('index.html'); | ||
expect(compilation.files[0].type).toEqual('text/html'); | ||
expect(compilation.files[0].contents).toBeA('string'); | ||
const html = compilation.files[0].contents.toString(); | ||
const $ = cheerio.load(html); | ||
expect($('#test').text()).toEqual('hello'); | ||
expect(compilation.files[1].name).toEqual('main.js'); | ||
expect(compilation.files[1].type).toEqual('application/javascript'); | ||
expect(compilation.files[1].contents).toBeA('string'); | ||
})); | ||
}); }); | ||
}); | ||
//# sourceMappingURL=AppCompiler.spec.js.map |
225
lib/index.js
@@ -10,25 +10,55 @@ "use strict"; | ||
}; | ||
const SpinSDK = require("@spin-io/sdk"); | ||
const SpinSDKServer = require("@spin-io/sdk/lib/server"); | ||
const safeEval = require("safe-eval"); | ||
const sdkVersion = require('@spin-io/sdk/package.json').version; | ||
const CDN_URL = 'https://cdn.spin.io'; | ||
class AppCompiler { | ||
constructor(options) { | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t; | ||
return { next: verb(0), "throw": verb(1), "return": verb(2) }; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [0, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var SpinSDK = require("@spin-io/sdk"); | ||
var SpinSDKServer = require("@spin-io/sdk/lib/server"); | ||
var safeEval = require("safe-eval"); | ||
var sdkVersion = require('@spin-io/sdk/package.json').version; | ||
var CDN_URL = 'https://cdn.spin.io'; | ||
var AppCompiler = (function () { | ||
function AppCompiler(options) { | ||
this.options = options; | ||
} | ||
compile() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const compilation = this.getCompliation(); | ||
return { | ||
files: [ | ||
{ name: 'index.html', type: 'text/html', contents: compilation.indexHtml }, | ||
{ name: 'main.js', type: 'application/javascript', contents: compilation.mainJs } | ||
] | ||
}; | ||
AppCompiler.prototype.compile = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var compilation; | ||
return __generator(this, function (_a) { | ||
compilation = this.getCompliation(); | ||
return [2 /*return*/, { | ||
files: [ | ||
{ name: 'index.html', type: 'text/html', contents: compilation.indexHtml }, | ||
{ name: 'main.js', type: 'application/javascript', contents: compilation.mainJs } | ||
] | ||
}]; | ||
}); | ||
}); | ||
} | ||
evalComponent(source) { | ||
const componentExports = { default: {} }; | ||
const evalContext = { | ||
}; | ||
AppCompiler.prototype.evalComponent = function (source) { | ||
var componentExports = { default: {} }; | ||
var evalContext = { | ||
app: { | ||
@@ -38,23 +68,23 @@ isServer: true, | ||
}, | ||
SpinSDK, | ||
componentExports, | ||
console | ||
SpinSDK: SpinSDK, | ||
componentExports: componentExports, | ||
console: console | ||
}; | ||
const component = safeEval(source, evalContext).default; | ||
var component = safeEval(source, evalContext).default; | ||
return component; | ||
} | ||
getPrender() { | ||
const appComponent = this.evalComponent(this.options.appSource); | ||
const themeComponent = this.evalComponent(this.options.themeSource); | ||
let main = (themeProps, appProps) => { | ||
}; | ||
AppCompiler.prototype.getPrender = function () { | ||
var appComponent = this.evalComponent(this.options.appSource); | ||
var themeComponent = this.evalComponent(this.options.themeSource); | ||
var main = function (themeProps, appProps) { | ||
return SpinSDK.createElement(themeComponent, themeProps, SpinSDK.createElement(appComponent, appProps)); | ||
}; | ||
if (this.options.containerComponentSource) { | ||
const containerComponent = this.evalComponent(this.options.containerComponentSource); | ||
let mainInner = main; | ||
main = (themeProps, appProps) => { | ||
return SpinSDK.createElement(containerComponent, { themeProps, appProps }, mainInner); | ||
var containerComponent_1 = this.evalComponent(this.options.containerComponentSource); | ||
var mainInner_1 = main; | ||
main = function (themeProps, appProps) { | ||
return SpinSDK.createElement(containerComponent_1, { themeProps: themeProps, appProps: appProps }, mainInner_1); | ||
}; | ||
} | ||
const prerender = SpinSDKServer.renderToString(main(this.options.themeProps, this.options.appProps)); | ||
var prerender = SpinSDKServer.renderToString(main(this.options.themeProps, this.options.appProps)); | ||
return { | ||
@@ -65,117 +95,22 @@ css: prerender.css, | ||
}; | ||
} | ||
getTemplate(prerender, debug = false) { | ||
return ` | ||
<!doctype html> | ||
<html data-timestamp="${new Date().toISOString()}"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"> | ||
<title>${debug ? '[DEBUG] ' : ''}${this.options.name}</title> | ||
<style> | ||
html { height: 100%; font-size: ${SpinSDK.rootFontSize}px; } | ||
body { height: 100%; margin: 0; } | ||
#root { height: 100%; } | ||
</style> | ||
<style> | ||
${prerender.css} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root">${prerender.html}</div> | ||
<script src="${CDN_URL}/libs/spin-sdk/${sdkVersion}/spin-sdk${debug ? '' : '.min'}.js"></script> | ||
<script src="main${debug ? '.debug' : ''}.js"></script> | ||
</body> | ||
</html> | ||
`; | ||
} | ||
getMain(prerender) { | ||
return ` | ||
var ns | ||
if (typeof global !== 'undefined') { | ||
ns = global | ||
} else if (typeof window !== 'undefined') { | ||
ns = window | ||
} | ||
var app = ns.app || { | ||
isServer: false, | ||
isClient: true | ||
}; | ||
app.render = (function() { | ||
var SpinSDK = ns.SpinSDK | ||
SpinSDK.rehydrateCss(${JSON.stringify(prerender.cssIds)}); | ||
var appProps = ${JSON.stringify(this.options.appProps)}; | ||
var appComponent = (function () { | ||
var module = ${this.options.appSource}; | ||
return module.default | ||
})(); | ||
var themeProps = ${JSON.stringify(this.options.themeProps)}; | ||
var themeComponent = (function () { | ||
var module = ${this.options.themeSource}; | ||
return module.default | ||
})(); | ||
var initialProps = { | ||
appProps: appProps, | ||
themeProps: themeProps | ||
}; | ||
var containerComponent = (function () { | ||
var module = ${this.options.containerComponentSource || '{}'}; | ||
return module.default | ||
})(); | ||
function render(props) { | ||
var main = function(themeProps, appProps) { | ||
return SpinSDK.createElement( | ||
themeComponent, | ||
themeProps, | ||
SpinSDK.createElement(appComponent, appProps) | ||
) | ||
} | ||
if (containerComponent) { | ||
var mainInner = main; | ||
main = function(themeProps, appProps) { | ||
return SpinSDK.createElement( | ||
containerComponent, | ||
{ | ||
themeProps: themeProps, | ||
appProps: appProps | ||
}, | ||
mainInner | ||
); | ||
} | ||
} | ||
SpinSDK.render( | ||
main(props.themeProps, props.appProps), | ||
document.getElementById('root') | ||
); | ||
} | ||
render(initialProps) | ||
return render | ||
})() | ||
`; | ||
} | ||
getCompliation() { | ||
const prerender = this.options.prerender ? | ||
}; | ||
AppCompiler.prototype.getTemplate = function (prerender, debug) { | ||
if (debug === void 0) { debug = false; } | ||
return "\n <!doctype html>\n <html data-timestamp=\"" + new Date().toISOString() + "\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1\">\n <title>" + (debug ? '[DEBUG] ' : '') + this.options.name + "</title>\n <style>\n html { height: 100%; font-size: " + SpinSDK.rootFontSize + "px; }\n body { height: 100%; margin: 0; }\n #root { height: 100%; }\n </style>\n <style>\n " + prerender.css + "\n </style>\n </head>\n <body>\n <div id=\"root\">" + prerender.html + "</div>\n <script src=\"" + CDN_URL + "/libs/spin-sdk/" + sdkVersion + "/spin-sdk" + (debug ? '' : '.min') + ".js\"></script>\n <script src=\"main" + (debug ? '.debug' : '') + ".js\"></script>\n </body>\n </html>\n "; | ||
}; | ||
AppCompiler.prototype.getMain = function (prerender) { | ||
return "\n var ns\n\n if (typeof global !== 'undefined') {\n ns = global\n } else if (typeof window !== 'undefined') {\n ns = window\n }\n\n var app = ns.app || {\n isServer: false,\n isClient: true\n };\n\n app.render = (function() {\n var SpinSDK = ns.SpinSDK\n\n SpinSDK.rehydrateCss(" + JSON.stringify(prerender.cssIds) + ");\n\n var appProps = " + JSON.stringify(this.options.appProps) + ";\n var appComponent = (function () {\n var module = " + this.options.appSource + ";\n return module.default\n })();\n\n var themeProps = " + JSON.stringify(this.options.themeProps) + ";\n var themeComponent = (function () {\n var module = " + this.options.themeSource + ";\n return module.default\n })();\n\n var initialProps = {\n appProps: appProps,\n themeProps: themeProps\n };\n\n var containerComponent = (function () {\n var module = " + (this.options.containerComponentSource || '{}') + ";\n return module.default\n })();\n\n function render(props) {\n var main = function(themeProps, appProps) {\n return SpinSDK.createElement(\n themeComponent,\n themeProps,\n SpinSDK.createElement(appComponent, appProps)\n )\n }\n\n if (containerComponent) {\n var mainInner = main;\n\n main = function(themeProps, appProps) {\n return SpinSDK.createElement(\n containerComponent,\n {\n themeProps: themeProps,\n appProps: appProps\n },\n mainInner\n );\n }\n }\n\n SpinSDK.render(\n main(props.themeProps, props.appProps),\n document.getElementById('root')\n );\n }\n\n render(initialProps)\n\n return render\n })()\n "; | ||
}; | ||
AppCompiler.prototype.getCompliation = function () { | ||
var prerender = this.options.prerender ? | ||
this.getPrender() : | ||
{ html: '', css: '', cssIds: {} }; | ||
const indexHtml = this.getTemplate(prerender); | ||
const mainJs = this.getMain(prerender); | ||
return { indexHtml, mainJs }; | ||
} | ||
} | ||
var indexHtml = this.getTemplate(prerender); | ||
var mainJs = this.getMain(prerender); | ||
return { indexHtml: indexHtml, mainJs: mainJs }; | ||
}; | ||
return AppCompiler; | ||
}()); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = AppCompiler; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@spin-io/app-compiler", | ||
"version": "0.9.0", | ||
"version": "0.9.2", | ||
"main": "./lib/index.js", | ||
@@ -10,6 +10,6 @@ "typings": "./lib/index.d.ts", | ||
"test": "mocha -r ts-node/register -S src/**/*.spec.ts", | ||
"prepublish": "npm run build:lib" | ||
"prepublish": "npm run test && npm run build:lib" | ||
}, | ||
"dependencies": { | ||
"@spin-io/sdk": "^0.8.1", | ||
"@spin-io/sdk": "^0.9.3", | ||
"bluebird": "^3.4.6", | ||
@@ -16,0 +16,0 @@ "memory-fs": "^0.3.0", |
{ | ||
"compilerOptions": { | ||
"target": "es2015", | ||
"target": "es5", | ||
"module": "commonjs", | ||
"lib": ["es6", "dom"], | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"declaration": true, | ||
"noImplicitAny": false, | ||
"outDir": "lib", | ||
"allowSyntheticDefaultImports": true, | ||
"removeComments": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"strictNullChecks": true | ||
"experimentalDecorators": true, | ||
"pretty": true, | ||
"removeComments": true | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
"files": [ | ||
"node_modules/typescript/lib/lib.es2015.d.ts", | ||
"node_modules/typescript/lib/lib.dom.d.ts", | ||
"typings.d.ts", | ||
"src/index.ts", | ||
"src/AppCompiler.spec.ts" | ||
], | ||
@@ -17,0 +23,0 @@ "exclude": [ |
declare module 'webpack' | ||
declare module 'safe-eval' | ||
// ts fails to compile if this is removed | ||
// not sure how to avoid duplicating this with sdk | ||
declare module 'react-redux-theme-provider' { | ||
var actions: any | ||
var constants: any | ||
var reducer: any | ||
var ThemeProvider: any | ||
var withTheme: any | ||
var withStyles: any | ||
var connectTheme: any | ||
} | ||
declare module 'safe-eval' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126762
536
+ Addedgopd@1.0.1(transitive)
- Removedgopd@1.1.0(transitive)
Updated@spin-io/sdk@^0.9.3