From 34fc9dd094db0cf09714d55cf8fabc3b3c81ffc2 Mon Sep 17 00:00:00 2001 From: 2betop <2betop.cn@gmail.com> Date: Wed, 15 Apr 2020 18:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20tinymce=20sdk=20css=20?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/embed-packager.js | 266 ++++++++++++++++++++++------------------ 1 file changed, 150 insertions(+), 116 deletions(-) diff --git a/build/embed-packager.js b/build/embed-packager.js index b9051e51..e03b1277 100644 --- a/build/embed-packager.js +++ b/build/embed-packager.js @@ -1,158 +1,192 @@ /* eslint-disable */ -var rLinkScript = /(||$))|(?:(\s*]*)>([\s\S]*?)<\/script>)|(?:\s*(]*?)(?:\/)?>)|(]*)>([\s\S]*?)<\/style>)))()?\n?/ig; +var rLinkScript = /(||$))|(?:(\s*]*)>([\s\S]*?)<\/script>)|(?:\s*(]*?)(?:\/)?>)|(]*)>([\s\S]*?)<\/style>)))()?\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|$)/ig; +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;}'; + 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; - 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)/)) - return sel; - else - return prefix + ' ' + sel; - } + function prefixSelector(sel) { + if (sel.match(/^@/)) 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)/)) 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 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); - }); + 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(); +module.exports = function(ret, pack, settings, opt) { + var root = fis.project.getProjectPath(); - var tpl = ret.pkg['/examples/sdk-placeholder.html']; - tpl.skiped = true; + 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; + 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; } - var mapping = {}; - var contents = tpl.getContent(); - var cssContents = ''; - var jsContents = ''; - var entryJs = ''; - var resource = tpl._resource; + if (script && !body.trim() && rSrcHref.test(attrs)) { + all = ''; + let src = RegExp.$2; + let file = resource.getFileByUrl(src); - var files = ret.pkg; - Object.keys(files).forEach(function (subpath) { - var file = files[subpath]; + if (!file) { + file = resource.getFileByUrl( + fis.util(path.join(path.dirname(tpl.release), src)) + ); + } - mapping[file.getUrl()] = file; - }); + if (!file) { + file = mapping[src]; + } - contents.replace(rLinkScript, function (all, comment, script, attrs, body, link, lattrs, style, sattrs, sbody, ignored) { - // 忽略注释。 - if (comment || ignored) { - return all; - } + if (file) { + file.skiped = true; + let contents = file.getContent(); - 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() { + 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(/\\/[^\\/]*$/, ''); } - ${contents.replace(/\"url\"\s*\:\s*('|")(\.\/.*)\1/g, function(_, quote, value) { - return `"url": d + ${quote}${value.substring(1)}${quote}`; + ${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 += 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); - jsContents = jsContents.replace(rSourceMap, ''); - jsContents = unicodeJs(jsContents); + if (!file) { + file = resource.getFileByUrl( + fis.util(path.join(path.dirname(tpl.release), href)) + ); + } - let jsFile = fis.file(root, 'sdk.js'); - jsFile.setContent(jsContents); - ret.pkg[jsFile.subpath] = jsFile; + if (!file) { + file = mapping[href]; + } - cssContents = prefixCss(cssContents, '.amis-scope'); - let cssFile = fis.file(root, 'sdk.css'); - cssFile.setContent(cssContents); - ret.pkg[cssFile.subpath] = cssFile; + if (file) { + cssContents += '\n' + file.getContent(); + file.skiped = true; + } + all = ''; + } else if (style && sbody.trim()) { + cssContents += sbody; + all = ''; + } - // tpl.setContent(contents); - caches[tpl.id] = contents; + 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; };