Skip to content

Commit

Permalink
feat: 修复wxss依赖不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Aug 1, 2019
1 parent 18f6c32 commit 88f7dc4
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/plugin/handleWXSSDep.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = class HandleWXSSDep {
mpb.hooks.beforeEmitFile.tapPromise('HandleWXSSDep', async (asset) => {
if (/\.wxss$/.test(asset.name)) {
const deps = [];
const distDeps = [];
try {
const root = postcss.parse(asset.contents);
root.walkAtRules('import', (rule) => {
Expand All @@ -30,35 +31,44 @@ module.exports = class HandleWXSSDep {
} else {
filePath = path.resolve(asset.dir, `./${src}`);
}

const root = asset.getMeta('root');
let outputPath = path.resolve(mpb.dest, path.relative(mpb.src, filePath));

if(filePath.includes('node_modules')) {
const root = asset.getMeta('root');
let outputPath = path.resolve(
mpb.dest,
path.relative(mpb.src, filePath)
);
if (filePath.includes('node_modules')) {
outputPath = this.mainPkgPathMap[filePath];
if(!outputPath) {
if (!outputPath) {
outputPath = path.join(
mpb.dest,
`./${root}`,
path
.relative(mpb.cwd, filePath)
.replace('node_modules', mpb.config.output.npm)
);
if(!root) {
if (!root) {
this.mainPkgPathMap[filePath] = outputPath;
}
}
};
return mpb.assetManager.addAsset(
filePath,
outputPath,
{
root,
source: asset.filePath
}
);
}
distDeps.push(outputPath);
return mpb.assetManager.addAsset(filePath, outputPath, {
root,
source: asset.filePath
});
})
);
let index = 0;
root.walkAtRules('import', (rule) => {
const libPath = rule.params.replace(/'|"/g, '');
const root = asset.getMeta('root');
rule.params = JSON.stringify(
path.relative(path.dirname(asset.outputFilePath), distDeps[index])
);
index++;
});
asset.contents = root.toString();
} catch (e) {
console.error(e);
}
Expand All @@ -69,4 +79,4 @@ module.exports = class HandleWXSSDep {
return asset;
});
}
};
};

0 comments on commit 88f7dc4

Please sign in to comment.