msurdi / frontend-dependencies

Copies node packages to a directory where your frontend tools will be able to find them
24 stars 5 forks source link

version range is not working. #13

Closed ve3 closed 5 years ago

ve3 commented 5 years ago

This is part of my package.json.

"frontendDependencies": {
    "target": "assets",
    "packages": {
      "jquery": {
        "version": "^3.0.0",
        "src": "{dist/jquery.min.js,dist/jquery.min.map}",
        "target": "assets/js/jquery"
      }
    }
  }

From semver calculator, it should working with jQuery 3.0.0 to 3.4.1 (current latest version). But I just got 3.0.0 always.

I have to change from ^3.0.0 to >=3.0.0 to make it working.

FelixFurtmayr commented 5 years ago

Hi ve3,

I guess this might be a problem with npm. The full npm command looks something like:

npm i --no-save --no-optional --production --prefix {modulePathPrefix} {npmPackageList}

You will see it complete on your command line, while executing the frontend dependecies script. Maybe you can figure out which part does not work on your system by copying the line from your terminal, manual executing and trying the same install in a test folder. Then trying again step by step omitting parameters till it works.

In your case the command might look like

npm i --no-save --no-optional --production --prefix /home/user/testfolder jquery@^3.0.0

Could you tell then what the problem is?

Regards Felix

ve3 commented 5 years ago

I got this from command.

[frontend-dependencies]: build the "npm install" command: npm i --no-save --no-optional --production --prefix node_modules/frontend-dependencies jquery@^3.0.0

I tried to execute this command npm i --no-save --no-optional --production --prefix node_modules/frontend-dependencies jquery@^3.0.0 by removed and change things and at last, I found that the version range must have quotes. See document.

Note that most version ranges must be put in quotes so that your shell will treat it as a single argument.

The modified command is npm i --no-save --no-optional --production --prefix node_modules/frontend-dependencies jquery@"^3.0.0" and it is working.

Maybe your index.js have to wrap the version range with quotes.

Another problem is when I use version range >=3.0.0 and run frontend-dependencies.cmd, there is a file 3.0.0 create at the same location as package.json. How to prevent this?

FelixFurtmayr commented 5 years ago

The solution would be to add some logic here.

For the first case something like this might do it:

        // add quotes on version if needed; e.g. for ^3.0.0 => jquery@"^3.0.0"
        if (pkg.version && pkg.version[0] === '^') pkg.version = '"' + pkg.version + '"';

I really don't like those version ranges, as they lead to a variance in your code base which can lead to problems. After I stopped using version ranges I never had problems again, as the changes may only be done by you, when you know about it and can test it. But that is my personal view. The question in this case would be how much custom logic we are willing to add, as this changes might lead to problems the more complex it becomes.

I don't like those npm features and also the idea of much custom logic. However - if you are willing to contribute (and this seems to be the case) and if it helps you: Fine with me.

@msurdi what do you think about this? Should we implement this?

ve3 commented 5 years ago

The version range, npm, and your package are the reasons that I'm going to use npm for manage packages like jquery, bootstrap, etc.. It is easy to update them all with just a command and version range is necessary for this. If no version range, I think I will just go back to the old way.... download and paste (jquery, bootstrap, etc) one by one.

FelixFurtmayr commented 5 years ago

ok, ok. Seems almost like a threat to me. I really don't want you to do the stuff manual ;-)

For the second problem: The file seems to be an npm log file and has for me a content like

+ normalize.css@4.2.0
+ jquery@3.4.1
updated 2 packages and audited 3 packages in 0.963s
found 0 vulnerabilities

It is created also when executing the npm command when a version range is set; I used:

npm i --no-save --no-optional --production --prefix node_modules/frontend-dependencies jquery@>=3.1.0 normalize.css@4.2.0

And as ve3 said, no package.json (not sure if that is a problem) image

Adding a package.json removes the error, but still the file remains. I have not found anyting about the file so far. It allways appears when a version range is added.

FelixFurtmayr commented 5 years ago

ah too easy: the problem is, that we write the output of npm into a file

FelixFurtmayr commented 5 years ago

ok, there is a very simple solution: we will just add the quotes all the time. no extra logic and I see no disadvantage. this will do the trick.

      pkgName += ('@"' + pkg.version + '"');
ve3 commented 5 years ago

Yes, thank you very much.