amis/scripts/embed-packager.js

217 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-08-05 11:11:08 +08:00
/* eslint-disable */
var rLinkScript =
/(<!(?:--)?\[[\s\S]*?<\!\[endif\](?:--)?>|<!--[\s\S]*?(?:-->|$))|(?:(\s*<script([^>]*)>([\s\S]*?)<\/script>)|(?:\s*(<link([^>]*?)(?:\/)?>)|(<style([^>]*)>([\s\S]*?)<\/style>)))(<!--ignore-->)?\n?/gi;
2019-08-05 11:11:08 +08:00
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;
2019-08-05 11:11:08 +08:00
var caches = {};
var createResource = fis.require('postpackager-loader/lib/resource.js');
function prefixCss(code, prefix) {
2020-04-15 18:21:49 +08:00
var cssAst = css.parse(code);
prefixNode(cssAst);
return css.stringify(cssAst);
2020-04-15 18:21:49 +08:00
function prefixSelector(sel) {
if (sel.match(/^@/)) return sel;
if (sel.match(/^:root/)) return sel;
2020-04-15 18:21:49 +08:00
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|\.AMISDebug|\.monaco-|\.vs-dark|\.hc-black|\.vs\b|\.cursor-|::|\.context-view|\.menubar|\.fullscreen|\.colorpicker-)/
)
)
return sel;
2020-04-15 18:21:49 +08:00
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);
2019-08-05 11:11:08 +08:00
}
2020-04-15 18:21:49 +08:00
}
2019-08-05 11:11:08 +08:00
}
function unicodeJs(str) {
return str.replace(
/([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[\u2019])/g,
function (_, value) {
return '\\u' + value.charCodeAt(0).toString(16);
}
);
2019-08-05 11:11:08 +08:00
}
module.exports = function (ret, pack, settings, opt) {
2020-04-15 18:21:49 +08:00
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 = [];
2020-04-15 18:21:49 +08:00
var jsContents = '';
var entryJs = '';
var resource = tpl._resource;
var files = ret.pkg;
Object.keys(files).forEach(function (subpath) {
2020-04-15 18:21:49 +08:00
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;
}
2019-08-05 11:11:08 +08:00
if (script && !body.trim() && rSrcHref.test(attrs)) {
all = '';
let src = RegExp.$2;
let file = resource.getFileByUrl(src);
2019-08-05 11:11:08 +08:00
if (!file) {
file = resource.getFileByUrl(
fis.util(path.join(path.dirname(tpl.release), src))
);
}
2019-08-05 11:11:08 +08:00
if (!file) {
file = mapping[src];
}
2019-08-05 16:54:39 +08:00
if (file) {
file.skiped = true;
let contents = file.getContent();
2019-08-05 16:54:39 +08:00
if (/_map\.js$/.test(file.subpath)) {
contents = `(function() {
2019-08-05 16:54:39 +08:00
var d = '';
try {
throw new Error()
} catch (e) {
2021-06-24 21:38:36 +08:00
d = (/((?:https?|file):.*?)\\n/.test(e.stack) && RegExp.$1).replace(/\\/[^\\/]*$/, '');
2019-08-05 16:54:39 +08:00
}
amis.host = d;
${contents.replace(
/\"url\"\s*\:\s*('|")(\.\/.*?)\1/g,
function (_, quote, value) {
return `"url": d + ${quote}${value.substring(1)}${quote}`;
}
)}
2019-08-05 16:54:39 +08:00
})()`;
}
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))
);
2019-08-05 11:11:08 +08:00
}
2020-04-15 18:21:49 +08:00
if (!file) {
file = mapping[href];
}
2020-04-15 18:21:49 +08:00
if (file) {
cssContents.push({
name: file.basename,
content: file.getContent()
});
file.skiped = true;
}
all = '';
} else if (style && sbody.trim()) {
cssContents.push({
name: 'inline',
content: sbody
});
all = '';
2020-04-15 18:21:49 +08:00
}
return all;
}
);
2019-08-05 11:11:08 +08:00
2020-04-15 18:21:49 +08:00
jsContents = jsContents.replace(rSourceMap, '');
jsContents = unicodeJs(jsContents);
2019-08-05 11:11:08 +08:00
2020-04-15 18:21:49 +08:00
let jsFile = fis.file(root, 'sdk.js');
jsFile.setContent(jsContents);
ret.pkg[jsFile.subpath] = jsFile;
2019-08-05 11:11:08 +08:00
// cssContents = prefixCss(cssContents, '.amis-scope');
// let cssFile = fis.file(root, 'sdk.css');
// cssFile.setContent(cssContents);
// ret.pkg[cssFile.subpath] = cssFile;
const themes = ['ang', 'cxd', 'dark', 'antd'];
themes.forEach(function (theme) {
const rest = themes.filter(a => a !== theme).map(item => item + '.scss');
let contents = cssContents
.filter(item => !rest.includes(item.name))
.map(item => item.content)
.join('\n');
contents = prefixCss(contents, '.amis-scope');
let cssFile = fis.file(root, (theme === 'cxd' ? 'sdk' : theme) + '.css');
cssFile.setContent(contents);
ret.pkg[cssFile.subpath] = cssFile;
});
2019-08-05 11:11:08 +08:00
2020-04-15 18:21:49 +08:00
// tpl.setContent(contents);
caches[tpl.id] = contents;
2019-08-05 11:11:08 +08:00
};