Commit d2d7a220 authored by 劳工's avatar 劳工

Merge branch 'dev' into 'master'

Dev

See merge request !5
parents ca323596 e4ae62fb
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
import decamelize from 'decamelize';
import crypto from 'crypto';
import babel from '@babel/core';
/**
* Created by rockyl on 2019-11-30.
*/
const UglifyJS = require('uglify-js');
async function compile(source, debug = false) {
const {code, map} = await babel.transformAsync(source, {
presets: [
['@babel/env', {}]
],
babelrc: false,
sourceMaps: debug,
});
const result = {
code,
};
if (map) {
result.sourcemap = map.mappings;
}
return result;
}
function uglify(source){
const uglifyResult = UglifyJS.minify(source, {
});
if (!uglifyResult.error) {
return uglifyResult.code;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
function md5(source) {
const hash = crypto.createHash('md5');
return hash.update(source).digest('hex');
}
class ProcessManager {
constructor() {
this.pool = {};
this.scripts = '';
}
deal(process) {
let hash = this.put(process.script);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
let hash = this.put(subProcess.script);
if (hash) {
subProcess.script = 'link://' + hash;
}
}
}
}
put(script) {
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = script;
}
return hash;
}
}
async generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.pool = {};
const {code ,sourcemap} = await compile(scripts, true);
this.scripts = code;
}
generate() {
let scripts = this.scripts;
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
return `
(function(){
var processScripts = {};
(function(exports){
${scripts}
})(processScripts);
engine.setScriptMap(processScripts);
})();
`
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class ScriptManager {
constructor() {
this.pool = [];
}
deal(script){
this.pool.push(script);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
(function(exports){
${code}
})(exports);
engine.registerScriptDef(exports.default.id, exports.default);
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
${scripts}
})();
`;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class CustomManager {
constructor() {
this.pool = [];
}
deal(custom) {
this.pool.push(custom);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
module = {
exports: {}
};
(function(module){
${code}
})(module);
exports['${id}'] = module.exports;
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
var module;
${scripts}
for(var key in exports){
engine.registerCustomModule(key, exports[key]);
}
})();
`
}
}
/**
* Created by rockyl on 2019-11-13.
*
* 项目打包
*/
const camelcase = require('camelcase');
const replaceFields = ['page_title', 'container_id', 'version'];
const replaceFields = ['pageTitle', 'containerId'];
async function pack(project, options) {
async function pack(data, options) {
let version = Date.now() + Math.floor(Math.random() * 1000);
let data = JSON.parse(project.data);
const newTpl = await pageTemplate(data, options, version);
pageTemplate(data, data.options, version);
const newData = await packData(data, options);
return {
version,
data: newData,
tpl: newTpl,
}
}
async function packData(data, {getProcesses, getScripts, getCustoms}) {
function fillTpl(data, params) {
const {options} = data;
fillTemplate(options.newTpl, options, params);
return options.newTpl;
}
async function packData(data, {debug, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {};
newData.options = data.options;
delete newData.options.tpl;
......@@ -30,6 +240,43 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
newData.dataMapping = data.dataMapping;
newData.processes = data.processes;
/*=====START process =====*/
let processIDs = [];
findDepPidsBat(processIDs, data.processes);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
}
for (let process of data.processes) {
processManager.deal(process);
}
await processManager.generateCurrent(); //自定义过程先编译
for (let process of builtinProcesses) {
processManager.deal(process);
}
let processScriptContent = processManager.generate();
//console.log(processScriptContent);
if (!debug) {
processScriptContent = uglify(processScriptContent);
}
/*=====END process =====*/
/*=====START script =====*/
let scriptIDs = [];
for (let view of newData.views) {
traverseNode(view, (node) => {
......@@ -43,44 +290,47 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
});
}
//console.log('scriptIDs:', scriptIDs);
let scriptsContainer = newData.scripts = {};
//let scriptsContainer = newData.scripts = {};
//let scriptsCode = '';
if (scriptIDs.length > 0) {
const scripts = await getScripts(scriptIDs);
for (let scriptData of scripts) {
let {id, code} = JSON.parse(scriptData);
scriptsContainer[id] = code;
let script = JSON.parse(scriptData);
//scriptsContainer[id] = code;
scriptManager.deal(script);
}
//console.log('scripts:', scriptsContainer);
}
let processIDs = [];
findDepPidsBat(processIDs, newData.processes);
//console.log('processIDs:', processIDs);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
let scriptsContent = scriptManager.generate();
//console.log(scriptsContent);
if (!debug) {
scriptsContent = uglify(scriptsContent);
}
/*=====END script =====*/
newData.customs = [];
/*=====START custom =====*/
//newData.customs = [];
if (data.customs && data.customs.length > 0) {
newData.customs = (await getCustoms(data.customs)).map(item => {
return JSON.parse(item);
/*newData.customs = */
(await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item));
//return JSON.parse(item);
});
}
let customScriptContent = customManager.generate();
//console.log(customScriptContent);
if (!debug) {
customScriptContent = uglify(customScriptContent);
}
/*=====END custom =====*/
return JSON.stringify(newData);
return {
data: JSON.stringify(newData),
processScriptContent,
scriptsContent,
customScriptContent,
};
}
function findDepPids(list, process) {
......@@ -114,18 +364,23 @@ async function addBuiltinProcesses(list, ids, getProcesses) {
return newPids;
}
async function pageTemplate(data, options, version) {
const {tpl,} = data.options;
let newTpl = tpl;
let newOptions = Object.assign(data.options, {
function pageTemplate(tpl, options, version) {
const params = {
version,
});
delete newOptions.tpl;
};
for (let field of replaceFields) {
const key = camelcase(field);
newTpl = newTpl.replace(new RegExp(`\\$${field.toUpperCase()}\\$`, 'g'), newOptions[key]);
params[field] = options[field];
}
return newTpl;
fillTemplate(options.tpl, options, params);
}
function fillTemplate(tpl, options, params) {
for (let field in params) {
const pattern = decamelize(field).toUpperCase();
tpl = tpl.replace(new RegExp(`\\$${pattern}\\$`, 'g'), params[field]);
}
options.newTpl = tpl;
}
function traverseNode(root, callback) {
......@@ -137,5 +392,5 @@ function traverseNode(root, callback) {
}
}
export { pack };
export { fillTpl, pack };
//# sourceMappingURL=index.es.js.map
This diff is collapsed.
......@@ -2,30 +2,242 @@
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var decamelize = _interopDefault(require('decamelize'));
var crypto = _interopDefault(require('crypto'));
var babel = _interopDefault(require('@babel/core'));
/**
* Created by rockyl on 2019-11-30.
*/
const UglifyJS = require('uglify-js');
async function compile(source, debug = false) {
const {code, map} = await babel.transformAsync(source, {
presets: [
['@babel/env', {}]
],
babelrc: false,
sourceMaps: debug,
});
const result = {
code,
};
if (map) {
result.sourcemap = map.mappings;
}
return result;
}
function uglify(source){
const uglifyResult = UglifyJS.minify(source, {
});
if (!uglifyResult.error) {
return uglifyResult.code;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
function md5(source) {
const hash = crypto.createHash('md5');
return hash.update(source).digest('hex');
}
class ProcessManager {
constructor() {
this.pool = {};
this.scripts = '';
}
deal(process) {
let hash = this.put(process.script);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
let hash = this.put(subProcess.script);
if (hash) {
subProcess.script = 'link://' + hash;
}
}
}
}
put(script) {
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = script;
}
return hash;
}
}
async generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.pool = {};
const {code ,sourcemap} = await compile(scripts, true);
this.scripts = code;
}
generate() {
let scripts = this.scripts;
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
return `
(function(){
var processScripts = {};
(function(exports){
${scripts}
})(processScripts);
engine.setScriptMap(processScripts);
})();
`
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class ScriptManager {
constructor() {
this.pool = [];
}
deal(script){
this.pool.push(script);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
(function(exports){
${code}
})(exports);
engine.registerScriptDef(exports.default.id, exports.default);
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
${scripts}
})();
`;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class CustomManager {
constructor() {
this.pool = [];
}
deal(custom) {
this.pool.push(custom);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
module = {
exports: {}
};
(function(module){
${code}
})(module);
exports['${id}'] = module.exports;
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
var module;
${scripts}
for(var key in exports){
engine.registerCustomModule(key, exports[key]);
}
})();
`
}
}
/**
* Created by rockyl on 2019-11-13.
*
* 项目打包
*/
const camelcase = require('camelcase');
const replaceFields = ['page_title', 'container_id', 'version'];
const replaceFields = ['pageTitle', 'containerId'];
async function pack(project, options) {
async function pack(data, options) {
let version = Date.now() + Math.floor(Math.random() * 1000);
let data = JSON.parse(project.data);
const newTpl = await pageTemplate(data, options, version);
pageTemplate(data, data.options, version);
const newData = await packData(data, options);
return {
version,
data: newData,
tpl: newTpl,
}
}
async function packData(data, {getProcesses, getScripts, getCustoms}) {
function fillTpl(data, params) {
const {options} = data;
fillTemplate(options.newTpl, options, params);
return options.newTpl;
}
async function packData(data, {debug, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {};
newData.options = data.options;
delete newData.options.tpl;
......@@ -34,6 +246,43 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
newData.dataMapping = data.dataMapping;
newData.processes = data.processes;
/*=====START process =====*/
let processIDs = [];
findDepPidsBat(processIDs, data.processes);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
}
for (let process of data.processes) {
processManager.deal(process);
}
await processManager.generateCurrent(); //自定义过程先编译
for (let process of builtinProcesses) {
processManager.deal(process);
}
let processScriptContent = processManager.generate();
//console.log(processScriptContent);
if (!debug) {
processScriptContent = uglify(processScriptContent);
}
/*=====END process =====*/
/*=====START script =====*/
let scriptIDs = [];
for (let view of newData.views) {
traverseNode(view, (node) => {
......@@ -47,44 +296,47 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
});
}
//console.log('scriptIDs:', scriptIDs);
let scriptsContainer = newData.scripts = {};
//let scriptsContainer = newData.scripts = {};
//let scriptsCode = '';
if (scriptIDs.length > 0) {
const scripts = await getScripts(scriptIDs);
for (let scriptData of scripts) {
let {id, code} = JSON.parse(scriptData);
scriptsContainer[id] = code;
let script = JSON.parse(scriptData);
//scriptsContainer[id] = code;
scriptManager.deal(script);
}
//console.log('scripts:', scriptsContainer);
}
let processIDs = [];
findDepPidsBat(processIDs, newData.processes);
//console.log('processIDs:', processIDs);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
let scriptsContent = scriptManager.generate();
//console.log(scriptsContent);
if (!debug) {
scriptsContent = uglify(scriptsContent);
}
/*=====END script =====*/
newData.customs = [];
/*=====START custom =====*/
//newData.customs = [];
if (data.customs && data.customs.length > 0) {
newData.customs = (await getCustoms(data.customs)).map(item => {
return JSON.parse(item);
/*newData.customs = */
(await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item));
//return JSON.parse(item);
});
}
let customScriptContent = customManager.generate();
//console.log(customScriptContent);
if (!debug) {
customScriptContent = uglify(customScriptContent);
}
/*=====END custom =====*/
return JSON.stringify(newData);
return {
data: JSON.stringify(newData),
processScriptContent,
scriptsContent,
customScriptContent,
};
}
function findDepPids(list, process) {
......@@ -118,18 +370,23 @@ async function addBuiltinProcesses(list, ids, getProcesses) {
return newPids;
}
async function pageTemplate(data, options, version) {
const {tpl,} = data.options;
let newTpl = tpl;
let newOptions = Object.assign(data.options, {
function pageTemplate(tpl, options, version) {
const params = {
version,
});
delete newOptions.tpl;
};
for (let field of replaceFields) {
const key = camelcase(field);
newTpl = newTpl.replace(new RegExp(`\\$${field.toUpperCase()}\\$`, 'g'), newOptions[key]);
params[field] = options[field];
}
return newTpl;
fillTemplate(options.tpl, options, params);
}
function fillTemplate(tpl, options, params) {
for (let field in params) {
const pattern = decamelize(field).toUpperCase();
tpl = tpl.replace(new RegExp(`\\$${pattern}\\$`, 'g'), params[field]);
}
options.newTpl = tpl;
}
function traverseNode(root, callback) {
......@@ -141,5 +398,6 @@ function traverseNode(root, callback) {
}
}
exports.fillTpl = fillTpl;
exports.pack = pack;
//# sourceMappingURL=index.js.map
This diff is collapsed.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global['zeroing-pack'] = {}));
}(this, function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('decamelize'), require('crypto'), require('@babel/core')) :
typeof define === 'function' && define.amd ? define(['exports', 'decamelize', 'crypto', '@babel/core'], factory) :
(global = global || self, factory(global['zeroing-pack'] = {}, global.decamelize, global.crypto, global.babel));
}(this, function (exports, decamelize, crypto, babel) { 'use strict';
decamelize = decamelize && decamelize.hasOwnProperty('default') ? decamelize['default'] : decamelize;
crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto;
babel = babel && babel.hasOwnProperty('default') ? babel['default'] : babel;
/**
* Created by rockyl on 2019-11-30.
*/
const UglifyJS = require('uglify-js');
async function compile(source, debug = false) {
const {code, map} = await babel.transformAsync(source, {
presets: [
['@babel/env', {}]
],
babelrc: false,
sourceMaps: debug,
});
const result = {
code,
};
if (map) {
result.sourcemap = map.mappings;
}
return result;
}
function uglify(source){
const uglifyResult = UglifyJS.minify(source, {
});
if (!uglifyResult.error) {
return uglifyResult.code;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
function md5(source) {
const hash = crypto.createHash('md5');
return hash.update(source).digest('hex');
}
class ProcessManager {
constructor() {
this.pool = {};
this.scripts = '';
}
deal(process) {
let hash = this.put(process.script);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
let hash = this.put(subProcess.script);
if (hash) {
subProcess.script = 'link://' + hash;
}
}
}
}
put(script) {
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = script;
}
return hash;
}
}
async generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.pool = {};
const {code ,sourcemap} = await compile(scripts, true);
this.scripts = code;
}
generate() {
let scripts = this.scripts;
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
return `
(function(){
var processScripts = {};
(function(exports){
${scripts}
})(processScripts);
engine.setScriptMap(processScripts);
})();
`
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class ScriptManager {
constructor() {
this.pool = [];
}
deal(script){
this.pool.push(script);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
(function(exports){
${code}
})(exports);
engine.registerScriptDef(exports.default.id, exports.default);
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
${scripts}
})();
`;
}
}
/**
* Created by rockyl on 2019-11-29.
*/
class CustomManager {
constructor() {
this.pool = [];
}
deal(custom) {
this.pool.push(custom);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
module = {
exports: {}
};
(function(module){
${code}
})(module);
exports['${id}'] = module.exports;
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
var module;
${scripts}
for(var key in exports){
engine.registerCustomModule(key, exports[key]);
}
})();
`
}
}
/**
* Created by rockyl on 2019-11-13.
......@@ -10,24 +213,31 @@
* 项目打包
*/
const camelcase = require('camelcase');
const replaceFields = ['page_title', 'container_id', 'version'];
const replaceFields = ['pageTitle', 'containerId'];
async function pack(project, options) {
async function pack(data, options) {
let version = Date.now() + Math.floor(Math.random() * 1000);
let data = JSON.parse(project.data);
const newTpl = await pageTemplate(data, options, version);
pageTemplate(data, data.options, version);
const newData = await packData(data, options);
return {
version,
data: newData,
tpl: newTpl,
}
}
async function packData(data, {getProcesses, getScripts, getCustoms}) {
function fillTpl(data, params) {
const {options} = data;
fillTemplate(options.newTpl, options, params);
return options.newTpl;
}
async function packData(data, {debug, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {};
newData.options = data.options;
delete newData.options.tpl;
......@@ -36,6 +246,43 @@
newData.dataMapping = data.dataMapping;
newData.processes = data.processes;
/*=====START process =====*/
let processIDs = [];
findDepPidsBat(processIDs, data.processes);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
}
for (let process of data.processes) {
processManager.deal(process);
}
await processManager.generateCurrent(); //自定义过程先编译
for (let process of builtinProcesses) {
processManager.deal(process);
}
let processScriptContent = processManager.generate();
//console.log(processScriptContent);
if (!debug) {
processScriptContent = uglify(processScriptContent);
}
/*=====END process =====*/
/*=====START script =====*/
let scriptIDs = [];
for (let view of newData.views) {
traverseNode(view, (node) => {
......@@ -49,44 +296,47 @@
});
}
//console.log('scriptIDs:', scriptIDs);
let scriptsContainer = newData.scripts = {};
//let scriptsContainer = newData.scripts = {};
//let scriptsCode = '';
if (scriptIDs.length > 0) {
const scripts = await getScripts(scriptIDs);
for (let scriptData of scripts) {
let {id, code} = JSON.parse(scriptData);
scriptsContainer[id] = code;
let script = JSON.parse(scriptData);
//scriptsContainer[id] = code;
scriptManager.deal(script);
}
//console.log('scripts:', scriptsContainer);
}
let processIDs = [];
findDepPidsBat(processIDs, newData.processes);
//console.log('processIDs:', processIDs);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
let scriptsContent = scriptManager.generate();
//console.log(scriptsContent);
if (!debug) {
scriptsContent = uglify(scriptsContent);
}
/*=====END script =====*/
newData.customs = [];
/*=====START custom =====*/
//newData.customs = [];
if (data.customs && data.customs.length > 0) {
newData.customs = (await getCustoms(data.customs)).map(item => {
return JSON.parse(item);
/*newData.customs = */
(await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item));
//return JSON.parse(item);
});
}
let customScriptContent = customManager.generate();
//console.log(customScriptContent);
if (!debug) {
customScriptContent = uglify(customScriptContent);
}
/*=====END custom =====*/
return JSON.stringify(newData);
return {
data: JSON.stringify(newData),
processScriptContent,
scriptsContent,
customScriptContent,
};
}
function findDepPids(list, process) {
......@@ -120,18 +370,23 @@
return newPids;
}
async function pageTemplate(data, options, version) {
const {tpl,} = data.options;
let newTpl = tpl;
let newOptions = Object.assign(data.options, {
function pageTemplate(tpl, options, version) {
const params = {
version,
});
delete newOptions.tpl;
};
for (let field of replaceFields) {
const key = camelcase(field);
newTpl = newTpl.replace(new RegExp(`\\$${field.toUpperCase()}\\$`, 'g'), newOptions[key]);
params[field] = options[field];
}
fillTemplate(options.tpl, options, params);
}
return newTpl;
function fillTemplate(tpl, options, params) {
for (let field in params) {
const pattern = decamelize(field).toUpperCase();
tpl = tpl.replace(new RegExp(`\\$${pattern}\\$`, 'g'), params[field]);
}
options.newTpl = tpl;
}
function traverseNode(root, callback) {
......@@ -143,6 +398,7 @@
}
}
exports.fillTpl = fillTpl;
exports.pack = pack;
Object.defineProperty(exports, '__esModule', { value: true });
......
This diff is collapsed.
{
"systemParams": "darwin-x64-67",
"modulesFolders": [
"node_modules"
],
"flags": [],
"linkedModules": [
"game-cli",
"qunity-core",
"scilla",
"scilla-declare-generator",
"scilla-launcher",
"scilla-ts-compiler"
],
"topLevelPatterns": [
"camelcase@^5.3.1"
],
"lockfileEntries": {
"camelcase@^5.3.1": "https://registry.npm.taobao.org/camelcase/download/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
},
"files": [],
"artifacts": {}
}
\ No newline at end of file
declare namespace camelcase {
interface Options {
/**
Uppercase the first character: `foo-bar` → `FooBar`.
@default false
*/
readonly pascalCase?: boolean;
}
}
declare const camelcase: {
/**
Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`.
@param input - String to convert to camel case.
@example
```
import camelCase = require('camelcase');
camelCase('foo-bar');
//=> 'fooBar'
camelCase('foo_bar');
//=> 'fooBar'
camelCase('Foo-Bar');
//=> 'fooBar'
camelCase('Foo-Bar', {pascalCase: true});
//=> 'FooBar'
camelCase('--foo.bar', {pascalCase: false});
//=> 'fooBar'
camelCase('foo bar');
//=> 'fooBar'
console.log(process.argv[3]);
//=> '--foo-bar'
camelCase(process.argv[3]);
//=> 'fooBar'
camelCase(['foo', 'bar']);
//=> 'fooBar'
camelCase(['__foo__', '--bar'], {pascalCase: true});
//=> 'FooBar'
```
*/
(input: string | ReadonlyArray<string>, options?: camelcase.Options): string;
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function camelcase(
// input: string | ReadonlyArray<string>,
// options?: camelcase.Options
// ): string;
// export = camelcase;
default: typeof camelcase;
};
export = camelcase;
'use strict';
const preserveCamelCase = string => {
let isLastCharLower = false;
let isLastCharUpper = false;
let isLastLastCharUpper = false;
for (let i = 0; i < string.length; i++) {
const character = string[i];
if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) {
string = string.slice(0, i) + '-' + string.slice(i);
isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
i++;
} else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(character) && character.toLowerCase() === character) {
string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = false;
isLastCharLower = true;
} else {
isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;
}
}
return string;
};
const camelCase = (input, options) => {
if (!(typeof input === 'string' || Array.isArray(input))) {
throw new TypeError('Expected the input to be `string | string[]`');
}
options = Object.assign({
pascalCase: false
}, options);
const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
if (Array.isArray(input)) {
input = input.map(x => x.trim())
.filter(x => x.length)
.join('-');
} else {
input = input.trim();
}
if (input.length === 0) {
return '';
}
if (input.length === 1) {
return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
}
const hasUpperCase = input !== input.toLowerCase();
if (hasUpperCase) {
input = preserveCamelCase(input);
}
input = input
.replace(/^[_.\- ]+/, '')
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase())
.replace(/\d+(\w|$)/g, m => m.toUpperCase());
return postProcess(input);
};
module.exports = camelCase;
// TODO: Remove this for the next major release
module.exports.default = camelCase;
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "camelcase",
"version": "5.3.1",
"description": "Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`",
"license": "MIT",
"repository": "sindresorhus/camelcase",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"camelcase",
"camel-case",
"camel",
"case",
"dash",
"hyphen",
"dot",
"underscore",
"separator",
"string",
"text",
"convert",
"pascalcase",
"pascal-case"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}
# camelcase [![Build Status](https://travis-ci.org/sindresorhus/camelcase.svg?branch=master)](https://travis-ci.org/sindresorhus/camelcase)
> Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: `foo-bar` → `fooBar`
---
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-camelcase?utm_source=npm-camelcase&utm_medium=referral&utm_campaign=readme">Get professional support for 'camelcase' with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
---
## Install
```
$ npm install camelcase
```
## Usage
```js
const camelCase = require('camelcase');
camelCase('foo-bar');
//=> 'fooBar'
camelCase('foo_bar');
//=> 'fooBar'
camelCase('Foo-Bar');
//=> 'fooBar'
camelCase('Foo-Bar', {pascalCase: true});
//=> 'FooBar'
camelCase('--foo.bar', {pascalCase: false});
//=> 'fooBar'
camelCase('foo bar');
//=> 'fooBar'
console.log(process.argv[3]);
//=> '--foo-bar'
camelCase(process.argv[3]);
//=> 'fooBar'
camelCase(['foo', 'bar']);
//=> 'fooBar'
camelCase(['__foo__', '--bar'], {pascalCase: true});
//=> 'FooBar'
```
## API
### camelCase(input, [options])
#### input
Type: `string` `string[]`
String to convert to camel case.
#### options
Type: `Object`
##### pascalCase
Type: `boolean`<br>
Default: `false`
Uppercase the first character: `foo-bar``FooBar`
## Security
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
## Related
- [decamelize](https://github.com/sindresorhus/decamelize) - The inverse of this module
- [uppercamelcase](https://github.com/SamVerschueren/uppercamelcase) - Like this module, but to PascalCase instead of camelCase
- [titleize](https://github.com/sindresorhus/titleize) - Capitalize every word in string
- [humanize-string](https://github.com/sindresorhus/humanize-string) - Convert a camelized/dasherized/underscored string into a humanized one
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
......@@ -4,6 +4,11 @@
"main": "dist/index.js",
"license": "MIT",
"dependencies": {
"camelcase": "^5.3.1"
"@babel/core": "^7.7.4",
"decamelize": "^3.2.0",
"uglify-js": "^3.7.1"
},
"scripts": {
"build": "rollup -c"
}
}
/**
* Created by rockyl on 2019-11-29.
*/
export default class CustomManager {
constructor() {
this.pool = [];
}
deal(custom) {
this.pool.push(custom);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
module = {
exports: {}
};
(function(module){
${code}
})(module);
exports['${id}'] = module.exports;
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
var module;
${scripts}
for(var key in exports){
engine.registerCustomModule(key, exports[key]);
}
})();
`
}
}
/**
* Created by rockyl on 2019-11-29.
*/
import crypto from 'crypto'
import {compile} from "./code-process";
function md5(source) {
const hash = crypto.createHash('md5');
return hash.update(source).digest('hex');
}
export default class ProcessManager {
constructor() {
this.pool = {};
this.scripts = '';
}
deal(process) {
let hash = this.put(process.script);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
let hash = this.put(subProcess.script);
if (hash) {
subProcess.script = 'link://' + hash;
}
}
}
}
put(script) {
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = script;
}
return hash;
}
}
async generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.pool = {};
const {code ,sourcemap} = await compile(scripts, true);
this.scripts = code;
}
generate() {
let scripts = this.scripts;
for (let hash in this.pool) {
let code = this.pool[hash];
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${code}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
return `
(function(){
var processScripts = {};
(function(exports){
${scripts}
})(processScripts);
engine.setScriptMap(processScripts);
})();
`
}
}
/**
* Created by rockyl on 2019-11-29.
*/
export default class ScriptManager {
constructor() {
this.pool = [];
}
deal(script){
this.pool.push(script);
}
generate(){
let scripts = '';
for(let {id, code} of this.pool){
scripts += `
/*=====START ${id} START=====*/
(function(exports){
${code}
})(exports);
engine.registerScriptDef(exports.default.id, exports.default);
/*=====END ${id} END=====*/`;
}
return `
(function(){
var exports = {};
${scripts}
})();
`;
}
}
/**
* Created by rockyl on 2019-11-30.
*/
import babel from '@babel/core';
const UglifyJS = require('uglify-js');
export async function compile(source, debug = false) {
const {code, map} = await babel.transformAsync(source, {
presets: [
['@babel/env', {}]
],
babelrc: false,
sourceMaps: debug,
});
const result = {
code,
};
if (map) {
result.sourcemap = map.mappings;
}
return result;
}
export function uglify(source){
const uglifyResult = UglifyJS.minify(source, {
});
if (!uglifyResult.error) {
return uglifyResult.code;
}
}
\ No newline at end of file
......@@ -4,24 +4,37 @@
* 项目打包
*/
const camelcase = require('camelcase');
import decamelize from 'decamelize'
import ProcessManager from './ProcessManager'
import ScriptManager from './ScriptManager'
import CustomManager from './CustomManager'
import {uglify} from "./code-process";
const replaceFields = ['page_title', 'container_id', 'version'];
const replaceFields = ['pageTitle', 'containerId'];
export async function pack(project, options) {
export async function pack(data, options) {
let version = Date.now() + Math.floor(Math.random() * 1000);
let data = JSON.parse(project.data);
const newTpl = await pageTemplate(data, options, version);
pageTemplate(data, data.options, version);
const newData = await packData(data, options);
return {
version,
data: newData,
tpl: newTpl,
}
}
async function packData(data, {getProcesses, getScripts, getCustoms}) {
export function fillTpl(data, params) {
const {options} = data;
fillTemplate(options.newTpl, options, params);
return options.newTpl;
}
async function packData(data, {debug, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {};
newData.options = data.options;
delete newData.options.tpl;
......@@ -30,6 +43,43 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
newData.dataMapping = data.dataMapping;
newData.processes = data.processes;
/*=====START process =====*/
let processIDs = [];
findDepPidsBat(processIDs, data.processes);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
}
for (let process of data.processes) {
processManager.deal(process);
}
await processManager.generateCurrent(); //自定义过程先编译
for (let process of builtinProcesses) {
processManager.deal(process);
}
let processScriptContent = processManager.generate();
//console.log(processScriptContent);
if (!debug) {
processScriptContent = uglify(processScriptContent);
}
/*=====END process =====*/
/*=====START script =====*/
let scriptIDs = [];
for (let view of newData.views) {
traverseNode(view, (node) => {
......@@ -43,44 +93,47 @@ async function packData(data, {getProcesses, getScripts, getCustoms}) {
});
}
//console.log('scriptIDs:', scriptIDs);
let scriptsContainer = newData.scripts = {};
//let scriptsContainer = newData.scripts = {};
//let scriptsCode = '';
if (scriptIDs.length > 0) {
const scripts = await getScripts(scriptIDs);
for (let scriptData of scripts) {
let {id, code} = JSON.parse(scriptData);
scriptsContainer[id] = code;
let script = JSON.parse(scriptData);
//scriptsContainer[id] = code;
scriptManager.deal(script);
}
//console.log('scripts:', scriptsContainer);
}
let processIDs = [];
findDepPidsBat(processIDs, newData.processes);
//console.log('processIDs:', processIDs);
let builtinProcesses = newData.builtinProcesses = [];
let bProcessIDs = processIDs;
while (true) {
let newPids = await addBuiltinProcesses(builtinProcesses, bProcessIDs, getProcesses);
bProcessIDs = [];
for (let id of newPids) {
if (!processIDs.includes(id)) {
bProcessIDs.push(id);
processIDs.push(id);
}
}
if (bProcessIDs.length === 0) {
break;
}
let scriptsContent = scriptManager.generate();
//console.log(scriptsContent);
if (!debug) {
scriptsContent = uglify(scriptsContent);
}
/*=====END script =====*/
newData.customs = [];
/*=====START custom =====*/
//newData.customs = [];
if (data.customs && data.customs.length > 0) {
newData.customs = (await getCustoms(data.customs)).map(item => {
return JSON.parse(item);
/*newData.customs = */
(await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item));
//return JSON.parse(item);
})
}
let customScriptContent = customManager.generate();
//console.log(customScriptContent);
if (!debug) {
customScriptContent = uglify(customScriptContent);
}
/*=====END custom =====*/
return JSON.stringify(newData);
return {
data: JSON.stringify(newData),
processScriptContent,
scriptsContent,
customScriptContent,
};
}
function findDepPids(list, process) {
......@@ -114,18 +167,23 @@ async function addBuiltinProcesses(list, ids, getProcesses) {
return newPids;
}
async function pageTemplate(data, options, version) {
const {tpl,} = data.options;
let newTpl = tpl;
let newOptions = Object.assign(data.options, {
function pageTemplate(tpl, options, version) {
const params = {
version,
});
delete newOptions.tpl;
};
for (let field of replaceFields) {
const key = camelcase(field);
newTpl = newTpl.replace(new RegExp(`\\$${field.toUpperCase()}\\$`, 'g'), newOptions[key]);
params[field] = options[field];
}
fillTemplate(options.tpl, options, params);
}
function fillTemplate(tpl, options, params) {
for (let field in params) {
const pattern = decamelize(field).toUpperCase();
tpl = tpl.replace(new RegExp(`\\$${pattern}\\$`, 'g'), params[field]);
}
return newTpl;
options.newTpl = tpl;
}
function traverseNode(root, callback) {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment