Skip to content

Commit

Permalink
fix: 修复在转换用 MIP 组件时,插入 JS 引用
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexb committed Mar 18, 2018
1 parent 06d4e0f commit 582bae1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/modules/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

'use strict';

const util = require('../util');

exports.process = function ($, config) {
const html = $('body')
.html()
.replace(/<audio([^>]*)>([\s\S]*?)<\/audio>/ig, (match, attrs, tags) => {
return`<mip-audio${attrs}>${tags}</mip-audio>`;
});
$('body').html(html);

if ($('mip-audio').length) {
util.insertExtensions($, 'mip-audio');
}
};
6 changes: 6 additions & 0 deletions lib/modules/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const util = require('../util');

exports.process = function ($, config) {
// 先替换属性
$('body form').each((index, el) => {
Expand All @@ -21,4 +23,8 @@ exports.process = function ($, config) {
return`<mip-form${attrs}>${tags}</mip-form>`;
});
$('body').html(html);

if ($('mip-form').length) {
util.insertExtensions($, 'mip-form');
}
};
2 changes: 1 addition & 1 deletion lib/modules/mip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const isCDNpath = require('../util').isCDNpath;

// 在删除无用 <script> 之后处理
exports.level = 3;
exports.level = -3;

exports.process = $ => {
// 注入 mip 属性
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/remove-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

const isCDN = require('../util').isCDN;

exports.level = -5;

exports.process = $ => {
$('script').each((index, el) => {
const $el = $(el);
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ exports.isCDN = str => /^(https:)?\/\/(c\.mipcdn\.com|mipcache\.bdstatic\.com)(\
* @return {string}
*/
exports.random = () => `m${cryptoRandomString(5)}`;

/**
* 注入组件 JS 脚本
*
* @param {Object} $ 文档对象
* @param {string} name 组件名
*/
exports.insertExtensions = ($, name) => {
const $script = $('body script[src]')
.filter((index, el) => exports.isCDNpath($(el).attr('src'), `/static/v1/${name}/${name}.js`));

if (!$script.length) {
$('body').append(`<script src="https://c.mipcdn.com/static/v1/${name}/${name}.js"></script>`);
}
};

0 comments on commit 582bae1

Please sign in to comment.