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

Transform absolute file paths to file URI scheme #8

Merged
merged 1 commit into from Mar 24, 2014

Conversation

jgoz
Copy link
Contributor

@jgoz jgoz commented Mar 18, 2014

This is a fix for the issues reported in #3, #7 and maybe #4.

  1. Phantomjs appears to have issues processing relative paths for referenced files (e.g., script/style refs) when its webpage is created via an absolute path. (I was unable to find a relevant issue on the phantomjs GH page.)
  2. gulp.src produces absolute paths in some cases/environments, which will trigger (1) when using this plugin. I'm not really clear on when this occurs, but it happens on my Win8.1/x64 machine.

This fix will determine if a file path is absolute and, if so, will transform it to the file URI scheme (e.g., file:///c:/path/to/test.html), which phantomjs can handle. Relative file paths remain untouched.

I also included a test case demonstrating the behaviour.


For anyone else affected by this issue, there is a workaround that you can add to your gulpfile. Fixing it here in the repository would obviously be preferable, but the following works in my environment:

var gulp = require("gulp");
var qunit = require("gulp-qunit");
var path = require("path"),
var util = require('util');
var Transform = require('stream').Transform;

util.inherits(FilePathToUrl, Transform);

function FilePathToUrl(options){
    Transform.call(this, options);
}

FilePathToUrl.prototype._transform = function(file, encoding, done) {
    file.path = 'file:///' + path.resolve(file.path).replace(/\\/g, '/');
    this.push(file);
    return done();
};

function toUrl() {
    return new FilePathToUrl({ objectMode: true });
}

gulp.task("test", function() {
    return gulp.src("./qunit/test-runner.html")
        .pipe(toUrl())
        .pipe(qunit());
});

On Windows, phantomjs appears to have issues processing relative paths for
referenced files (e.g., script/style refs) when its webpage is created via
an absolute path. I was unable to find a relevant issue on GitHub.

`gulp.src` produces absolute paths in some cases/environments, which will
trigger the above phantomjs issue when using this plugin.

This fix will determine if a file path is absolute and, if so, will
transform it to the file URI scheme (e.g., 'file:///c:/path/to/test.html')
which phantomjs *can* handle. Relative file paths remain untouched.

A test case demonstrating the behaviour was added.
jonkemp added a commit that referenced this pull request Mar 24, 2014
Transform absolute file paths to file URI scheme
@jonkemp jonkemp merged commit 4a5ab0f into jonkemp:master Mar 24, 2014
@jonkemp
Copy link
Owner

jonkemp commented Mar 24, 2014

I went ahead and merged this. I never had this issue on Mac or on Windows but it seemed some people did. I'm wondering if there is any case where the paths from gulp are not absolute though, in which case you wouldn't need to just pass file.path.

@jonkemp
Copy link
Owner

jonkemp commented Mar 24, 2014

Closes #7

@jgoz jgoz deleted the relative-path-win-issue branch March 24, 2014 17:57
@jgoz
Copy link
Contributor Author

jgoz commented Mar 24, 2014

I went ahead and merged this. I never had this issue on Mac or on Windows but it seemed some people did.

Thank you.

I'm wondering if there is any case where the paths from gulp are not absolute though, in which case you wouldn't need to just pass file.path.

I'm not sure I follow you here, but there are definitely cases where gulp paths are not absolute. All my machines use absolute paths for my projects, but since you were unable to reproduce this issue, your machine would have been using relative paths...

@jonkemp
Copy link
Owner

jonkemp commented Mar 24, 2014

Good to know then. Thanks!

@ikari-pl
Copy link

I love it! I had the same issue, worked around by starting a local express and hardcoding localhost URLs, but this solves it :) I can simplify the paths again.

ikari-pl added a commit to Schibsted-Tech-Polska/snd-api-js that referenced this pull request Apr 29, 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

Successfully merging this pull request may close these issues.

None yet

3 participants