Commit c936d152 authored by rockyl's avatar rockyl

分离代码分离器

parent 7a65f27a
import decamelize from 'decamelize'; import decamelize from 'decamelize';
import crypto from 'crypto';
import babel from '@babel/core'; import babel from '@babel/core';
import { divideCode } from 'zeroing-code-divider';
/** /**
* Created by rockyl on 2019-11-30. * Created by rockyl on 2019-11-30.
...@@ -34,172 +34,6 @@ function uglify(source){ ...@@ -34,172 +34,6 @@ function uglify(source){
} }
} }
/**
* 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);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
this.deal(subProcess);
}
}
}
put(process) {
const {id, name, script} = process;
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = {
script,
ids: [],
};
}
this.pool[hash].ids.push(`${id} - ${name}`);
return hash;
}
}
generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let {script, ids} = this.pool[hash];
let idsComment = ids.map(id => {
return `/*== ${id} ==*/`
}).join('\n');
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${idsComment}
${script}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.scripts += scripts;
this.pool = {};
}
async compile() {
try {
const {code, sourcemap} = await compile(this.scripts, true);
this.scripts = code;
} catch (e) {
console.log('编译失败', e);
}
}
generate() {
this.generateCurrent();
let scripts = this.scripts;
return `
(function(){
var exports = {};
${scripts}
engine.setScriptMap(exports);
})();
`
}
}
/**
* 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. * Created by rockyl on 2019-11-13.
* *
...@@ -230,10 +64,6 @@ function fillTpl(data, params) { ...@@ -230,10 +64,6 @@ function fillTpl(data, params) {
} }
async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) { async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {}; let newData = {};
newData.options = data.options; newData.options = data.options;
newData.views = data.views; newData.views = data.views;
...@@ -246,7 +76,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -246,7 +76,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
console.log(TAG, 'start'); console.log(TAG, 'start');
/*=====START process =====*/ const {
processScriptContent,
scriptsContent,
customScriptContent,
} = await divideCode(newData, {
debug,
uglify, compile,
getProcesses, getScripts, getCustoms,
});
/*/!*=====START process =====*!/
console.log(TAG, 'start process'); console.log(TAG, 'start process');
let processIDs = []; let processIDs = [];
findDepPidsBat(processIDs, newData.processes); findDepPidsBat(processIDs, newData.processes);
...@@ -285,9 +125,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -285,9 +125,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
processScriptContent = uglify(processScriptContent); processScriptContent = uglify(processScriptContent);
} }
/*=====END process =====*/ /!*=====END process =====*!/
/*=====START script =====*/ /!*=====START script =====*!/
console.log(TAG, 'start script'); console.log(TAG, 'start script');
let scriptIDs = []; let scriptIDs = [];
for (let view of newData.views) { for (let view of newData.views) {
...@@ -319,24 +159,24 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -319,24 +159,24 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
scriptsContent = uglify(scriptsContent); scriptsContent = uglify(scriptsContent);
} }
/*=====END script =====*/ /!*=====END script =====*!/
/*=====START custom =====*/ /!*=====START custom =====*!/
console.log(TAG, 'start custom'); console.log(TAG, 'start custom');
//newData.customs = []; //newData.customs = [];
if (data.customs && data.customs.length > 0) { if (data.customs && data.customs.length > 0) {
/*newData.customs = */ /!*newData.customs = *!/
(await getCustoms(data.customs)).map(item => { (await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item)); customManager.deal(JSON.parse(item));
//return JSON.parse(item); //return JSON.parse(item);
}); })
} }
let customScriptContent = customManager.generate(); let customScriptContent = customManager.generate();
//console.log(customScriptContent); //console.log(customScriptContent);
if (!debug) { if (!debug) {
customScriptContent = uglify(customScriptContent); customScriptContent = uglify(customScriptContent);
} }
/*=====END custom =====*/ /!*=====END custom =====*!/*/
return { return {
data: JSON.stringify(newData), data: JSON.stringify(newData),
...@@ -366,36 +206,6 @@ function deleteUnusedData(processes) { ...@@ -366,36 +206,6 @@ function deleteUnusedData(processes) {
} }
} }
function findDepPids(list, process) {
if (process.sub) {
for (let key in process.sub) {
let p = process.sub[key];
if (!list.includes(p.meta)) {
list.push(p.meta);
}
}
}
}
function findDepPidsBat(list, processes) {
for (let process of processes) {
findDepPids(list, process);
}
}
async function addBuiltinProcesses(list, ids, getProcesses) {
let newPids = [];
if (ids.length > 0) {
let processes = await getProcesses(ids);
for (let processData of processes) {
let process = JSON.parse(processData);
list.push(process);
findDepPids(newPids, process);
}
}
return newPids;
}
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version) {
const params = { const params = {
version, version,
...@@ -415,14 +225,5 @@ function fillTemplate(tpl, options, params) { ...@@ -415,14 +225,5 @@ function fillTemplate(tpl, options, params) {
options.newTpl = tpl; options.newTpl = tpl;
} }
function traverseNode(root, callback) {
callback(root);
if (root.children && root.children.length > 0) {
for (let childNode of root.children) {
traverseNode(childNode, callback);
}
}
}
export { fillTpl, pack }; export { fillTpl, pack };
//# sourceMappingURL=index.es.js.map //# sourceMappingURL=index.es.js.map
This diff is collapsed.
...@@ -5,8 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true }); ...@@ -5,8 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var decamelize = _interopDefault(require('decamelize')); var decamelize = _interopDefault(require('decamelize'));
var crypto = _interopDefault(require('crypto'));
var babel = _interopDefault(require('@babel/core')); var babel = _interopDefault(require('@babel/core'));
var zeroingCodeDivider = require('zeroing-code-divider');
/** /**
* Created by rockyl on 2019-11-30. * Created by rockyl on 2019-11-30.
...@@ -40,172 +40,6 @@ function uglify(source){ ...@@ -40,172 +40,6 @@ function uglify(source){
} }
} }
/**
* 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);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
this.deal(subProcess);
}
}
}
put(process) {
const {id, name, script} = process;
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = {
script,
ids: [],
};
}
this.pool[hash].ids.push(`${id} - ${name}`);
return hash;
}
}
generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let {script, ids} = this.pool[hash];
let idsComment = ids.map(id => {
return `/*== ${id} ==*/`
}).join('\n');
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${idsComment}
${script}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.scripts += scripts;
this.pool = {};
}
async compile() {
try {
const {code, sourcemap} = await compile(this.scripts, true);
this.scripts = code;
} catch (e) {
console.log('编译失败', e);
}
}
generate() {
this.generateCurrent();
let scripts = this.scripts;
return `
(function(){
var exports = {};
${scripts}
engine.setScriptMap(exports);
})();
`
}
}
/**
* 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. * Created by rockyl on 2019-11-13.
* *
...@@ -236,10 +70,6 @@ function fillTpl(data, params) { ...@@ -236,10 +70,6 @@ function fillTpl(data, params) {
} }
async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) { async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {}; let newData = {};
newData.options = data.options; newData.options = data.options;
newData.views = data.views; newData.views = data.views;
...@@ -252,7 +82,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -252,7 +82,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
console.log(TAG, 'start'); console.log(TAG, 'start');
/*=====START process =====*/ const {
processScriptContent,
scriptsContent,
customScriptContent,
} = await zeroingCodeDivider.divideCode(newData, {
debug,
uglify, compile,
getProcesses, getScripts, getCustoms,
});
/*/!*=====START process =====*!/
console.log(TAG, 'start process'); console.log(TAG, 'start process');
let processIDs = []; let processIDs = [];
findDepPidsBat(processIDs, newData.processes); findDepPidsBat(processIDs, newData.processes);
...@@ -291,9 +131,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -291,9 +131,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
processScriptContent = uglify(processScriptContent); processScriptContent = uglify(processScriptContent);
} }
/*=====END process =====*/ /!*=====END process =====*!/
/*=====START script =====*/ /!*=====START script =====*!/
console.log(TAG, 'start script'); console.log(TAG, 'start script');
let scriptIDs = []; let scriptIDs = [];
for (let view of newData.views) { for (let view of newData.views) {
...@@ -325,24 +165,24 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -325,24 +165,24 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
scriptsContent = uglify(scriptsContent); scriptsContent = uglify(scriptsContent);
} }
/*=====END script =====*/ /!*=====END script =====*!/
/*=====START custom =====*/ /!*=====START custom =====*!/
console.log(TAG, 'start custom'); console.log(TAG, 'start custom');
//newData.customs = []; //newData.customs = [];
if (data.customs && data.customs.length > 0) { if (data.customs && data.customs.length > 0) {
/*newData.customs = */ /!*newData.customs = *!/
(await getCustoms(data.customs)).map(item => { (await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item)); customManager.deal(JSON.parse(item));
//return JSON.parse(item); //return JSON.parse(item);
}); })
} }
let customScriptContent = customManager.generate(); let customScriptContent = customManager.generate();
//console.log(customScriptContent); //console.log(customScriptContent);
if (!debug) { if (!debug) {
customScriptContent = uglify(customScriptContent); customScriptContent = uglify(customScriptContent);
} }
/*=====END custom =====*/ /!*=====END custom =====*!/*/
return { return {
data: JSON.stringify(newData), data: JSON.stringify(newData),
...@@ -372,36 +212,6 @@ function deleteUnusedData(processes) { ...@@ -372,36 +212,6 @@ function deleteUnusedData(processes) {
} }
} }
function findDepPids(list, process) {
if (process.sub) {
for (let key in process.sub) {
let p = process.sub[key];
if (!list.includes(p.meta)) {
list.push(p.meta);
}
}
}
}
function findDepPidsBat(list, processes) {
for (let process of processes) {
findDepPids(list, process);
}
}
async function addBuiltinProcesses(list, ids, getProcesses) {
let newPids = [];
if (ids.length > 0) {
let processes = await getProcesses(ids);
for (let processData of processes) {
let process = JSON.parse(processData);
list.push(process);
findDepPids(newPids, process);
}
}
return newPids;
}
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version) {
const params = { const params = {
version, version,
...@@ -421,15 +231,6 @@ function fillTemplate(tpl, options, params) { ...@@ -421,15 +231,6 @@ function fillTemplate(tpl, options, params) {
options.newTpl = tpl; options.newTpl = tpl;
} }
function traverseNode(root, callback) {
callback(root);
if (root.children && root.children.length > 0) {
for (let childNode of root.children) {
traverseNode(childNode, callback);
}
}
}
exports.fillTpl = fillTpl; exports.fillTpl = fillTpl;
exports.pack = pack; exports.pack = pack;
//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map
This diff is collapsed.
(function (global, factory) { (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('decamelize'), require('crypto'), require('@babel/core')) : typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('decamelize'), require('@babel/core'), require('zeroing-code-divider')) :
typeof define === 'function' && define.amd ? define(['exports', 'decamelize', 'crypto', '@babel/core'], factory) : typeof define === 'function' && define.amd ? define(['exports', 'decamelize', '@babel/core', 'zeroing-code-divider'], factory) :
(global = global || self, factory(global['zeroing-pack'] = {}, global.decamelize, global.crypto, global.babel)); (global = global || self, factory(global['zeroing-pack'] = {}, global.decamelize, global.babel, global.zeroingCodeDivider));
}(this, function (exports, decamelize, crypto, babel) { 'use strict'; }(this, function (exports, decamelize, babel, zeroingCodeDivider) { 'use strict';
decamelize = decamelize && decamelize.hasOwnProperty('default') ? decamelize['default'] : decamelize; decamelize = decamelize && decamelize.hasOwnProperty('default') ? decamelize['default'] : decamelize;
crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto;
babel = babel && babel.hasOwnProperty('default') ? babel['default'] : babel; babel = babel && babel.hasOwnProperty('default') ? babel['default'] : babel;
/** /**
...@@ -40,172 +39,6 @@ ...@@ -40,172 +39,6 @@
} }
} }
/**
* 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);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
this.deal(subProcess);
}
}
}
put(process) {
const {id, name, script} = process;
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = {
script,
ids: [],
};
}
this.pool[hash].ids.push(`${id} - ${name}`);
return hash;
}
}
generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let {script, ids} = this.pool[hash];
let idsComment = ids.map(id => {
return `/*== ${id} ==*/`
}).join('\n');
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${idsComment}
${script}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.scripts += scripts;
this.pool = {};
}
async compile() {
try {
const {code, sourcemap} = await compile(this.scripts, true);
this.scripts = code;
} catch (e) {
console.log('编译失败', e);
}
}
generate() {
this.generateCurrent();
let scripts = this.scripts;
return `
(function(){
var exports = {};
${scripts}
engine.setScriptMap(exports);
})();
`
}
}
/**
* 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. * Created by rockyl on 2019-11-13.
* *
...@@ -236,10 +69,6 @@ ${scripts} ...@@ -236,10 +69,6 @@ ${scripts}
} }
async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) { async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {}; let newData = {};
newData.options = data.options; newData.options = data.options;
newData.views = data.views; newData.views = data.views;
...@@ -252,7 +81,17 @@ ${scripts} ...@@ -252,7 +81,17 @@ ${scripts}
console.log(TAG, 'start'); console.log(TAG, 'start');
/*=====START process =====*/ const {
processScriptContent,
scriptsContent,
customScriptContent,
} = await zeroingCodeDivider.divideCode(newData, {
debug,
uglify, compile,
getProcesses, getScripts, getCustoms,
});
/*/!*=====START process =====*!/
console.log(TAG, 'start process'); console.log(TAG, 'start process');
let processIDs = []; let processIDs = [];
findDepPidsBat(processIDs, newData.processes); findDepPidsBat(processIDs, newData.processes);
...@@ -291,9 +130,9 @@ ${scripts} ...@@ -291,9 +130,9 @@ ${scripts}
if (!debug) { if (!debug) {
processScriptContent = uglify(processScriptContent); processScriptContent = uglify(processScriptContent);
} }
/*=====END process =====*/ /!*=====END process =====*!/
/*=====START script =====*/ /!*=====START script =====*!/
console.log(TAG, 'start script'); console.log(TAG, 'start script');
let scriptIDs = []; let scriptIDs = [];
for (let view of newData.views) { for (let view of newData.views) {
...@@ -325,24 +164,24 @@ ${scripts} ...@@ -325,24 +164,24 @@ ${scripts}
if (!debug) { if (!debug) {
scriptsContent = uglify(scriptsContent); scriptsContent = uglify(scriptsContent);
} }
/*=====END script =====*/ /!*=====END script =====*!/
/*=====START custom =====*/ /!*=====START custom =====*!/
console.log(TAG, 'start custom'); console.log(TAG, 'start custom');
//newData.customs = []; //newData.customs = [];
if (data.customs && data.customs.length > 0) { if (data.customs && data.customs.length > 0) {
/*newData.customs = */ /!*newData.customs = *!/
(await getCustoms(data.customs)).map(item => { (await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item)); customManager.deal(JSON.parse(item));
//return JSON.parse(item); //return JSON.parse(item);
}); })
} }
let customScriptContent = customManager.generate(); let customScriptContent = customManager.generate();
//console.log(customScriptContent); //console.log(customScriptContent);
if (!debug) { if (!debug) {
customScriptContent = uglify(customScriptContent); customScriptContent = uglify(customScriptContent);
} }
/*=====END custom =====*/ /!*=====END custom =====*!/*/
return { return {
data: JSON.stringify(newData), data: JSON.stringify(newData),
...@@ -372,36 +211,6 @@ ${scripts} ...@@ -372,36 +211,6 @@ ${scripts}
} }
} }
function findDepPids(list, process) {
if (process.sub) {
for (let key in process.sub) {
let p = process.sub[key];
if (!list.includes(p.meta)) {
list.push(p.meta);
}
}
}
}
function findDepPidsBat(list, processes) {
for (let process of processes) {
findDepPids(list, process);
}
}
async function addBuiltinProcesses(list, ids, getProcesses) {
let newPids = [];
if (ids.length > 0) {
let processes = await getProcesses(ids);
for (let processData of processes) {
let process = JSON.parse(processData);
list.push(process);
findDepPids(newPids, process);
}
}
return newPids;
}
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version) {
const params = { const params = {
version, version,
...@@ -421,15 +230,6 @@ ${scripts} ...@@ -421,15 +230,6 @@ ${scripts}
options.newTpl = tpl; options.newTpl = tpl;
} }
function traverseNode(root, callback) {
callback(root);
if (root.children && root.children.length > 0) {
for (let childNode of root.children) {
traverseNode(childNode, callback);
}
}
}
exports.fillTpl = fillTpl; exports.fillTpl = fillTpl;
exports.pack = pack; exports.pack = pack;
......
This diff is collapsed.
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
"@babel/core": "^7.7.4", "@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4", "@babel/preset-env": "^7.7.4",
"decamelize": "^3.2.0", "decamelize": "^3.2.0",
"uglify-js": "^3.7.1" "uglify-js": "^3.7.1",
"zeroing-code-divider": "http://gitlab2.dui88.com/laoqifeng/zeroing-code-divider.git"
}, },
"scripts": { "scripts": {
"build": "rollup -c" "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);
if (hash) {
process.script = 'link://' + hash;
}
if (process.metas) {
for (let subProcess of process.metas) {
this.deal(subProcess);
}
}
}
put(process) {
const {id, name, script} = process;
if (script) {
let hash = md5(script);
if (!this.pool.hasOwnProperty(hash)) {
this.pool[hash] = {
script,
ids: [],
};
}
this.pool[hash].ids.push(`${id} - ${name}`);
return hash;
}
}
generateCurrent() {
let scripts = '';
for (let hash in this.pool) {
let {script, ids} = this.pool[hash];
let idsComment = ids.map(id => {
return `/*== ${id} ==*/`
}).join('\n');
scripts += `
exports['${hash}'] = function(args, props, target, global, vm){
return new Promise(function(resolve, reject){
${idsComment}
${script}
function next(type, payload){resolve({type: type, payload: payload})}
});
};
`;
}
this.scripts += scripts;
this.pool = {};
}
async compile() {
try {
const {code, sourcemap} = await compile(this.scripts, true);
this.scripts = code;
} catch (e) {
console.log('编译失败', e);
}
}
generate() {
this.generateCurrent();
let scripts = this.scripts;
return `
(function(){
var exports = {};
${scripts}
engine.setScriptMap(exports);
})();
`
}
}
/**
* 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}
})();
`;
}
}
...@@ -5,10 +5,8 @@ ...@@ -5,10 +5,8 @@
*/ */
import decamelize from 'decamelize' import decamelize from 'decamelize'
import ProcessManager from './ProcessManager' import {uglify, compile} from "./code-process";
import ScriptManager from './ScriptManager' import {divideCode} from 'zeroing-code-divider';
import CustomManager from './CustomManager'
import {uglify} from "./code-process";
const replaceFields = ['pageTitle', 'containerId']; const replaceFields = ['pageTitle', 'containerId'];
const TAG = 'zeroing-pack'; const TAG = 'zeroing-pack';
...@@ -34,10 +32,6 @@ export function fillTpl(data, params) { ...@@ -34,10 +32,6 @@ export function fillTpl(data, params) {
} }
async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) { async function packData(data, {debug, packedAssets, getProcesses, getScripts, getCustoms}) {
const processManager = new ProcessManager();
const scriptManager = new ScriptManager();
const customManager = new CustomManager();
let newData = {}; let newData = {};
newData.options = data.options; newData.options = data.options;
newData.views = data.views; newData.views = data.views;
...@@ -50,7 +44,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -50,7 +44,17 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
console.log(TAG, 'start'); console.log(TAG, 'start');
/*=====START process =====*/ const {
processScriptContent,
scriptsContent,
customScriptContent,
} = await divideCode(newData, {
debug,
uglify, compile,
getProcesses, getScripts, getCustoms,
});
/*/!*=====START process =====*!/
console.log(TAG, 'start process'); console.log(TAG, 'start process');
let processIDs = []; let processIDs = [];
findDepPidsBat(processIDs, newData.processes); findDepPidsBat(processIDs, newData.processes);
...@@ -89,9 +93,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -89,9 +93,9 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
processScriptContent = uglify(processScriptContent); processScriptContent = uglify(processScriptContent);
} }
/*=====END process =====*/ /!*=====END process =====*!/
/*=====START script =====*/ /!*=====START script =====*!/
console.log(TAG, 'start script'); console.log(TAG, 'start script');
let scriptIDs = []; let scriptIDs = [];
for (let view of newData.views) { for (let view of newData.views) {
...@@ -123,13 +127,13 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -123,13 +127,13 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
scriptsContent = uglify(scriptsContent); scriptsContent = uglify(scriptsContent);
} }
/*=====END script =====*/ /!*=====END script =====*!/
/*=====START custom =====*/ /!*=====START custom =====*!/
console.log(TAG, 'start custom'); console.log(TAG, 'start custom');
//newData.customs = []; //newData.customs = [];
if (data.customs && data.customs.length > 0) { if (data.customs && data.customs.length > 0) {
/*newData.customs = */ /!*newData.customs = *!/
(await getCustoms(data.customs)).map(item => { (await getCustoms(data.customs)).map(item => {
customManager.deal(JSON.parse(item)); customManager.deal(JSON.parse(item));
//return JSON.parse(item); //return JSON.parse(item);
...@@ -140,7 +144,7 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge ...@@ -140,7 +144,7 @@ async function packData(data, {debug, packedAssets, getProcesses, getScripts, ge
if (!debug) { if (!debug) {
customScriptContent = uglify(customScriptContent); customScriptContent = uglify(customScriptContent);
} }
/*=====END custom =====*/ /!*=====END custom =====*!/*/
return { return {
data: JSON.stringify(newData), data: JSON.stringify(newData),
......
...@@ -1005,3 +1005,7 @@ xregexp@^4.2.4: ...@@ -1005,3 +1005,7 @@ xregexp@^4.2.4:
integrity sha1-AqSuoFbWWkJjLALwIz6rjk1+V+0= integrity sha1-AqSuoFbWWkJjLALwIz6rjk1+V+0=
dependencies: dependencies:
"@babel/runtime-corejs2" "^7.2.0" "@babel/runtime-corejs2" "^7.2.0"
"zeroing-code-divider@http://gitlab2.dui88.com/laoqifeng/zeroing-code-divider.git":
version "1.0.0"
resolved "http://gitlab2.dui88.com/laoqifeng/zeroing-code-divider.git#18776cf8096cc5d5133c16dd58ac3de512c39402"
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