提交 286e26a0 编写于 作者: Q qiang

fix: 小程序端解构组件样式兼容不同平台

上级 4f756ad0
...@@ -12,9 +12,9 @@ wx.createComponent({ ...@@ -12,9 +12,9 @@ wx.createComponent({
` `
} }
function generateCssCode (ownerName) { function generateCssCode (filename) {
return ` return `
@import './${ownerName}.wxss' @import "./${filename}"
` `
} }
...@@ -106,10 +106,15 @@ module.exports = { ...@@ -106,10 +106,15 @@ module.exports = {
const jsContent = generateJsCode(genCode(t.objectExpression(objectProperties), true)) const jsContent = generateJsCode(genCode(t.objectExpression(objectProperties), true))
state.files[jsFile] = jsContent state.files[jsFile] = jsContent
const cssFile = resourcePath.replace(ownerName + extname, componentName + '.wxss') try {
const cssContent = generateCssCode(ownerName) // TODO 使用 getPlatformExts 在单元测试报错,改从 state.options.platform 判断
const { getPlatformExts } = require('@dcloudio/uni-cli-shared')
let styleExtname = getPlatformExts().style
const styleFile = resourcePath.replace(ownerName + extname, componentName + styleExtname)
const styleContent = generateCssCode(ownerName + styleExtname)
state.files[cssFile] = cssContent state.files[styleFile] = styleContent
} catch (error) {}
if (!state.generic) { if (!state.generic) {
state.generic = [] state.generic = []
......
...@@ -2,7 +2,8 @@ const fs = require('fs') ...@@ -2,7 +2,8 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const { const {
removeExt, removeExt,
normalizePath normalizePath,
getPlatformExts
} = require('@dcloudio/uni-cli-shared') } = require('@dcloudio/uni-cli-shared')
const { const {
getComponentSet getComponentSet
...@@ -64,7 +65,7 @@ module.exports = function generateComponent (compilation) { ...@@ -64,7 +65,7 @@ module.exports = function generateComponent (compilation) {
const concatenatedModules = modules.filter(module => module.modules) const concatenatedModules = modules.filter(module => module.modules)
const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === uniPath).id const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === uniPath).id
const wxssImports = {} const styleImports = {}
Object.keys(assets).forEach(name => { Object.keys(assets).forEach(name => {
if (components.has(name.replace('.js', ''))) { if (components.has(name.replace('.js', ''))) {
...@@ -126,20 +127,21 @@ module.exports = function generateComponent (compilation) { ...@@ -126,20 +127,21 @@ module.exports = function generateComponent (compilation) {
assets[name].source = newSource assets[name].source = newSource
} }
} }
if (name.endsWith('.wxss')) { const styleExtname = getPlatformExts().style
if (name.endsWith(styleExtname)) {
// 移除部分含有错误引用的 wxss 文件 // 移除部分含有错误引用的 wxss 文件
let origSource = assets[name].source() let origSource = assets[name].source()
origSource = origSource.trim ? origSource.trim() : '' origSource = origSource.trim ? origSource.trim() : ''
const result = origSource.match(/^@import ["'](.+?)["']$/) const result = origSource.match(/^@import ["'](.+?)["']$/)
if (result) { if (result) {
const wxssPath = path.join(path.dirname(name), result[1]) const stylePath = path.join(path.dirname(name), result[1])
if (Object.keys(assets).includes(wxssPath)) { if (Object.keys(assets).includes(stylePath)) {
wxssImports[wxssPath] = wxssImports[wxssPath] || [] styleImports[stylePath] = styleImports[stylePath] || []
wxssImports[wxssPath].push(name) styleImports[stylePath].push(name)
} else { } else {
if (wxssImports[name]) { if (styleImports[name]) {
wxssImports[name].forEach(name => delete assets[name]) styleImports[name].forEach(name => delete assets[name])
delete wxssImports[name] delete styleImports[name]
} }
delete assets[name] delete assets[name]
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册