amis2/scripts/embed-packager.js
吴多益 33686a375e
SCSS 基于 CSS custom properties 重构,支持通过配置来控制展现风格 (#1190)
* 使用自定义 css 属性初步,支持大部分组件的展现

* button 大部分可以看了

* cxd 和 dark 大部分正常

* 修复一些细节样式错误;补充 css 变量的文档

* 修复几个脚本发现的错误

* 完善一下注释

* 修复一些样式不一致问题

* 修复可能存在的 css xss

* 恢复 font-variant 功能

* 修复绝大部分 @if 相关的问题

* 恢复之前的注释

* 修复小错误,并将所有 background-color 改成 background,这样就能设置渐变色

* 修复 button group 在 cxd 下不一致问题

* 缩小查看配置和复制配置的宽度,留出更多空间

* 修复一些潜在的错误

* 恢复 utilities 中 label 背景色的设置

* 修复错误的 css 变量

* 补充 IE11 Variables Polyfill
2020-12-21 10:08:40 +08:00

199 lines
4.9 KiB
JavaScript

/* eslint-disable */
var rLinkScript = /(<!(?:--)?\[[\s\S]*?<\!\[endif\](?:--)?>|<!--[\s\S]*?(?:-->|$))|(?:(\s*<script([^>]*)>([\s\S]*?)<\/script>)|(?:\s*(<link([^>]*?)(?:\/)?>)|(<style([^>]*)>([\s\S]*?)<\/style>)))(<!--ignore-->)?\n?/gi;
var rScriptType = /type=('|")(.*?)\1/i;
var rSrcHref = /\s*(?:src|href)=('|")(.+?)\1/i;
var rRefStyle = /rel=('|")stylesheet\1/i;
var path = require('path');
var css = require('css');
var rSourceMap = /(?:\/\/\#\s*sourceMappingURL[^\r\n\'\"]*|\/\*\#\s*sourceMappingURL[^\r\n\'\"]*\*\/)(?:\r?\n|$)/gi;
var caches = {};
var createResource = fis.require('postpackager-loader/lib/resource.js');
function prefixCss(code, prefix) {
var cssAst = css.parse(code);
prefixNode(cssAst);
return (
css.stringify(cssAst) +
'\n' +
prefix +
'{background-color: #f0f3f4;position: relative;} \n' +
prefix +
' .app {padding-bottom:0;}'
);
function prefixSelector(sel) {
if (sel.match(/^@/)) return sel;
if (sel.match(/^:root/)) return sel;
var m = sel.match(/(^| )(body|html)($|\W.*)/i);
if (m) return m[1] + prefix + m[3];
else if (sel.match(/^\.is\-modalOpened/))
return sel.replace(
/^\.is\-modalOpened\s/,
'.is-modalOpened ' + prefix + ' '
);
else if (
sel.match(
/^(?:\.fr-|\.fa|\.tox|\.monaco-|\.vs-dark|\.hc-black|\.vs\b|\.cursor-|::|\.context-view|\.menubar|\.fullscreen|\.colorpicker-)/
)
)
return sel;
else return prefix + ' ' + sel;
}
function prefixNode(node) {
if (node.selectors) {
node.selectors = node.selectors.map(prefixSelector);
} else if (node.stylesheet) {
node.stylesheet.rules.forEach(prefixNode);
} else if (node.rules) {
node.rules.forEach(prefixNode);
}
}
}
function unicodeJs(str) {
return str.replace(/([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2019])/g, function (
_,
value
) {
return '\\u' + value.charCodeAt(0).toString(16);
});
}
module.exports = function (ret, pack, settings, opt) {
var root = fis.project.getProjectPath();
var tpl = ret.pkg['/examples/sdk-placeholder.html'];
tpl.skiped = true;
if (tpl && tpl._fromCache && caches[tpl.id]) {
tpl.setContent(caches[tpl.id]);
return;
} else if (!tpl) {
return;
}
var mapping = {};
var contents = tpl.getContent();
var cssContents = '';
var jsContents = '';
var entryJs = '';
var resource = tpl._resource;
var files = ret.pkg;
Object.keys(files).forEach(function (subpath) {
var file = files[subpath];
mapping[file.getUrl()] = file;
});
contents.replace(rLinkScript, function (
all,
comment,
script,
attrs,
body,
link,
lattrs,
style,
sattrs,
sbody,
ignored
) {
// 忽略注释。
if (comment || ignored) {
return all;
}
if (script && !body.trim() && rSrcHref.test(attrs)) {
all = '';
let src = RegExp.$2;
let file = resource.getFileByUrl(src);
if (!file) {
file = resource.getFileByUrl(
fis.util(path.join(path.dirname(tpl.release), src))
);
}
if (!file) {
file = mapping[src];
}
if (file) {
file.skiped = true;
let contents = file.getContent();
if (/_map\.js$/.test(file.subpath)) {
contents = `(function() {
var d = '';
try {
throw new Error()
} catch (e) {
d = (/((?:https?|file)\:.*)$/.test(e.stack) ? RegExp.$1 : '').replace(/\\/[^\\/]*$/, '');
}
amis.host = d;
${contents.replace(/\"url\"\s*\:\s*('|")(\.\/.*)\1/g, function (
_,
quote,
value
) {
return `"url": d + ${quote}${value.substring(1)}${quote}`;
})}
})()`;
}
jsContents += contents + ';\n';
}
} else if (
(script && !rScriptType.test(attrs)) ||
(rScriptType.test(attrs) &&
~['text/javascript', 'application/javascript'].indexOf(
RegExp.$2.toLowerCase()
))
) {
entryJs += ';' + body;
all = '';
} else if (link && rRefStyle.test(lattrs) && rSrcHref.test(lattrs)) {
var href = RegExp.$2;
let file = resource.getFileByUrl(href);
if (!file) {
file = resource.getFileByUrl(
fis.util(path.join(path.dirname(tpl.release), href))
);
}
if (!file) {
file = mapping[href];
}
if (file) {
cssContents += '\n' + file.getContent();
file.skiped = true;
}
all = '';
} else if (style && sbody.trim()) {
cssContents += sbody;
all = '';
}
return all;
});
jsContents = jsContents.replace(rSourceMap, '');
jsContents = unicodeJs(jsContents);
let jsFile = fis.file(root, 'sdk.js');
jsFile.setContent(jsContents);
ret.pkg[jsFile.subpath] = jsFile;
cssContents = prefixCss(cssContents, '.amis-scope');
let cssFile = fis.file(root, 'sdk.css');
cssFile.setContent(cssContents);
ret.pkg[cssFile.subpath] = cssFile;
// tpl.setContent(contents);
caches[tpl.id] = contents;
};