Skip to content

Commit

Permalink
fix(cli): Bug css url loader (#4780)
Browse files Browse the repository at this point in the history
* 修复头条下 css 内background 未正确加载图片

* 修复头条下 css 内background 未正确加载图片

* 修复头条下 css 内background 未正确加载图片,修改版本号

* 修复头条下 css 内background 未正确加载图片,修改版本号
  • Loading branch information
tang-haibo authored and luckyadam committed Nov 26, 2019
1 parent 36e19b5 commit de11450
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/taro-cli/src/mini/compileStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function processStyleUseCssModule (styleObj: IStyleObj): Promise<an
}

async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
const { appPath, projectConfig, npmConfig, isProduction, buildAdapter } = getBuildData()
const { appPath, outputDir,projectConfig, npmConfig, isProduction, buildAdapter } = getBuildData()
const weappConf = Object.assign({}, projectConfig.weapp)
const publicPath = weappConf.publicPath
const useModuleConf = weappConf.module || {}
Expand Down Expand Up @@ -164,14 +164,33 @@ async function processStyleWithPostCSS (styleObj: IStyleObj): Promise<string> {
}
}



/***
* 修复小程序下css没有正确引用样式
* 当前位置只进行了文件hash转换并没有做文件copy操作
*/
if (publicPath && typeof url !== 'function') {
customUrlConf.config.url = (assets) => {

// 本地文件路径
if (/\./.test(assets.url)) {
const hashName = getHashName(assets.absolutePath)
assets.url = (/\/$/.test(publicPath) ? publicPath : publicPath + '/') + hashName
const hashName = getHashName(assets.absolutePath);
assets.url = (/\/$/.test(publicPath) ? publicPath : publicPath + '/') + hashName;

// 目前只在头条小程序复现,避免影响,只针对头条处理
if(buildAdapter === BUILD_TYPES.TT){
const outputFile = path.resolve(outputDir,`./${assets.url}`);
fs.ensureDirSync(path.dirname(outputFile));
fs.copySync(assets.absolutePath,outputFile);
// 头条下不支持 / ,修正头条css background路径
assets.url = assets.url.replace(/^[\/]/,'');
}
}

return assets.url
}

}

const cssUrlParseConf = {
Expand Down

0 comments on commit de11450

Please sign in to comment.