From aa644a5cb310b5c89f993842a3107a0cc889a396 Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Thu, 16 Feb 2017 13:32:58 +0100 Subject: [PATCH] Add explanation of stringifyRequest to README --- README.md | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5e4b36a..388ecac 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,41 @@ null -> {} ### `stringifyRequest` -Makes a request pretty and stringifies it. Absolute paths are replaced with relative ones. +Turns a request into a string that can be used inside a `require()` or `import()` call or an `import` statement. +Use it instead of `JSON.stringify(...)` if you're generating code inside a loader. -Use it instead of `JSON.stringify(...)` to build code of a `require(...)` call in a loader. +This function: -``` javascript -loaderUtils.stringifyRequest(this, require.resolve("./test")); -// = "../node_modules/some-loader/lib/test.js" +- resolves module requests into relative requests +- resolves absolute requests into relative requests if the request and the module are on the same hard drive +- replaces `\` with `/` if the request and the module are on the same hard drive +- won't change the path at all if the request and the module are on different hard drives +- applies `JSON.stringify` to the result + +```javascript +loaderUtils.stringifyRequest(this, "./test.js"); +// "\"./test.js\"" + +loaderUtils.stringifyRequest(this, "test"); +// "\"test\"" + +loaderUtils.stringifyRequest(this, "test/lib/index.js"); +// "\"test/lib/index.js\"" + +loaderUtils.stringifyRequest(this, "otherLoader?andConfig!test?someConfig"); +// "\"otherLoader?andConfig!test?someConfig\"" + +loaderUtils.stringifyRequest(this, require.resolve("test")); +// "\"../node_modules/some-loader/lib/test.js\"" + +loaderUtils.stringifyRequest(this, "C:\\module\\test.js"); +// "\"../../test.js\"" (on Windows, in case the module and the request are on the same drive) + +loaderUtils.stringifyRequest(this, "C:\\module\\test.js"); +// "\"C:\\module\\test.js\"" (on Windows, in case the module and the request are on different drives) + +loaderUtils.stringifyRequest(this, "\\network-drive\\test.js"); +// "\"\\\\network-drive\\\\test.js\"" (on Windows, in case the module and the request are on different drives) ``` ### `urlToRequest`