Commit e6891b70 authored by rockyl's avatar rockyl

增加zeroing-template-fill

parent fef8a31c
import decamelize from 'decamelize';
import babel from '@babel/core'; import babel from '@babel/core';
import { divideCode } from 'zeroing-code-divider'; import { divideCode } from 'zeroing-code-divider';
import { generateLibraryScriptEl, generateJsScriptEl, fillTemplate } from 'zeroing-template-fill';
/** /**
* Created by rockyl on 2019-11-30. * Created by rockyl on 2019-11-30.
...@@ -44,8 +44,13 @@ const replaceFields = ['pageTitle', 'containerId']; ...@@ -44,8 +44,13 @@ const replaceFields = ['pageTitle', 'containerId'];
const TAG = 'zeroing-pack'; const TAG = 'zeroing-pack';
async function pack(data, options) { async function pack(data, options) {
let dependencies = data.dependencies;
let getPackages = options.getPackages;
const {libraryScriptElMap, analyseResult} = await generateLibraryScriptEl(data.options.tpl, dependencies, getPackages, '//yun.duiba.com.cn/editor/zeroing/libs/');
let version = Date.now() + Math.floor(Math.random() * 1000); let version = Date.now() + Math.floor(Math.random() * 1000);
pageTemplate(data, data.options, version); pageTemplate(data, data.options, version, analyseResult, libraryScriptElMap);
const newData = await packData(data, options); const newData = await packData(data, options);
return { return {
...@@ -56,7 +61,8 @@ async function pack(data, options) { ...@@ -56,7 +61,8 @@ async function pack(data, options) {
function fillTpl(data, params) { function fillTpl(data, params) {
const {options} = data; const {options} = data;
fillTemplate(options.newTpl, options, params); params.scripts = params.scripts.map(item => generateJsScriptEl(item)).join('\n');
options.newTpl = fillTemplate(options.newTpl, params);
const newTpl = options.newTpl; const newTpl = options.newTpl;
delete options.newTpl; delete options.newTpl;
...@@ -85,6 +91,7 @@ async function packData(data, {debug, packedAssets, getPackages}) { ...@@ -85,6 +91,7 @@ async function packData(data, {debug, packedAssets, getPackages}) {
debug, debug,
uglify, compile, uglify, compile,
getPackages, getPackages,
dependencies: data.dependencies,
}); });
return { return {
...@@ -115,23 +122,16 @@ function deleteUnusedData(processes) { ...@@ -115,23 +122,16 @@ function deleteUnusedData(processes) {
} }
} }
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version, analyseResult, libraryScriptElMap) {
const params = { const params = {
version, version,
libraries: libraryScriptElMap,
}; };
for (let field of replaceFields) { for (let field of replaceFields) {
params[field] = options[field]; 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]);
}
options.newTpl = tpl; options.newTpl = fillTemplate(options.tpl, params, analyseResult);
} }
export { fillTpl, pack }; export { fillTpl, pack };
......
This diff is collapsed.
...@@ -4,9 +4,9 @@ Object.defineProperty(exports, '__esModule', { value: true }); ...@@ -4,9 +4,9 @@ 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 babel = _interopDefault(require('@babel/core')); var babel = _interopDefault(require('@babel/core'));
var zeroingCodeDivider = require('zeroing-code-divider'); var zeroingCodeDivider = require('zeroing-code-divider');
var zeroingTemplateFill = require('zeroing-template-fill');
/** /**
* Created by rockyl on 2019-11-30. * Created by rockyl on 2019-11-30.
...@@ -50,8 +50,13 @@ const replaceFields = ['pageTitle', 'containerId']; ...@@ -50,8 +50,13 @@ const replaceFields = ['pageTitle', 'containerId'];
const TAG = 'zeroing-pack'; const TAG = 'zeroing-pack';
async function pack(data, options) { async function pack(data, options) {
let dependencies = data.dependencies;
let getPackages = options.getPackages;
const {libraryScriptElMap, analyseResult} = await zeroingTemplateFill.generateLibraryScriptEl(data.options.tpl, dependencies, getPackages, '//yun.duiba.com.cn/editor/zeroing/libs/');
let version = Date.now() + Math.floor(Math.random() * 1000); let version = Date.now() + Math.floor(Math.random() * 1000);
pageTemplate(data, data.options, version); pageTemplate(data, data.options, version, analyseResult, libraryScriptElMap);
const newData = await packData(data, options); const newData = await packData(data, options);
return { return {
...@@ -62,7 +67,8 @@ async function pack(data, options) { ...@@ -62,7 +67,8 @@ async function pack(data, options) {
function fillTpl(data, params) { function fillTpl(data, params) {
const {options} = data; const {options} = data;
fillTemplate(options.newTpl, options, params); params.scripts = params.scripts.map(item => zeroingTemplateFill.generateJsScriptEl(item)).join('\n');
options.newTpl = zeroingTemplateFill.fillTemplate(options.newTpl, params);
const newTpl = options.newTpl; const newTpl = options.newTpl;
delete options.newTpl; delete options.newTpl;
...@@ -91,6 +97,7 @@ async function packData(data, {debug, packedAssets, getPackages}) { ...@@ -91,6 +97,7 @@ async function packData(data, {debug, packedAssets, getPackages}) {
debug, debug,
uglify, compile, uglify, compile,
getPackages, getPackages,
dependencies: data.dependencies,
}); });
return { return {
...@@ -121,23 +128,16 @@ function deleteUnusedData(processes) { ...@@ -121,23 +128,16 @@ function deleteUnusedData(processes) {
} }
} }
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version, analyseResult, libraryScriptElMap) {
const params = { const params = {
version, version,
libraries: libraryScriptElMap,
}; };
for (let field of replaceFields) { for (let field of replaceFields) {
params[field] = options[field]; 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]);
}
options.newTpl = tpl; options.newTpl = zeroingTemplateFill.fillTemplate(options.tpl, params, analyseResult);
} }
exports.fillTpl = fillTpl; exports.fillTpl = fillTpl;
......
This diff is collapsed.
(function (global, factory) { (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('decamelize'), require('@babel/core'), require('zeroing-code-divider')) : typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/core'), require('zeroing-code-divider'), require('zeroing-template-fill')) :
typeof define === 'function' && define.amd ? define(['exports', 'decamelize', '@babel/core', 'zeroing-code-divider'], factory) : typeof define === 'function' && define.amd ? define(['exports', '@babel/core', 'zeroing-code-divider', 'zeroing-template-fill'], factory) :
(global = global || self, factory(global['zeroing-pack'] = {}, global.decamelize, global.babel, global.zeroingCodeDivider)); (global = global || self, factory(global['zeroing-pack'] = {}, global.babel, global.zeroingCodeDivider, global.zeroingTemplateFill));
}(this, function (exports, decamelize, babel, zeroingCodeDivider) { 'use strict'; }(this, function (exports, babel, zeroingCodeDivider, zeroingTemplateFill) { 'use strict';
decamelize = decamelize && decamelize.hasOwnProperty('default') ? decamelize['default'] : decamelize;
babel = babel && babel.hasOwnProperty('default') ? babel['default'] : babel; babel = babel && babel.hasOwnProperty('default') ? babel['default'] : babel;
/** /**
...@@ -49,8 +48,13 @@ ...@@ -49,8 +48,13 @@
const TAG = 'zeroing-pack'; const TAG = 'zeroing-pack';
async function pack(data, options) { async function pack(data, options) {
let dependencies = data.dependencies;
let getPackages = options.getPackages;
const {libraryScriptElMap, analyseResult} = await zeroingTemplateFill.generateLibraryScriptEl(data.options.tpl, dependencies, getPackages, '//yun.duiba.com.cn/editor/zeroing/libs/');
let version = Date.now() + Math.floor(Math.random() * 1000); let version = Date.now() + Math.floor(Math.random() * 1000);
pageTemplate(data, data.options, version); pageTemplate(data, data.options, version, analyseResult, libraryScriptElMap);
const newData = await packData(data, options); const newData = await packData(data, options);
return { return {
...@@ -61,7 +65,8 @@ ...@@ -61,7 +65,8 @@
function fillTpl(data, params) { function fillTpl(data, params) {
const {options} = data; const {options} = data;
fillTemplate(options.newTpl, options, params); params.scripts = params.scripts.map(item => zeroingTemplateFill.generateJsScriptEl(item)).join('\n');
options.newTpl = zeroingTemplateFill.fillTemplate(options.newTpl, params);
const newTpl = options.newTpl; const newTpl = options.newTpl;
delete options.newTpl; delete options.newTpl;
...@@ -90,6 +95,7 @@ ...@@ -90,6 +95,7 @@
debug, debug,
uglify, compile, uglify, compile,
getPackages, getPackages,
dependencies: data.dependencies,
}); });
return { return {
...@@ -120,23 +126,16 @@ ...@@ -120,23 +126,16 @@
} }
} }
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version, analyseResult, libraryScriptElMap) {
const params = { const params = {
version, version,
libraries: libraryScriptElMap,
}; };
for (let field of replaceFields) { for (let field of replaceFields) {
params[field] = options[field]; 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]);
}
options.newTpl = tpl; options.newTpl = zeroingTemplateFill.fillTemplate(options.tpl, params, analyseResult);
} }
exports.fillTpl = fillTpl; exports.fillTpl = fillTpl;
......
This diff is collapsed.
...@@ -6,11 +6,12 @@ ...@@ -6,11 +6,12 @@
"dependencies": { "dependencies": {
"@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", "gensync": "^1.0.0-beta.1",
"uglify-js": "^3.7.1", "uglify-js": "^3.7.1",
"zeroing-code-divider": "http://gitlab2.dui88.com/laoqifeng/zeroing-code-divider.git" "zeroing-code-divider": "http://gitlab2.dui88.com/laoqifeng/zeroing-code-divider.git"
}, },
"scripts": { "scripts": {
"dev": "rollup -c -w",
"build": "rollup -c" "build": "rollup -c"
} }
} }
...@@ -4,16 +4,21 @@ ...@@ -4,16 +4,21 @@
* 项目打包 * 项目打包
*/ */
import decamelize from 'decamelize'
import {uglify, compile} from "./code-process"; import {uglify, compile} from "./code-process";
import {divideCode} from 'zeroing-code-divider'; import {divideCode} from 'zeroing-code-divider';
import {generateLibraryScriptEl, fillTemplate, generateJsScriptEl} from 'zeroing-template-fill';
const replaceFields = ['pageTitle', 'containerId']; const replaceFields = ['pageTitle', 'containerId'];
const TAG = 'zeroing-pack'; const TAG = 'zeroing-pack';
export async function pack(data, options) { export async function pack(data, options) {
let dependencies = data.dependencies;
let getPackages = options.getPackages;
const {libraryScriptElMap, analyseResult} = await generateLibraryScriptEl(data.options.tpl, dependencies, getPackages, '//yun.duiba.com.cn/editor/zeroing/libs/');
let version = Date.now() + Math.floor(Math.random() * 1000); let version = Date.now() + Math.floor(Math.random() * 1000);
pageTemplate(data, data.options, version); pageTemplate(data, data.options, version, analyseResult, libraryScriptElMap);
const newData = await packData(data, options); const newData = await packData(data, options);
return { return {
...@@ -24,7 +29,8 @@ export async function pack(data, options) { ...@@ -24,7 +29,8 @@ export async function pack(data, options) {
export function fillTpl(data, params) { export function fillTpl(data, params) {
const {options} = data; const {options} = data;
fillTemplate(options.newTpl, options, params); params.scripts = params.scripts.map(item => generateJsScriptEl(item)).join('\n');
options.newTpl = fillTemplate(options.newTpl, params);
const newTpl = options.newTpl; const newTpl = options.newTpl;
delete options.newTpl; delete options.newTpl;
...@@ -53,6 +59,7 @@ async function packData(data, {debug, packedAssets, getPackages}) { ...@@ -53,6 +59,7 @@ async function packData(data, {debug, packedAssets, getPackages}) {
debug, debug,
uglify, compile, uglify, compile,
getPackages, getPackages,
dependencies: data.dependencies,
}); });
return { return {
...@@ -83,21 +90,14 @@ function deleteUnusedData(processes) { ...@@ -83,21 +90,14 @@ function deleteUnusedData(processes) {
} }
} }
function pageTemplate(tpl, options, version) { function pageTemplate(tpl, options, version, analyseResult, libraryScriptElMap) {
const params = { const params = {
version, version,
libraries: libraryScriptElMap,
}; };
for (let field of replaceFields) { for (let field of replaceFields) {
params[field] = options[field]; 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]);
}
options.newTpl = tpl; options.newTpl = fillTemplate(options.tpl, params, analyseResult);
} }
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