Skip to content

Commit

Permalink
修复微信新版本小程序开发工具编译策略不支持动态引用的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fan authored and fan committed Feb 24, 2022
1 parent 67d763d commit 0f85612
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
45 changes: 39 additions & 6 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,49 @@ class Build{
});
});

// 启用highlight扩展
// highlight扩展开关处理
let markdownIndex = path.join(_ts.outDir,'parse','markdown','index.js');
let markdownIndexStr;
if(!config.highlight.length){
let str = fs.readFileSync(markdownIndex,'utf8');
str = str.replace(/hljs =/g,'\/\/ hljs =');
fs.writeFileSync(markdownIndex,str);
markdownIndexStr = fs.readFileSync(markdownIndex,'utf8');
markdownIndexStr = markdownIndexStr.replace(/hljs =/g,'\/\/ hljs =');
fs.writeFileSync(markdownIndex,markdownIndexStr);
};

// 处理微信新版本不能动态require的问题(parse/markdown/index.js)
const regMarkdownPluginsStr = (()=>{
let result = ``;
[...config.markdown,...config.components].forEach(item => {
if(!/^audio-player|table|todogroup|img$/.test(item)){
result += `md.use(require('./plugins/${item}'));`
};
});
return result;
})();
if(regMarkdownPluginsStr && config.highlight.length){
markdownIndexStr = fs.readFileSync(markdownIndex,'utf8');
markdownIndexStr = markdownIndexStr.replace(/\/\/@regMarkdownPlugins/g,regMarkdownPluginsStr);
fs.writeFileSync(markdownIndex,markdownIndexStr);
};

// 处理微信新版本不能动态require的问题(parse/highlight/index.js)
let highlightIndex = path.join(_ts.outDir,'parse','highlight','index.js');
let highlightIndexStr;
let registerLanguageStr = (()=>{
let result = ``;
config.highlight.forEach(item => {
result += `hljs.registerLanguage('${item}', require('./languages/${item}').default);`;
});
return result;
})();
if(registerLanguageStr){
highlightIndexStr = fs.readFileSync(highlightIndex,'utf8');
highlightIndexStr = highlightIndexStr.replace(/\/\/@registerLanguage/g,registerLanguageStr);
fs.writeFileSync(highlightIndex,highlightIndexStr);
};

// 创建自定义组件配置
let obj = {component:true,usingComponents:{decode:`/towxml/decode`}};

config.components.forEach(item => {
obj.usingComponents[item] = `/towxml/${item}/${item}`
});
Expand All @@ -202,8 +234,9 @@ class Build{
});
};

console.log(`构建完成,请将『`,_ts.outDir,`』目录复制到小程序项目目录下`);
console.log(`构建完成,请将『`,_ts.outDir,`』目录复制到小程序项目目录下${!_ts.option.debug ? ',并改名为『towxml』' : ''}`);
}


// 输出模版
writeTpl(){
Expand Down
7 changes: 4 additions & 3 deletions dist/parse/highlight/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions dist/parse/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ const config = require('../../config'),
md = require('./markdown')(mdOption);

// 应用Markdown解析扩展,包括自定义组件(['sub','sup','ins','mark','emoji','todo','latex','yuml','echarts'])
[...config.markdown,...config.components].forEach(item => {
if(!/^audio-player|table|todogroup|img$/.test(item)){
md.use(require(`./plugins/${item}`));
};
});
// [...config.markdown,...config.components].forEach(item => {
// if(!/^audio-player|table|todogroup|img$/.test(item)){
// md.use(require(`./plugins/${item}`));
// };
// });
md.use(require('./plugins/sub'));md.use(require('./plugins/sup'));md.use(require('./plugins/ins'));md.use(require('./plugins/mark'));md.use(require('./plugins/emoji'));md.use(require('./plugins/todo'));md.use(require('./plugins/latex'));md.use(require('./plugins/yuml'));

// 定义emoji渲染规则
md.renderer.rules.emoji = (token,index)=>{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "towxml",
"version": "3.1.2",
"version": "3.2.2",
"description": "HTML、Markdown转WXML(WeiXin Markup Language)渲染库",
"main": "./dist/index.js",
"scripts": {
Expand Down
7 changes: 4 additions & 3 deletions parse/highlight/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const config = require('../../config'),
hljs = require('./highlight');
config.highlight.forEach(item => {
hljs.registerLanguage(item, require(`./languages/${item}`).default);
});
// config.highlight.forEach(item => {
// hljs.registerLanguage(item, require(`./languages/${item}`).default);
// });
//@registerLanguage

module.exports = hljs;
11 changes: 6 additions & 5 deletions parse/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ const config = require('../../config'),
md = require('./markdown')(mdOption);

// 应用Markdown解析扩展,包括自定义组件(['sub','sup','ins','mark','emoji','todo','latex','yuml','echarts'])
[...config.markdown,...config.components].forEach(item => {
if(!/^audio-player|table|todogroup|img$/.test(item)){
md.use(require(`./plugins/${item}`));
};
});
// [...config.markdown,...config.components].forEach(item => {
// if(!/^audio-player|table|todogroup|img$/.test(item)){
// md.use(require(`./plugins/${item}`));
// };
// });
//@regMarkdownPlugins

// 定义emoji渲染规则
md.renderer.rules.emoji = (token,index)=>{
Expand Down

0 comments on commit 0f85612

Please sign in to comment.