Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use classes do not allow for paths outside of current folder #114

Closed
struct78 opened this issue Nov 19, 2019 · 7 comments · Fixed by #117
Closed

Use classes do not allow for paths outside of current folder #114

struct78 opened this issue Nov 19, 2019 · 7 comments · Fixed by #117
Assignees
Labels

Comments

@struct78
Copy link

A couple of things:

I think the update from 3.2.7 to 3.2.8 should have had a major version bump.

We have our project structure set up in a way that mimics the use of Java classes. For example, we have a simple file loader that outputs SVG code inline so it can be styled.

<sly data-sly-use.svg="${'com.companyname.project.InlineSvgUtility' @ svgPath=properties.logo}">
    ${logo.svgItem @ context='unsafe'}
</sly>

We then used the withUseDirectory method of the runtime to load modules from the 'modules/' folder in the root e.g.

const fse = require('fs-extra')
const path = require('path')

class InlineSvgUtility {
  async use() {
    const assetsPath = 'assets/icons/'

    if (!this.svgPath) {
      return {
        svgItem: null,
      }
    }

    const data = await fse.readFile(path.join(assetsPath, this.svgPath), 'utf-8')

    return {
      svgItem: data,
    }
  }
}

module.exports = InlineSvgUtility

So this sits in src/modules/com.companyname.project.InlineSvgUtility.js, and was working happily. In 3.2.8 the withUseDirectory seems to effectively do nothing.

This line seems to be the cause.

6d00698#diff-59e501da46af5081071db46e7c50a748R243

If the path doesn't start with './' (e.g. '../'), it will attempt to resolve files relative to the folder the Compiler script is in, rather than the baseDir. For example, this will throw a "MODULE_NOT_FOUND" error.

<sly data-sly-use.svg="${'../modules/com.companyname.project.InlineSvgUtility' @ svgPath=properties.logo}">
    ${logo.svgItem @ context='unsafe'}
</sly>

I can fix it by traversing up 6 or so folders out of the node_modules, but that's not ideal.

<sly data-sly-use.svg="${'../../../../../../modules/com.companyname.project.InlineSvgUtility' @ svgPath=properties.logo}">
    ${logo.svgItem @ context='unsafe'}
</sly>
@tripodsan
Copy link
Contributor

I think the update from 3.2.7 to 3.2.8 should have had a major version bump.

yes. you're probably right.

the use classes now follow the nodejs require semantic. if the name doesn't start with a ./, it is considered a global module, otherwise it is relative to the htl file. I think the bug here is that it doesn't check for ../. so you could try to reference it with ./../modules/com.companyname.project.InlineSvgUtility.js, depending where your .htl is.

another possibility could be to add the src/modules path to the module path when running your script. maybe you can play around with NODE_PATH.

@struct78 any chance our source code is public?

@struct78
Copy link
Author

@tripodsan Unfortunately not. I'll try and make a public repo with some sample code if I get the time.

@struct78
Copy link
Author

@tripodsan Here is some sample code that closely matches our setup https://github.com/struct78/htlengine-issue-114

@tripodsan
Copy link
Contributor

thanks, I will look into it as soon as I have time....

@tripodsan tripodsan self-assigned this Nov 28, 2019
@tripodsan
Copy link
Contributor

ok, I see the problem. you are using the compileToFunction() which fakes the module and uses the require of the compiler.

@tripodsan
Copy link
Contributor

nodejs 10.12 added: https://nodejs.org/docs/latest-v10.x/api/modules.html#modules_module_createrequirefrompath_filename

which would be an easy way to add the useDirectory to the module paths...

adobe-bot pushed a commit that referenced this issue Feb 3, 2020
## [3.3.1](v3.3.0...v3.3.1) (2020-02-03)

### Bug Fixes

* **compiler:** allow to specify require when using compileToFunction() ([#117](#117)) ([fb901fd](fb901fd)), closes [#114](#114)
@adobe-bot
Copy link

🎉 This issue has been resolved in version 3.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants