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

Unable to use absolute path to compiled source #25

Closed
skotchio opened this issue Oct 26, 2014 · 12 comments
Closed

Unable to use absolute path to compiled source #25

skotchio opened this issue Oct 26, 2014 · 12 comments
Assignees

Comments

@skotchio
Copy link

In your examples you use relative path to compiled source like the following:

//-<!-- build:js public/js/scripts.min.js -->
script(src='./src/js/script1.js')
script(src='./src/js/script2.js')
//-<!-- endbuild -->

But I need to use absolute path to scripts.min.js like the following:

//-<!-- build:js /public/js/scripts.min.js -->
script(src='./src/js/script1.js')
script(src='./src/js/script2.js')
//-<!-- endbuild -->

But if do as above then my scripts.min.js will be saved on my file system in root public directory instead my deployment path.

How could I set absolute path /public/js/scripts.min.js to my compiled script and reside it in my deployment folder instead root one?

@pgilad pgilad self-assigned this Oct 27, 2014
@skotchio
Copy link
Author

@pgilad could you help?

@pgilad
Copy link
Owner

pgilad commented Oct 28, 2014

I don't think there is a middle way here (without adding an option). If you want to go with absolute paths, that's fine, but you will need to specify the full path to output to. for example, in your case, that might be:
//-<!-- build:js /home/skotchio/projects/whatever/public/js/scripts.min.js -->

But if you go with relative paths, they will be relative to your project root. It's impossible to guess which option you want without adding an option for that (which I'm very reluctant to do, this plugin is already loaded with options). But if you must go with absolute path, just specify the entire path (from root of your system / ) to the compiled file.

@skotchio
Copy link
Author

So if I write //-<!-- build:js /home/skotchio/projects/whatever/public/js/scripts.min.js --> then which src javascript attribute I'll get in processed jade?

@pgilad
Copy link
Owner

pgilad commented Oct 28, 2014

Why are you required to use absolute paths? Is that because of the way your routing is setup?
In your case (try it) I believe the processed jade will have the absolute path as target.

@skotchio
Copy link
Author

My static processed by nginx by the following route:

location ~* \.(?:ico|css|js|gif|jpe?g|png) {
   root /var/www/myproject/static;
}

So I need my all javascript included in html looks like the following:

<html>
     <head>
            <script src="/js/scripts.min.js/"></script>
    </head>

@pgilad
Copy link
Owner

pgilad commented Oct 28, 2014

Ack I'm no nginx expert, but you can try url rewrite? so if someone hits with a public path you re-write it to your relative path (perhaps drop the non-needed part with regex).
Other than that, you can perhaps look for grunt plugins that will do src rewrite (something maybe like https://github.com/yoniholmes/grunt-text-replace but more sophisticated). Then you'll need to configure them to go over your target jade files and drop the absolute path that is specific to your system (and keep only the public/... parts)

@skotchio
Copy link
Author

@pgilad Many sites require to use domain name in their scripts like:

 <html>
   <head>
        <script src="http://www.mysite.com/js/scripts.min.js/"></script>
  </head>

What in this case?

@pgilad
Copy link
Owner

pgilad commented Oct 29, 2014

I think that's usually bad design, since you always load the scripts from your domain root. The ones you don't are from cdn (or external), but either way - you don't compile them.
So the solution is to drop the leading / and go for relative routing.

Another solution is to use something like https://www.npmjs.org/package/grunt-string-replace to search and replace the scripts to their absolute counterparts... That's some work, but it's possible

@skotchio
Copy link
Author

@pgilad your solusion about droping the leading / and go for relative routing is good but if I have the page with address http://www.mysite.com/user/profile/ and use relative path as you suggested:

<html>
<head>
    <script src="js/scripts.min.js/"></script>
</head> 
</html>

My scripts.min.js will be saved into user/profile/js folder which looks ugly.

@pgilad
Copy link
Owner

pgilad commented Oct 30, 2014

There are 2 things to note here:

  1. Where you target your scripts using this plugin (should be your output compiled directory for scripts)
  2. How you serve those scripts relies on how you configure your routing.

I suggest tweaking server routing to point at the location of the scripts, even if you use custom routing.
so mount the route: user/profile/js/:script to point to public/scripts/ and you should be fine.

Doing thing like re-writing urls in the build step, and using absolute urls is hard. It's possible but it's a lot of work.
Anyway my thoughts is that 1 plugin shouldn't be dealing with everything. As it is I think grunt-jade-usemin is too complex. I find that in the difficulty of some of the bug fixes.

If you feel that rewriting target urls is important, feel free to add a PR (with tests off course). But notice - it will have problems with things that rename file, such as the fileRev task.

@skotchio
Copy link
Author

@pgilad Thanks for the answer I think I'll resolve my problem by using re-writing urls. Thanks for the help and for the good grunt plugin

@pgilad
Copy link
Owner

pgilad commented Oct 30, 2014

👍

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

No branches or pull requests

2 participants