Repository navigation
Conversation
…nents path are not rendered
|
Thanks for the detailed analysis — your root cause is correct. I'm going to close this PR and land the fix myself, without the auto-fallback. Here's why the fallback isn't the direction we want:
What I'll land instead is the loud failure, with const checkIsAbsolute = _.isAbsolute(value)
const usingPath = checkIsAbsolute ? path.join(rootPath, value) : path.join(path.dirname(componentPath), value)
if (_.readFile(`${usingPath}.json`)) {
// file path
const id = register(usingPath, key, cache, hasRegisterCache)
usingComponents[key] = id
} else if (isPathLike(value)) {
throw new Error(buildUsingComponentsError({key, value, usingPath, componentPath}))
}The message names the offending component, the path it resolved to, and asks the user to check the path or pass the correct Relative entries coming from It goes in as a small change in Thanks for the repro case and the screenshots — they made the failure mode easy to confirm. |
Root cause
A component declared in
usingComponentswith an absolute path is resolved againstrootPath:rootPathdefaults topath.dirname(componentPath)of the component passed tosimulate.load(). For a page stored in its own directory (page/index),/components/u-icon/indexis therefore looked up at<project>/page/components/u-icon/index— which does not exist — soregister()skips the entry and the raw path string is handed toj-component/exparser as a component id. exparser then creates an unknown custom element with an empty template, so the nested component silently renders as an empty host node:The same happens one level deeper:
<main-status>declares"u-icon": "/components/u-icon/index", so even when<main-status>renders,<u-icon>stays empty. That is the symptom reported in #34 ("组件嵌套组件时,内部组件没有渲染").Fix
src/index.js:resolveUsingComponentsPath()keeps the documented behaviour — relative paths are resolved from the component directory, absolute paths fromrootPath.rootPathwas not supplied by the user (its default value is only the loaded component's directory, so the project root is unknown), an absolute path that cannot be found there is searched again from the component directory upwards, nearest ancestor first.rootPath) is widened to the common ancestor of the old root and the resolved component; otherwise the official WXML compiler cannot compile a template located outsiderootPath(invalid template).rootPathis passed explicitly, and the relative-path branch is untouched.Reproducible case
test/comp12mirrors the issue:page/index.jsonuses/components/main-status/index,components/main-status/index.jsonuses/components/u-icon/index, and the page is loaded withoutrootPath:Before the fix (
src/index.jsfrommaster):comp.dom.outerHTMLof the same fixture onmaster:After the fix:
comp.dom.outerHTMLafter the fix:The regression case runs both with the official compiler and with
{compiler: 'simulate'}(jest.resetModules()in between), assertingsimulate.match()plus theinnerHTMLof both nested hosts.Verification
The screenshots below are generated by rendering the simulated tree (
comp.dom.outerHTML) into an HTML page together with the WXSS the simulator injects (<style>nodes collected from the jsdom document), then photographed with Playwright/Chrome at 1000x780, deviceScaleFactor 2.Closes #34