node_modules

This commit is contained in:
saeid
2019-07-15 13:07:31 +04:30
parent d3735c6d30
commit ce1a3ebf9c
35713 changed files with 3639219 additions and 1 deletions
+73
View File
@@ -0,0 +1,73 @@
"use strict";
const path = require("path");
const os = require("os");
const fs = require("fs");
// A typical sass error looks like this
const SassError = { // eslint-disable-line no-unused-vars
message: "invalid property name",
column: 14,
line: 1,
file: "stdin",
status: 1
};
/**
* Enhances the sass error with additional information about what actually went wrong.
*
* @param {SassError} err
* @param {string} resourcePath
*/
function formatSassError(err, resourcePath) {
// Instruct webpack to hide the JS stack from the console
// Usually you're only interested in the SASS stack in this case.
err.hideStack = true;
// The file property is missing in rare cases.
// No improvement in the error is possible.
if (!err.file) {
return;
}
let msg = err.message;
if (err.file === "stdin") {
err.file = resourcePath;
}
// node-sass returns UNIX-style paths
err.file = path.normalize(err.file);
// The 'Current dir' hint of node-sass does not help us, we're providing
// additional information by reading the err.file property
msg = msg.replace(/\s*Current dir:\s*/, "");
err.message = getFileExcerptIfPossible(err) +
msg.charAt(0).toUpperCase() + msg.slice(1) + os.EOL +
" in " + err.file + " (line " + err.line + ", column " + err.column + ")";
}
/**
* Tries to get an excerpt of the file where the error happened.
* Uses err.line and err.column.
*
* Returns an empty string if the excerpt could not be retrieved.
*
* @param {SassError} err
* @returns {string}
*/
function getFileExcerptIfPossible(err) {
try {
const content = fs.readFileSync(err.file, "utf8");
return os.EOL +
content.split(os.EOL)[err.line - 1] + os.EOL +
new Array(err.column - 1).join(" ") + "^" + os.EOL +
" ";
} catch (err) {
// If anything goes wrong here, we don't want any errors to be reported to the user
return "";
}
}
module.exports = formatSassError;
+60
View File
@@ -0,0 +1,60 @@
"use strict";
const path = require("path");
const utils = require("loader-utils");
const matchModuleImport = /^~([^\/]+|@[^\/]+[\/][^\/]+)$/;
/**
* When libsass tries to resolve an import, it uses a special algorithm.
* Since the sass-loader uses webpack to resolve the modules, we need to simulate that algorithm. This function
* returns an array of import paths to try. The last entry in the array is always the original url
* to enable straight-forward webpack.config aliases.
*
* @param {string} url
* @returns {Array<string>}
*/
function importsToResolve(url) {
const request = utils.urlToRequest(url);
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
// @see https://github.com/webpack-contrib/sass-loader/issues/167
const ext = path.extname(request);
if (matchModuleImport.test(url)) {
return [request, url];
}
// libsass' import algorithm works like this:
// In case there is a file extension...
// - If the file is a CSS-file, do not include it all, but just link it via @import url().
// - The exact file name must match (no auto-resolving of '_'-modules).
if (ext === ".css") {
return [];
}
if (ext === ".scss" || ext === ".sass") {
return [request, url];
}
// In case there is no file extension...
// - Prefer modules starting with '_'.
// - File extension precedence: .scss, .sass, .css.
const basename = path.basename(request);
if (basename.charAt(0) === "_") {
return [
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
url
];
}
const dirname = path.dirname(request);
return [
`${ dirname }/_${ basename }.scss`, `${ dirname }/_${ basename }.sass`, `${ dirname }/_${ basename }.css`,
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
url
];
}
module.exports = importsToResolve;
+124
View File
@@ -0,0 +1,124 @@
"use strict";
const path = require("path");
const async = require("neo-async");
const formatSassError = require("./formatSassError");
const webpackImporter = require("./webpackImporter");
const normalizeOptions = require("./normalizeOptions");
const pify = require("pify");
const semver = require("semver");
let nodeSassJobQueue = null;
/**
* The sass-loader makes node-sass available to webpack modules.
*
* @this {LoaderContext}
* @param {string} content
*/
function sassLoader(content) {
const callback = this.async();
const isSync = typeof callback !== "function";
const self = this;
const resourcePath = this.resourcePath;
function addNormalizedDependency(file) {
// node-sass returns POSIX paths
self.dependency(path.normalize(file));
}
if (isSync) {
throw new Error("Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/sass-loader/issues/333");
}
const options = normalizeOptions(this, content, webpackImporter(
resourcePath,
pify(this.resolve.bind(this)),
addNormalizedDependency
));
// Skip empty files, otherwise it will stop webpack, see issue #21
if (options.data.trim() === "") {
callback(null, "");
return;
}
const render = getRenderFuncFromSassImpl(options.implementation || require("node-sass"));
render(options, (err, result) => {
if (err) {
formatSassError(err, this.resourcePath);
err.file && this.dependency(err.file);
callback(err);
return;
}
if (result.map && result.map !== "{}") {
result.map = JSON.parse(result.map);
// result.map.file is an optional property that provides the output filename.
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
delete result.map.file;
// The first source is 'stdin' according to node-sass because we've used the data input.
// Now let's override that value with the correct relative path.
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
// we know that this path is relative to process.cwd(). This is how node-sass works.
result.map.sources[0] = path.relative(process.cwd(), resourcePath);
// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
// This fixes an error on windows where the source-map module cannot resolve the source maps.
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
result.map.sourceRoot = path.normalize(result.map.sourceRoot);
result.map.sources = result.map.sources.map(path.normalize);
} else {
result.map = null;
}
result.stats.includedFiles.forEach(addNormalizedDependency);
callback(null, result.css.toString(), result.map);
});
}
/**
* Verifies that the implementation and version of Sass is supported by this loader.
*
* @param {Object} module
* @returns {Function}
*/
function getRenderFuncFromSassImpl(module) {
const info = module.info;
const components = info.split("\t");
if (components.length < 2) {
throw new Error("Unknown Sass implementation \"" + info + "\".");
}
const implementation = components[0];
const version = components[1];
if (!semver.valid(version)) {
throw new Error("Invalid Sass version \"" + version + "\".");
}
if (implementation === "dart-sass") {
if (!semver.satisfies(version, "^1.3.0")) {
throw new Error("Dart Sass version " + version + " is incompatible with ^1.3.0.");
}
return module.render.bind(module);
} else if (implementation === "node-sass") {
if (!semver.satisfies(version, "^4.0.0")) {
throw new Error("Node Sass version " + version + " is incompatible with ^4.0.0.");
}
// There is an issue with node-sass when async custom importers are used
// See https://github.com/sass/node-sass/issues/857#issuecomment-93594360
// We need to use a job queue to make sure that one thread is always available to the UV lib
if (nodeSassJobQueue === null) {
const threadPoolSize = Number(process.env.UV_THREADPOOL_SIZE || 4);
nodeSassJobQueue = async.queue(module.render.bind(module), threadPoolSize - 1);
}
return nodeSassJobQueue.push.bind(nodeSassJobQueue);
}
throw new Error("Unknown Sass implementation \"" + implementation + "\".");
}
module.exports = sassLoader;
+78
View File
@@ -0,0 +1,78 @@
"use strict";
const os = require("os");
const utils = require("loader-utils");
const cloneDeep = require("clone-deep");
const path = require("path");
const proxyCustomImporters = require("./proxyCustomImporters");
/**
* Derives the sass options from the loader context and normalizes its values with sane defaults.
*
* Please note: If loaderContext.query is an options object, it will be re-used across multiple invocations.
* That's why we must not modify the object directly.
*
* @param {LoaderContext} loaderContext
* @param {string} content
* @param {Function} webpackImporter
* @returns {Object}
*/
function normalizeOptions(loaderContext, content, webpackImporter) {
const options = cloneDeep(utils.getOptions(loaderContext)) || {};
const resourcePath = loaderContext.resourcePath;
options.data = options.data ? (options.data + os.EOL + content) : content;
// opt.outputStyle
if (!options.outputStyle && loaderContext.minimize) {
options.outputStyle = "compressed";
}
// opt.sourceMap
// Not using the `this.sourceMap` flag because css source maps are different
// @see https://github.com/webpack/css-loader/pull/40
if (options.sourceMap) {
// Deliberately overriding the sourceMap option here.
// node-sass won't produce source maps if the data option is used and options.sourceMap is not a string.
// In case it is a string, options.sourceMap should be a path where the source map is written.
// But since we're using the data option, the source map will not actually be written, but
// all paths in sourceMap.sources will be relative to that path.
// Pretty complicated... :(
options.sourceMap = path.join(process.cwd(), "/sass.map");
if ("sourceMapRoot" in options === false) {
options.sourceMapRoot = process.cwd();
}
if ("omitSourceMapUrl" in options === false) {
// The source map url doesn't make sense because we don't know the output path
// The css-loader will handle that for us
options.omitSourceMapUrl = true;
}
if ("sourceMapContents" in options === false) {
// If sourceMapContents option is not set, set it to true otherwise maps will be empty/null
// when exported by webpack-extract-text-plugin.
options.sourceMapContents = true;
}
}
// indentedSyntax is a boolean flag.
const ext = path.extname(resourcePath);
// If we are compiling sass and indentedSyntax isn't set, automatically set it.
if (ext && ext.toLowerCase() === ".sass" && "indentedSyntax" in options === false) {
options.indentedSyntax = true;
} else {
options.indentedSyntax = Boolean(options.indentedSyntax);
}
// Allow passing custom importers to `node-sass`. Accepts `Function` or an array of `Function`s.
options.importer = options.importer ? proxyCustomImporters(options.importer, resourcePath) : [];
options.importer.push(webpackImporter);
// `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
options.includePaths = options.includePaths || [];
options.includePaths.push(path.dirname(resourcePath));
return options;
}
module.exports = normalizeOptions;
+29
View File
@@ -0,0 +1,29 @@
"use strict";
/**
* Creates new custom importers that use the given `resourcePath` if libsass calls the custom importer with `prev`
* being 'stdin'.
*
* Why do we need this? We have to use the `data` option of node-sass in order to compile our sass because
* the `resourcePath` might not be an actual file on disk. When using the `data` option, libsass uses the string
* 'stdin' instead of a filename.
*
* We have to fix this behavior in order to provide a consistent experience to the webpack user.
*
* @param {function|Array<function>} importer
* @param {string} resourcePath
* @returns {Array<function>}
*/
function proxyCustomImporters(importer, resourcePath) {
return [].concat(importer).map((importer) => {
return function (url, prev, done) {
return importer.apply(
this, // eslint-disable-line no-invalid-this
Array.from(arguments)
.map((arg, i) => i === 1 && arg === "stdin" ? resourcePath : arg)
);
};
});
}
module.exports = proxyCustomImporters;
+72
View File
@@ -0,0 +1,72 @@
"use strict";
/**
* @name PromisedResolve
* @type {Function}
* @param {string} dir
* @param {string} request
* @returns Promise
*/
/**
* @name Importer
* @type {Function}
* @param {string} url
* @param {string} prev
* @param {Function<Error, string>} done
*/
const path = require("path");
const tail = require("lodash.tail");
const importsToResolve = require("./importsToResolve");
const matchCss = /\.css$/;
/**
* Returns an importer that uses webpack's resolving algorithm.
*
* It's important that the returned function has the correct number of arguments
* (based on whether the call is sync or async) because otherwise node-sass doesn't exit.
*
* @param {string} resourcePath
* @param {PromisedResolve} resolve
* @param {Function<string>} addNormalizedDependency
* @returns {Importer}
*/
function webpackImporter(resourcePath, resolve, addNormalizedDependency) {
function dirContextFrom(fileContext) {
return path.dirname(
// The first file is 'stdin' when we're using the data option
fileContext === "stdin" ? resourcePath : fileContext
);
}
function startResolving(dir, importsToResolve) {
return importsToResolve.length === 0 ?
Promise.reject() :
resolve(dir, importsToResolve[0])
.then(resolvedFile => {
// Add the resolvedFilename as dependency. Although we're also using stats.includedFiles, this might come
// in handy when an error occurs. In this case, we don't get stats.includedFiles from node-sass.
addNormalizedDependency(resolvedFile);
return {
// By removing the CSS file extension, we trigger node-sass to include the CSS file instead of just linking it.
file: resolvedFile.replace(matchCss, "")
};
}, () => startResolving(
dir,
tail(importsToResolve)
));
}
return (url, prev, done) => {
startResolving(
dirContextFrom(prev),
importsToResolve(url)
) // Catch all resolving errors, return the original file and pass responsibility back to other custom importers
.catch(() => ({ file: url }))
.then(done);
};
}
module.exports = webpackImporter;