const PLUGIN_NAME = 'rollup-plugin-uniapp-cementing'; const CEMENTING_MODULE = '_uni_cementing_component.vue' import fs from 'fs'; import path from 'path'; const SEP = '****************************************************************' const DEBUG_FILE = 'debug.log' let logReset = function() { fs.writeFileSync( path.join(__dirname, DEBUG_FILE), `${SEP}\n* ${new Date().toLocaleString()}\n${SEP}\n` ) } let logTitle = function(title) { let msg = `${SEP}\n* ${title}\n${SEP}` log(msg) } let log = function(...args) { let msgs = [] for (let msg of args) { if (typeof msg === 'string') { msgs.push(msg) } else { msgs.push(JSON.stringify(msg, null, 2)) } } fs.writeFileSync( path.join(__dirname, DEBUG_FILE), `${msgs.join(' ')}\n`, { flag: 'a' } ) } import { createFilter } from '@rollup/pluginutils'; import MagicString from 'magic-string'; import { walk } from 'estree-walker'; function walkTo(node, matchers = []) { let cur = node let matcher = matchers.shift() while (matcher) { let found walk(cur, { enter: function(node, parent, prop, index) { if (found) return if (typeof matcher == 'function' && matcher(node) || matcher === node.type) { found = node } } }) if (!found) return cur = found if (matchers.length == 0) return cur matcher = matchers.shift() } } function cementingPlugin(options = {}) { const { platforms = ['app', /^mp(\-.*)?$/], include = ['*.vue', '*.nvue'], exclude, resolve = __dirname, debug, components = {} } = options let enabled = false for (let platform of platforms) { if (platform instanceof RegExp) { if (platform.exec(process.env['UNI_PLATFORM'])) { enabled = true } } else if (platform === process.env['UNI_PLATFORM']) { enabled = true } } if (!enabled) return false if (!debug) { logReset = logTitle = log = () => {} } let filter = createFilter(include, exclude, { resolve }) return { name: PLUGIN_NAME, enforce: 'pre', buildStart(options) { logReset() // log(options) }, // 对使用了动态组件 的组件进行静态化处理。 transform(code, id) { if (!filter(id)) return logTitle('transform: ' + id) // 检查组件代码是否使用了动态组件 let re = /(|<\/component>)/sg let m = re.exec(code) if (!m) return // log('使用了动态组件的代码:', code) // 把