Closed Kristinita closed 3 years ago
@Kristinita resolve in version 0.1.6
Status: CONFIRMED :heavy_check_mark:
I upgraded posthtml-img-autosize to the version 0.1.6 → in Windows I got the same behavior as for Linux; see AppVeyor build.
Thanks.
@Scrum, excuse me, I said “confirmed” too early
Status: Another problem :exclamation:
posthtml-img-autosize 0.1.6 works incorrectly, if HTML files and images are in folders and/or subfolders.
As I wrote in the section 7 of original issue, “I don’t understand why posthtml-img-autosize has a root
option at all”.
HTML already use relative paths. For example, we have a line in our HTML:
<img alt="Nelia leaves" src="NeliaLeaves.jpg">
And a project where HTML files are located in various folders and subfolders.
If HTML file is KiraPostHTML.html
:
NeliaLeaves.jpg
should be in the root folder
elif KiraFolder/KiraPostHTML.html
:
correct path for image is KiraFolder/NeliaLeaves.jpg
else KiraFolder/KiraSubfolder/KiraPostHTML.html
:
KiraFolder/KiraSubfolder/NeliaLeaves.jpg
and so on.
In all three cases above posthtml-img-autosize tries to find the file NeliaLeaves.jpg
in the root folder.
You can see this configuration in the KiraPosthtmlImgAutosizePaths
branch of my demo/debugging repository.
The part of package.json
:
{
"devDependencies": {
"coffeescript": "^2.5.1",
"grunt": "^1.3.0",
"grunt-posthtml": "0.5.1",
"posthtml-img-autosize": "^0.1.6"
}
}
Gruntfile.coffee
(in example I builded the file object dynamically):
module.exports = (grunt) ->
grunt.loadNpmTasks 'grunt-posthtml'
grunt.initConfig
posthtml:
options:
use: [
require('posthtml-img-autosize')(processEmptySize: true)
]
target:
files: [
expand: true
cwd: '.'
src: 'KiraFolder/*.html'
dest: '.'
ext: '.output.html'
]
The part of .travis.yml
:
install:
- npm install -g grunt-cli posthtml-cli posthtml-img-autosize
- npm install
script:
- posthtml KiraFolder/KiraPostHTML.html -o KiraFolder/KiraImgAutosizeCLI.html -u posthtml-img-autosize --posthtml-img-autosize.processEmptySize true
- grunt posthtml --verbose
- cat KiraFolder/KiraImgAutosizeCLI.html
- cat KiraFolder/KiraPostHTML.output.html
KiraFolder/KiraPostHTML.html
:
<!doctype html>
<html>
<head>
<title>Kira Goddess!</title>
</head>
<body>
<img alt="Nelia leaves" src="NeliaLeaves.jpg">
</body>
</html>
Correct image path is SashaGruntDebugging/KiraFolder/NeliaLeaves.jpg
, not SashaGruntDebugging/NeliaLeaves.jpg
.
$ posthtml KiraFolder/KiraPostHTML.html -o KiraFolder/KiraImgAutosizeCLI.html -u posthtml-img-autosize --posthtml-img-autosize.processEmptySize true
Error: Promise rejected with value: "ENOENT: no such file or directory, open '/home/travis/build/Kristinita/SashaGruntDebugging/NeliaLeaves.jpg'"
at process.<anonymous> (/home/travis/.nvm/versions/node/v14.13.1/lib/node_modules/posthtml-cli/node_modules/hard-rejection/index.js:15:12)
at process.emit (events.js:314:20)
at processPromiseRejections (internal/process/promises.js:245:33)
at processTicksAndRejections (internal/process/task_queues.js:94:32)
The command "posthtml KiraFolder/KiraPostHTML.html -o KiraFolder/KiraImgAutosizeCLI.html -u posthtml-img-autosize --posthtml-img-autosize.processEmptySize true" exited with 1.
Running "posthtml:target" (posthtml) task
Verifying property posthtml.target exists in config…OK
Files: KiraFolder/KiraPostHTML.html -> KiraFolder/KiraPostHTML.output.html
Options: use=[null], singleTags=[], closingSingleTag="default", skipParse=null, sync=null
Reading KiraFolder/KiraPostHTML.html…OK
(node:6165) UnhandledPromiseRejectionWarning: ENOENT: no such file or directory, open '/home/travis/build/Kristinita/SashaGruntDebugging/NeliaLeaves.jpg'
(Use `node --trace-warnings …` to show where the warning was created)
(node:6165) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:6165) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Thanks.
As I wrote in the section 7 of original issue, “I don’t understand why posthtml-img-autosize has a root option at all”.
HTML already use relative paths. For example, we have a line in our HTML:
The build process itself has a root and all running build processes will rely on this root, accordingly plugins need to specify paths relative to the root of the build process
module.exports = (grunt) ->
grunt.loadNpmTasks 'grunt-posthtml'
grunt.initConfig
posthtml:
options:
use: [
require('posthtml-img-autosize')(
root: 'KiraFolder',
processEmptySize: true
)
]
target:
files: [
expand: true
cwd: '.'
src: 'KiraFolder/*.html'
dest: '.'
ext: '.output.html'
]
Status: Not fixed :crying_cat_face:
@Scrum, I don’t understand, how it can solve paths problem for real projects with multiple folders and subfolders. And I still think that the root
option — this is a wrong scenario.
If it doesn’t, please show the correct Gruntfile.coffee
for the MCVE below.
See this configuration in KiraPosthtmlImgAutosizeMultipleFiles branch of my repository for demonstrations and debugging.
Content of output
folder of my repository:
├───KiraAnotherFolder
│ KiraPostHTML.html
│
└───KiraFolder
│ KiraPostHTML.html
│ NeliaLeaves.jpg
│
└───KiraSubfolder
KiraPostHTML.html
Parts of the files:
KiraFolder/KiraPostHTML.html
:
<img alt="Nelia leaves" src="NeliaLeaves.jpg">
KiraFolder/KiraSubfolder/KiraPostHTML.html
:
<img alt="Nelia leaves" src="../NeliaLeaves.jpg">
KiraAnotherFolder/KiraPostHTML.html
:
<img alt="Nelia leaves" src="../KiraFolder/NeliaLeaves.jpg">
Paths in the src
attribute of all 3 files are correct, but posthtml-img-autosize doesn’t work as expected, see the Travis build.
Thanks.
@Scrum, I don’t understand, how it can solve paths problem for real projects with multiple folders and subfolders. And I still think that the
root
option — this is a wrong scenario.If it doesn’t, please show the correct
Gruntfile.coffee
for the MCVE below.
You can not specify the root directory, but then you need to specify the full path to the resource location.
Because your paths are relative to the root directory output
. This is how it will be right for all three files where used img
<img alt="Nelia leaves" src="KiraFolder/NeliaLeaves.jpg">
Status: Not fixed :crying_cat_face:
Because your paths are relative to the root directory
output
. This is how it will be right for all three files where usedimg
<img alt="Nelia leaves" src="KiraFolder/NeliaLeaves.jpg">
@Scrum, but browsers will not find the correct path to the image in this case. The image will not be displayed in browsers.
Thanks.
It seems to me that you have a rather confusing connection between templates and static resources.
I can suggest you structure your ecosystem to something like this
src
├── assets
│ ├── configs
│ ├── styles
│ ├── fonts
│ ├── images
│ ├── videos
│ ├── audios
│ └── javascripts
└── templates
├── base
│ └── base.html
├── coponents
│ └── button.html
├── layout
│ └── header.html
└── pages
└── index.html
Try to keep all static resources in one place assets
Status: Still not fixed :crying_cat_face:
Type: Counterargument 💬
The structure of files and folders in the folder output
:
│ KiraRoot.html
│
├───assets
│ NeliaLeaves.jpg
│
└───KiraFolder
│ KiraInFolder.html
│
└───KiraSubfolder
KiraInSubfolder.html
Parts of files:
KiraRoot.html
:<img alt="Nelia leaves" src="assets/NeliaLeaves.jpg">
KiraFolder/KiraInFolder.html
:<img alt="Nelia leaves" src="../assets/NeliaLeaves.jpg">
KiraFolder/KiraSubfolder/KiraInSubfolder.html
:<img alt="Nelia leaves" src="../../assets/NeliaLeaves.jpg">
posthtml-img-autosize will not work in this case.
Type: Questions :question:
src
attribute as in this MCVE?Thanks.
1. Will posthtml-img-autosize support relative paths in the `src` attribute as in this MCVE? 2. If not, can I hear the reasons for this solution? What’s wrong with such paths?
At the moment, relative paths are supported, but it should be borne in mind that they are resolved relative to the running process + root options and not relative to the html file
Type: Questions :question:
At the moment, relative paths are supported, but it should be borne in mind that they are resolved relative to the running process + root options and not relative to the html file
Yes, I understand the behavior at the moment, but my question is about something else. I’ll try to ask questions in other words:
src
attribute in the future?Thanks.
Yes, you can create a task feature where the root directory will be the context entry file.
Type: Question :question:
Yes, you can create a task feature where the root directory will be the context entry file.
Could you give an example of what you said (for example, for this MCVE)?
Thanks.
posthtml:
options:
use: [
require('posthtml-img-autosize')(
root: 'byEntry'
)
]
or new property ctx
posthtml:
options:
use: [
require('posthtml-img-autosize')(
ctx: 'byEntry'
)
]
This is an example of a feature that needs to be implemented. :)
Type: Question :question:
Are there any chances that this feature will be implemented?
Thanks.
Are there any chances that this feature will be implemented?
There are always chances
@Scrum, OK, we are waiting for this feature to be implemented.
Thanks.
This issue is still relevant for August, 2022.
I still can’t find any alternative that automatically adds height
and width
for all img
.
Thanks.
@Kristinita Hello, please create a new task, it is very difficult to understand the essence of the problem. a lot of comments.
Thank you.
@Scrum , OK, see #26.
Thanks.
1. Summary
posthtml-img-autosize doesn’t work for Windows.
I get errors as:
2. Reproducibility
I reproduce the problem for posthtml-cli and grunt-posthtml both on Windows.
On Linux I don’t have this problem.
I successfully use some another posthtml plugins (example), so I think that possibly that the problem is specific for the posthtml-img-autosize plugin.
3. Configuration
Operating system:
posthtml:
posthtml-img-autosize 0.1.5
4. MCVE
You can see the configuration in the
KiraPosthtmlAsync
branch of my demo/debugging repository.KiraPosthtml.html
:Gruntfile.coffee
:The part of
.travis.yml
— Ubuntu build:The part of
appveyor.yml
— Windows build:5. Behavior
5.1. Ubuntu — expected
Travis build:
posthtml-img-autosize successfully add width and height for CLI and Grunt commands.
5.2. Windows — bug
AppVeyor build:
Path problem for CLI and Grunt both.
6. Not helped
I tried:
6.1. CLI
Command-line arguments like:
-r '.'
--posthtml-img-autosize.root '\.'
6.2. Grunt
Options like:
root: '\.'
root: process.cwd()
7. root option
I don’t understand why posthtml-img-autosize has a
root
option at all. PostHTML works with HTML directly. In HTML, relative links to images look like:src="NeliaLeaves.jpg"
src="..\..\path\to\NeliaLeaves.jpg"
Is these cases a
root
option not really needed.Is it needed in any other cases?
Thanks.