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

fix(karma-webpack): normalize paths (windows) #351

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/karma-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const SingleEntryDependency = require('webpack/lib/dependencies/SingleEntryDepen
let blocked = [];
let isBlocked = false;

const normalize = (file) => file.replace(/\\/g, '/');

const escapeRegExp = function(str) {
// See details here https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
Expand Down Expand Up @@ -268,7 +270,9 @@ Plugin.prototype.make = function(compilation, callback) {

const dep = new SingleEntryDependency(entry);

const filename = path.relative(this.basePath, file).replace(/\\/g, '/');
const filename = normalize(
path.relative(this.basePath, file).replace(/\\/g, '/')
);
const name = path.join(
path.dirname(filename),
path.basename(filename, path.extname(filename))
Expand Down Expand Up @@ -297,7 +301,7 @@ Plugin.prototype.make = function(compilation, callback) {
Plugin.prototype.readFile = function(file, callback) {
const middleware = this.middleware;
const optionsCount = this.optionsCount;

file = normalize(file);
const doRead = function() {
if (optionsCount > 1) {
async.times(
Expand Down Expand Up @@ -383,8 +387,10 @@ function createPreprocesor(/* config.basePath */ basePath, webpackPlugin) {
throw err;
}

const outputPath = webpackPlugin.outputs.get(filename);
file.path = path.join(basePath, outputPath);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How comes you removed this? It's needed to rewrite the asset path for karma to point to .js if the file extension was .ts for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, but with this, just for simple js file, the karma web server will not find the file and throw 404

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to rewrite the file path like the above, but failed, so what do you think here should to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rynclark , I tried to add this code back when testing with .ts file, but there's a problem, here path.join(basePath, outputPath) refers to the absolute path in the file system, so that 404, because .js doesn't exist at this basePath, so when replacing with path same aswebpackOptions.output.path = path.join(os.tmpdir(), '_karma_webpack_', indexPath, '/');, still cannot find the compiled .js file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move discussion about this into another PR and first of all fix the file path normalization issue as this seems to be critical?

Copy link
Contributor Author

@Teamop Teamop Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried with v3.0.3, still needs this L393 changes which normalizes the path again. I have opened a PR #354 for 3.0. @michael-ciniawsky

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR is ready, with this changs, I have no issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np, thanks @michael-ciniawsky

const outputPath = webpackPlugin.outputs.get(
normalize(filename.replace(/\\/g, '/'))
);
file.path = normalize(path.join(basePath, outputPath));

done(err, content && content.toString());
});
Expand Down