diff --git a/packages/uni-migration/__tests__/demo.js b/packages/uni-migration/__tests__/demo.js index 6e934ae49e1b55d327f8007882d1c551df1a554b..83fec6ba66a3699f0cb8e4e0e9cb5743e8f235e7 100644 --- a/packages/uni-migration/__tests__/demo.js +++ b/packages/uni-migration/__tests__/demo.js @@ -13,5 +13,7 @@ migrate('/Users/fxy/Downloads/wa-vantui_1.1/wxcomponents/vant', // } = require('../lib/mp-weixin/transform/template-transformer') // console.log(transformTemplate( -// `` +// `aasdf`, { +// filename: 'index' +// } // )) diff --git a/packages/uni-migration/lib/index.js b/packages/uni-migration/lib/index.js index 0ce4c3090d865a6aece1b679ee6534769598b1f6..4f30face83d2d728e6458862f4436bc294d34c23 100644 --- a/packages/uni-migration/lib/index.js +++ b/packages/uni-migration/lib/index.js @@ -7,6 +7,24 @@ const migraters = { 'mp-weixin': require('./mp-weixin') } +/** + * 先简单的 hack 一下,支持 vant 的 array.wxs + * @param {Object} src + * @param {Object} dest + */ +function hackVant(src, dest) { + if (src.indexOf('array.wxs') !== -1) { + fs.outputFileSync( + dest, + fs.readFileSync(src) + .toString() + .replace(`array.constructor === 'Array'`, 'Array.isArray(array)') + ) + return true + } + return false +} + module.exports = function migrate(input, out, options = { platform: 'mp-weixin' }) { @@ -24,9 +42,16 @@ module.exports = function migrate(input, out, options = { }) const styleExtname = options.extname.style assets.forEach(asset => { - const src = path.resolve(input, asset) - const dest = path.resolve(out, asset.replace(styleExtname, '.css')) - console.log(`copy: ${dest}`) - fs.copySync(src, dest) + if (typeof asset === 'string') { + const src = path.resolve(input, asset) + const dest = path.resolve(out, asset.replace(styleExtname, '.css')) + console.log(`copy: ${dest}`) + if (!hackVant(src, dest)) { + fs.copySync(src, dest) + } + } else { + console.log(`write: ${path.resolve(out, asset.path)}`) + fs.outputFileSync(path.resolve(out, asset.path), asset.content) + } }) } diff --git a/packages/uni-migration/lib/mp-weixin/transform/file-transformer.js b/packages/uni-migration/lib/mp-weixin/transform/file-transformer.js index 05f71be7ff82abb0806ab0e11b0a6270bc11e0ba..fb80622690ea2440ae5da92b0a7e3fa6d919085a 100644 --- a/packages/uni-migration/lib/mp-weixin/transform/file-transformer.js +++ b/packages/uni-migration/lib/mp-weixin/transform/file-transformer.js @@ -1,3 +1,5 @@ +const path = require('path') + const { transformJsonFile } = require('./json-transformer') @@ -23,12 +25,14 @@ module.exports = function transformFile(input, options) { filepath + templateExtname ] - const [usingComponentsCode] = transformJsonFile(filepath + '.json', deps) + const [jsCode] = transformJsonFile(filepath + '.json', deps) - const [templateCode, wxsCode = ''] = transformTemplateFile(filepath + templateExtname) + const [templateCode, wxsCode = '', wxsFiles = []] = transformTemplateFile(filepath + templateExtname, { + filename: path.basename(filepath) + }) const styleCode = transformStyleFile(filepath + styleExtname, options, deps) || '' - const scriptCode = transformScriptFile(filepath + '.js', usingComponentsCode, options, deps) + const scriptCode = transformScriptFile(filepath + '.js', jsCode, options, deps) return [ `