Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
a92f630
fix: outputs from useRelativePath
adriancmiranda Apr 12, 2017
e15e3a4
fix: Allowing .travis.yml file in git
adriancmiranda Apr 12, 2017
6c5f129
style: indentation
adriancmiranda Apr 12, 2017
509855d
fix: outputs
adriancmiranda Apr 12, 2017
1eb69cd
style: keeping only double quotes
adriancmiranda Apr 12, 2017
7dbce89
fix: textOutputPath parameter validation
adriancmiranda Apr 12, 2017
ffe9709
fix: normalized outputPath sep in windows os
adriancmiranda Apr 12, 2017
f2ebd86
docs: textOutputPath parameter
adriancmiranda Apr 12, 2017
d3ddff5
docs: textOutputPath parameter
adriancmiranda Apr 12, 2017
7cf4921
docs: textOutputPath parameter
adriancmiranda Apr 12, 2017
74c9e98
style: keeping only double quotes
adriancmiranda Apr 12, 2017
5c4d2a4
fix: public path from js files
adriancmiranda Apr 12, 2017
bdbb0e2
style: identation to tabs
adriancmiranda Apr 14, 2017
39b66b2
chore: removed vendor references
adriancmiranda Apr 15, 2017
4d1a43c
chore: changing textOutputPath to cssOutputPath
adriancmiranda Apr 15, 2017
3dde284
docs: changing textOutputPath to cssOutputPath
adriancmiranda Apr 15, 2017
584f009
fix: cssOutputPath defaults
adriancmiranda Apr 15, 2017
41516e5
fix: script path validation
adriancmiranda Apr 15, 2017
7b9ee2c
fix: cssOutputPath default validation
adriancmiranda Apr 15, 2017
02983bf
fix: type validation is needed to empty values
adriancmiranda Apr 15, 2017
23dffad
fix: typo
adriancmiranda Apr 15, 2017
9c62249
Merge branch 'master' into master
adriancmiranda Apr 15, 2017
17e608b
Merge branch 'master' into master
adriancmiranda Apr 16, 2017
564e84d
Merge branch 'master' of https://github.com/adriancmiranda/file-loader
adriancmiranda Apr 16, 2017
788a189
refactor: removes support for older node version
adriancmiranda Apr 18, 2017
02c8e32
refactor: strict mode
adriancmiranda Apr 18, 2017
f190ee5
docs: adding examples folder
adriancmiranda Apr 18, 2017
298a059
docs: sample improvement
adriancmiranda Apr 18, 2017
68ac897
doc: sample with dev mode
adriancmiranda Apr 18, 2017
db0596c
docs: sample with asset request from html
adriancmiranda Apr 18, 2017
1a0be65
docs: sample build fixed up
adriancmiranda Apr 19, 2017
6ea9f7e
docs: sample build fixed up
adriancmiranda Apr 19, 2017
26077c7
docs: added examples builder
adriancmiranda Apr 20, 2017
3bf2dac
test: examples compilation
adriancmiranda Apr 20, 2017
7fc4bd2
test: reporter
adriancmiranda Apr 20, 2017
c66447b
test: _module.issuer.context mockup
adriancmiranda Apr 20, 2017
3d136e3
test: webpack config mock-up
adriancmiranda Apr 21, 2017
57786c9
docs: update samples
adriancmiranda Apr 21, 2017
8e511d7
chore: recovering the file-loader icon
adriancmiranda Apr 21, 2017
8ff353f
test: cssOutputPath option
adriancmiranda Apr 21, 2017
60e3aa3
fix: output dirname rule should accept urls like ../foo/../bar
adriancmiranda Apr 21, 2017
e4b006e
fix: default publicPath
adriancmiranda May 8, 2017
1cf0f71
fix: default publicPath
adriancmiranda May 8, 2017
1cf6f67
Merge branch 'master' into master
adriancmiranda Jun 5, 2017
bf65289
feat: add option.cssOutputPath
adriancmiranda Jun 8, 2017
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

# dotfiles
.*
!.editorconfig
!.gitignore
!.travis.yml
Copy link
Contributor

Choose a reason for hiding this comment

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

Why to change these two?

Copy link
Member

@michael-ciniawsky michael-ciniawsky Apr 14, 2017

Choose a reason for hiding this comment

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

Yep these will be taken care of by #130

Copy link
Contributor Author

@adriancmiranda adriancmiranda Apr 14, 2017

Choose a reason for hiding this comment

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

I ignored all dotfiles in my last commit, but I forgot to allow .travis.yml too, not only the .gitignore.
About the .editorconfig it was my carelessness shouldn't be there yet (what to led change formatting), I've been working on another machine and it would configure wrong. Sorry!

*~
*#*
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ use: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images
}
```

`textOutputPath` also should be used (together with `useRelativePath` property) to define the output path from extracted file (i.e. css files) if you use them.

#### Filename template placeholders

* `[ext]` the extension of the resource
Expand Down Expand Up @@ -152,4 +154,4 @@ MIT
[cover-url]: https://coveralls.io/github/webpack-contrib/file-loader

[chat]: https://badges.gitter.im/webpack/webpack.svg
[chat-url]: https://gitter.im/webpack/webpack
[chat-url]: https://gitter.im/webpack/webpack
50 changes: 37 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,49 @@ module.exports = function(content) {
});

var outputPath = "";

var filePath = this.resourcePath;
if (config.useRelativePath) {
var issuerContext = this._module && this._module.issuer && this._module.issuer.context || context;
var relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join("/");
var relativePath = relativeUrl && path.dirname(relativeUrl) + "/";
if (~relativePath.indexOf("../")) {
outputPath = path.posix.join(outputPath, relativePath, url);
} else {
outputPath = relativePath + url;
}
url = relativePath + url;
} else if (config.outputPath) {
if (config.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = (
typeof config.outputPath === "function"
? config.outputPath(url)
: config.outputPath + url
);
}

if (config.useRelativePath) {
// Only the dirname is needed in this case.
outputPath = outputPath.replace(url, "");

// We have access only to entry point relationships. So we work with this relations.
var issuerContext = (this._module && this._module.issuer && this._module.issuer.context) || context;
var relativeUrl = issuerContext && path.relative(issuerContext, this.resourcePath);
var relativePath = relativeUrl && path.dirname(relativeUrl);
var outputDirname = relativePath.replace(/\.\.(\/|\\)/g, "").split(path.sep).join("/");

// Output path
// If the `outputDirname` is pointing to up in relation to the `outputPath`.
// We forced him to the webpack output path config. Even though it is empty.
if (outputDirname.indexOf(outputPath) !== 0) {
outputDirname = outputPath;
}
outputPath = path.join(outputDirname, url).split(path.sep).join("/");

// Public path
// Entry files doesn't pass through the `file-loader`.
// So we don't have access to the files context to compare with your assets context
// then we need to create and the same way force the `relativePath` to bundled files
// on the webpack output path config folder and manually the same with CSS file.
var output = this.options.output || {};
if (output.filename && path.extname(output.filename) === ".js") {
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

@adriancmiranda adriancmiranda Apr 15, 2017

Choose a reason for hiding this comment

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

It's wrong yet. I remembered, it has 'jsx' too, maybe another kind of file for scripts. Maybe we need only to know if the file has an extension.

relativePath = outputDirname;
} else if (toString.call(config.textOutputPath) === "[object String]") {
var outputPackageDirname = output.path.replace(this.options.context + path.sep, "");
var issuerOutput = path.join(context, outputPackageDirname, config.textOutputPath);
var assetOutput = path.join(context, outputPackageDirname, outputDirname);
relativePath = path.relative(issuerOutput, assetOutput);
}
url = path.join(relativePath, url).split(path.sep).join("/");
} else if (config.outputPath) {
url = outputPath;
} else {
outputPath = url;
Expand Down
221 changes: 109 additions & 112 deletions test/correct-filename.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,137 +3,134 @@ var path = require("path");
var fileLoader = require("../");

function run(resourcePath, query, content) {
content = content || new Buffer("1234");
var file = null;
var context = {
resourcePath: resourcePath,
query: "?" + query,
options: {
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
file = url;
}
};
content = content || new Buffer("1234");
var file = null;
var context = {
resourcePath: resourcePath,
query: "?" + query,
options: {
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
file = url;
}
};

var result = fileLoader.call(context, content)
var result = fileLoader.call(context, content)

return {
file: file,
result: result
}
return {
file: file,
result: result
}
}
function run_with_options(resourcePath,options, content) {
content = content || new Buffer("1234");
var file = null;
content = content || new Buffer("1234");
var file = null;

var context = {
resourcePath: resourcePath,
options: {
"fileLoader": options,
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
file = url;
}
};
var context = {
resourcePath: resourcePath,
options: {
"fileLoader": options,
context: "/this/is/the/context"
},
emitFile: function(url, content2) {
content2.should.be.eql(content);
file = url;
}
};

var result = fileLoader.call(context, content)
var result = fileLoader.call(context, content)

return {
file: file,
result: result
}
return {
file: file,
result: result
}
}

function test(excepted, resourcePath, query, content) {
run(resourcePath, query, content).file.should.be.eql(excepted);
run(resourcePath, query, content).file.should.be.eql(excepted);
}

describe("correct-filename", function() {
it("should process defaults correctly", function() {
test("81dc9bdb52d04dc20036dbd8313ed055.txt", "/file.txt", "");
test("81dc9bdb52d04dc20036dbd8313ed055.png", "/file.png", "");
test("81dc9bdb52d04dc20036dbd8313ed055.txt", "file.txt", "");
test("81dc9bdb52d04dc20036dbd8313ed055.bin", "", "");
});
it("should process name correctly", function() {
test("file.txt", "/file.txt", "name=[name].[ext]");
test("file.png", "/file.png", "name=[name].[ext]");
test("file.txt", "file.txt", "name=[name].[ext]");
test("file.bin", "", "name=[name].[ext]");
test("file", "/file.txt", "name=[name]");
test("81dc9bdb52d04dc20036dbd8313ed055", "/file.txt", "name=[hash]");
test("81dc9bdb52d04dc20036dbd8313ed055/file.txt", "/file.txt", "name=[hash]/[name].[ext]");
test("file.txt", "/this/is/the/context/file.txt", "name=[path][name].[ext]");
test("dir/file.txt", "/this/is/the/context/dir/file.txt", "name=[path][name].[ext]");
test("dir/sub/file.txt", "/this/is/the/context/dir/sub/file.txt", "name=[path][name].[ext]");
test("_/_/dir/sub/file.txt", "/this/is/dir/sub/file.txt", "name=[path][name].[ext]");
test("dir/sub/file.txt", "/this/is/dir/sub/file.txt", "name=[path][name].[ext]&context=/this/is");
});
it("should process hash correctly", function() {
test("d93591bdf7860e1e4ee2fca799911215.txt", "/file.txt", "", new Buffer("4321"));
});
it("should process hash options correctly", function() {
test("81dc9.txt", "/file.txt", "name=[hash:5].[ext]");
test("d4045.txt", "/file.txt", "name=[sha512:hash:5].[ext]");
test("1lQ3UNSdIS0c9dQ5brCZO1.txt", "/file.txt", "name=[hash:base64].[ext]");
test("caYJDUvUOiGAdDsiHKffIEj.txt", "/file.txt", "name=[hash:base52].[ext]");
test("sntmopgidsdqrofkjywoyldtiij.txt", "/file.txt", "name=[hash:base26].[ext]");
test("sntmopgids.txt", "/file.txt", "name=[hash:base26:10].[ext]");
});
it("should process defaults correctly", function() {
test("81dc9bdb52d04dc20036dbd8313ed055.txt", "/file.txt", "");
test("81dc9bdb52d04dc20036dbd8313ed055.png", "/file.png", "");
test("81dc9bdb52d04dc20036dbd8313ed055.txt", "file.txt", "");
test("81dc9bdb52d04dc20036dbd8313ed055.bin", "", "");
});
it("should process name correctly", function() {
test("file.txt", "/file.txt", "name=[name].[ext]");
test("file.png", "/file.png", "name=[name].[ext]");
test("file.txt", "file.txt", "name=[name].[ext]");
test("file.bin", "", "name=[name].[ext]");
test("file", "/file.txt", "name=[name]");
test("81dc9bdb52d04dc20036dbd8313ed055", "/file.txt", "name=[hash]");
test("81dc9bdb52d04dc20036dbd8313ed055/file.txt", "/file.txt", "name=[hash]/[name].[ext]");
test("file.txt", "/this/is/the/context/file.txt", "name=[path][name].[ext]");
test("dir/file.txt", "/this/is/the/context/dir/file.txt", "name=[path][name].[ext]");
test("dir/sub/file.txt", "/this/is/the/context/dir/sub/file.txt", "name=[path][name].[ext]");
test("_/_/dir/sub/file.txt", "/this/is/dir/sub/file.txt", "name=[path][name].[ext]");
test("dir/sub/file.txt", "/this/is/dir/sub/file.txt", "name=[path][name].[ext]&context=/this/is");
});
it("should process hash correctly", function() {
test("d93591bdf7860e1e4ee2fca799911215.txt", "/file.txt", "", new Buffer("4321"));
});
it("should process hash options correctly", function() {
test("81dc9.txt", "/file.txt", "name=[hash:5].[ext]");
test("d4045.txt", "/file.txt", "name=[sha512:hash:5].[ext]");
test("1lQ3UNSdIS0c9dQ5brCZO1.txt", "/file.txt", "name=[hash:base64].[ext]");
test("caYJDUvUOiGAdDsiHKffIEj.txt", "/file.txt", "name=[hash:base52].[ext]");
test("sntmopgidsdqrofkjywoyldtiij.txt", "/file.txt", "name=[hash:base26].[ext]");
test("sntmopgids.txt", "/file.txt", "name=[hash:base26:10].[ext]");
});
});

describe("publicPath option", function() {
it("should be supported", function() {
run("/file.txt", "publicPath=http://cdn/").result.should.be.eql(
'module.exports = "http://cdn/81dc9bdb52d04dc20036dbd8313ed055.txt";'
);
});
it("should be supported", function() {
run("/file.txt", "publicPath=http://cdn/").result.should.be.eql(
'module.exports = "http://cdn/81dc9bdb52d04dc20036dbd8313ed055.txt";'
);
});
});

describe("useRelativePath option", function() {
it("should be supported", function() {
run("/this/is/the/context/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/is/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/this/is/the/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
});
it("should be supported", function() {
run("/this/is/the/context/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/is/file.txt", "useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/this/is/the/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
run("/this/file.txt", "context=/&useRelativePath=true").result.should.be.eql(
'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
});
});
describe("outputPath function", function() {
it("should be supported", function() {
outputFunc = function(value) {
return("/path/set/by/func");

};
var options = {};
options.outputPath = outputFunc;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"/path/set/by/func\";'
);

});
it("should be ignored if you set useRelativePath", function() {
outputFunc = function(value) {
return("/path/set/by/func");

};
var options = {};
options.outputPath = outputFunc;
options.useRelativePath = true;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);

});
describe("outputPath function", function() {
it("should be supported", function() {
outputFunc = function(value) {
return("/path/set/by/func");
};
var options = {};
options.outputPath = outputFunc;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"/path/set/by/func\";'
);
});
it("should be ignored if you set useRelativePath", function() {
outputFunc = function(value) {
return("/path/set/by/func");
};
var options = {};
options.outputPath = outputFunc;
options.useRelativePath = true;
run_with_options("/this/is/the/context/file.txt", options).result.should.be.eql(
'module.exports = __webpack_public_path__ + \"81dc9bdb52d04dc20036dbd8313ed055.txt\";'
);
});
});