diff options
Diffstat (limited to 'node_modules/babel-loader/lib')
-rw-r--r-- | node_modules/babel-loader/lib/fs-cache.js | 180 | ||||
-rw-r--r-- | node_modules/babel-loader/lib/index.js | 183 | ||||
-rw-r--r-- | node_modules/babel-loader/lib/resolve-rc.js | 26 | ||||
-rw-r--r-- | node_modules/babel-loader/lib/utils/exists.js | 13 | ||||
-rw-r--r-- | node_modules/babel-loader/lib/utils/read.js | 14 | ||||
-rw-r--r-- | node_modules/babel-loader/lib/utils/relative.js | 15 |
6 files changed, 431 insertions, 0 deletions
diff --git a/node_modules/babel-loader/lib/fs-cache.js b/node_modules/babel-loader/lib/fs-cache.js new file mode 100644 index 0000000..83cf61d --- /dev/null +++ b/node_modules/babel-loader/lib/fs-cache.js @@ -0,0 +1,180 @@ +"use strict"; + +/** + * Filesystem cache + * + * Given a file and a transform function, cache the result into files + * or retrieve the previously cached files if the given file is already known. + * + * @see https://github.com/babel/babel-loader/issues/34 + * @see https://github.com/babel/babel-loader/pull/41 + */ +var crypto = require("crypto"); +var mkdirp = require("mkdirp"); +var findCacheDir = require("find-cache-dir"); +var fs = require("fs"); +var os = require("os"); +var path = require("path"); +var zlib = require("zlib"); + +var defaultCacheDirectory = null; // Lazily instantiated when needed + +/** + * Read the contents from the compressed file. + * + * @async + * @params {String} filename + * @params {Function} callback + */ +var read = function read(filename, callback) { + return fs.readFile(filename, function (err, data) { + if (err) return callback(err); + + return zlib.gunzip(data, function (err, content) { + if (err) return callback(err); + + var result = {}; + + try { + result = JSON.parse(content); + } catch (e) { + return callback(e); + } + + return callback(null, result); + }); + }); +}; + +/** + * Write contents into a compressed file. + * + * @async + * @params {String} filename + * @params {String} result + * @params {Function} callback + */ +var write = function write(filename, result, callback) { + var content = JSON.stringify(result); + + return zlib.gzip(content, function (err, data) { + if (err) return callback(err); + + return fs.writeFile(filename, data, callback); + }); +}; + +/** + * Build the filename for the cached file + * + * @params {String} source File source code + * @params {Object} options Options used + * + * @return {String} + */ +var filename = function filename(source, identifier, options) { + var hash = crypto.createHash("md4"); + var contents = JSON.stringify({ + source: source, + options: options, + identifier: identifier + }); + + hash.update(contents); + + return hash.digest("hex") + ".json.gz"; +}; + +/** + * Handle the cache + * + * @params {String} directory + * @params {Object} params + * @params {Function} callback + */ +var handleCache = function handleCache(directory, params, callback) { + var source = params.source; + var options = params.options || {}; + var transform = params.transform; + var identifier = params.identifier; + var shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir(); + + // Make sure the directory exists. + mkdirp(directory, function (err) { + // Fallback to tmpdir if node_modules folder not writable + if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err); + + var file = path.join(directory, filename(source, identifier, options)); + + return read(file, function (err, content) { + var result = {}; + // No errors mean that the file was previously cached + // we just need to return it + if (!err) return callback(null, content); + + // Otherwise just transform the file + // return it to the user asap and write it in cache + try { + result = transform(source, options); + } catch (error) { + return callback(error); + } + + return write(file, result, function (err) { + // Fallback to tmpdir if node_modules folder not writable + if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err); + + callback(null, result); + }); + }); + }); +}; + +/** + * Retrieve file from cache, or create a new one for future reads + * + * @async + * @param {Object} params + * @param {String} params.directory Directory to store cached files + * @param {String} params.identifier Unique identifier to bust cache + * @param {String} params.source Original contents of the file to be cached + * @param {Object} params.options Options to be given to the transform fn + * @param {Function} params.transform Function that will transform the + * original file and whose result will be + * cached + * + * @param {Function<err, result>} callback + * + * @example + * + * cache({ + * directory: '.tmp/cache', + * identifier: 'babel-loader-cachefile', + * source: *source code from file*, + * options: { + * experimental: true, + * runtime: true + * }, + * transform: function(source, options) { + * var content = *do what you need with the source* + * return content; + * } + * }, function(err, result) { + * + * }); + */ + +module.exports = function (params, callback) { + var directory = void 0; + + if (typeof params.directory === "string") { + directory = params.directory; + } else { + if (defaultCacheDirectory === null) { + defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir(); + } + directory = defaultCacheDirectory; + } + + handleCache(directory, params, callback); +};
\ No newline at end of file diff --git a/node_modules/babel-loader/lib/index.js b/node_modules/babel-loader/lib/index.js new file mode 100644 index 0000000..f4d69df --- /dev/null +++ b/node_modules/babel-loader/lib/index.js @@ -0,0 +1,183 @@ +"use strict"; + +var babel = require("babel-core"); +var loaderUtils = require("loader-utils"); +var path = require("path"); +var cache = require("./fs-cache.js"); +var exists = require("./utils/exists"); +var relative = require("./utils/relative"); +var read = require("./utils/read"); +var resolveRc = require("./resolve-rc.js"); +var pkg = require("../package.json"); +var fs = require("fs"); + +/** + * Error thrown by Babel formatted to conform to Webpack reporting. + */ +function BabelLoaderError(name, message, codeFrame, hideStack, error) { + Error.call(this); + + this.name = "BabelLoaderError"; + this.message = formatMessage(name, message, codeFrame); + this.hideStack = hideStack; + this.error = error; + + Error.captureStackTrace(this, BabelLoaderError); +} + +BabelLoaderError.prototype = Object.create(Error.prototype); +BabelLoaderError.prototype.constructor = BabelLoaderError; + +var STRIP_FILENAME_RE = /^[^:]+: /; + +var formatMessage = function formatMessage(name, message, codeFrame) { + return (name ? name + ": " : "") + message + "\n\n" + codeFrame + "\n"; +}; + +var transpile = function transpile(source, options) { + var forceEnv = options.forceEnv; + var tmpEnv = void 0; + + delete options.forceEnv; + + if (forceEnv) { + tmpEnv = process.env.BABEL_ENV; + process.env.BABEL_ENV = forceEnv; + } + + var result = void 0; + try { + result = babel.transform(source, options); + } catch (error) { + if (forceEnv) restoreBabelEnv(tmpEnv); + if (error.message && error.codeFrame) { + var message = error.message; + var name = void 0; + var hideStack = void 0; + if (error instanceof SyntaxError) { + message = message.replace(STRIP_FILENAME_RE, ""); + name = "SyntaxError"; + hideStack = true; + } else if (error instanceof TypeError) { + message = message.replace(STRIP_FILENAME_RE, ""); + hideStack = true; + } + throw new BabelLoaderError(name, message, error.codeFrame, hideStack, error); + } else { + throw error; + } + } + var code = result.code; + var map = result.map; + var metadata = result.metadata; + + if (map && (!map.sourcesContent || !map.sourcesContent.length)) { + map.sourcesContent = [source]; + } + + if (forceEnv) restoreBabelEnv(tmpEnv); + + return { + code: code, + map: map, + metadata: metadata + }; +}; + +function restoreBabelEnv(prevValue) { + if (prevValue === undefined) { + delete process.env.BABEL_ENV; + } else { + process.env.BABEL_ENV = prevValue; + } +} + +function passMetadata(s, context, metadata) { + if (context[s]) { + context[s](metadata); + } +} + +module.exports = function (source, inputSourceMap) { + var _this = this; + + var filename = this.resourcePath; + + // Handle options + var loaderOptions = loaderUtils.getOptions(this) || {}; + var fileSystem = this.fs ? this.fs : fs; + var babelrcPath = null; + if (loaderOptions.babelrc !== false) { + babelrcPath = typeof loaderOptions.babelrc === "string" && exists(fileSystem, loaderOptions.babelrc) ? loaderOptions.babelrc : resolveRc(fileSystem, path.dirname(filename)); + } + + if (babelrcPath) { + this.addDependency(babelrcPath); + } + + var defaultOptions = { + metadataSubscribers: [], + inputSourceMap: inputSourceMap, + sourceRoot: process.cwd(), + filename: filename, + cacheIdentifier: JSON.stringify({ + "babel-loader": pkg.version, + "babel-core": babel.version, + babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null, + env: loaderOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development" + }) + }; + + var options = Object.assign({}, defaultOptions, loaderOptions); + + if (loaderOptions.sourceMap === undefined) { + options.sourceMap = this.sourceMap; + } + + if (options.sourceFileName === undefined) { + options.sourceFileName = relative(options.sourceRoot, options.filename); + } + + var cacheDirectory = options.cacheDirectory; + var cacheIdentifier = options.cacheIdentifier; + var metadataSubscribers = options.metadataSubscribers; + + delete options.cacheDirectory; + delete options.cacheIdentifier; + delete options.metadataSubscribers; + + if (cacheDirectory) { + var callback = this.async(); + return cache({ + directory: cacheDirectory, + identifier: cacheIdentifier, + source: source, + options: options, + transform: transpile + }, function (err) { + var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, + code = _ref.code, + map = _ref.map, + metadata = _ref.metadata; + + if (err) return callback(err); + + metadataSubscribers.forEach(function (s) { + return passMetadata(s, _this, metadata); + }); + + return callback(null, code, map); + }); + } + + var _transpile = transpile(source, options), + code = _transpile.code, + map = _transpile.map, + metadata = _transpile.metadata; + + metadataSubscribers.forEach(function (s) { + return passMetadata(s, _this, metadata); + }); + + this.callback(null, code, map); +};
\ No newline at end of file diff --git a/node_modules/babel-loader/lib/resolve-rc.js b/node_modules/babel-loader/lib/resolve-rc.js new file mode 100644 index 0000000..3910a81 --- /dev/null +++ b/node_modules/babel-loader/lib/resolve-rc.js @@ -0,0 +1,26 @@ +"use strict"; + +var path = require("path"); +var exists = require("./utils/exists"); + +module.exports = function find(fileSystem, start) { + var _arr = [".babelrc", "package.json"]; + + for (var _i = 0; _i < _arr.length; _i++) { + var fileName = _arr[_i]; + var file = path.join(start, fileName); + + if (exists(fileSystem, file)) { + if (fileName !== "package.json" || typeof require(file).babel === "object") { + return file; + } + } + } + + var up = path.dirname(start); + + // Reached root + if (up !== start) { + return find(fileSystem, up); + } +};
\ No newline at end of file diff --git a/node_modules/babel-loader/lib/utils/exists.js b/node_modules/babel-loader/lib/utils/exists.js new file mode 100644 index 0000000..7307a31 --- /dev/null +++ b/node_modules/babel-loader/lib/utils/exists.js @@ -0,0 +1,13 @@ +"use strict"; + +module.exports = function (fileSystem, filename) { + var exists = false; + + try { + exists = fileSystem.statSync(filename).isFile(); + } catch (err) { + if (err.code !== "ENOENT") throw err; + } + + return exists; +};
\ No newline at end of file diff --git a/node_modules/babel-loader/lib/utils/read.js b/node_modules/babel-loader/lib/utils/read.js new file mode 100644 index 0000000..978b879 --- /dev/null +++ b/node_modules/babel-loader/lib/utils/read.js @@ -0,0 +1,14 @@ +"use strict"; + +var path = require("path"); + +module.exports = function readBabelConfig(fileSystem, filename) { + if (path.basename(filename) === "package.json") { + var pkg = require(filename); + + return JSON.stringify(pkg.babel); + } + + // Webpack `fs` return Buffer + return fileSystem.readFileSync(filename).toString("utf8"); +};
\ No newline at end of file diff --git a/node_modules/babel-loader/lib/utils/relative.js b/node_modules/babel-loader/lib/utils/relative.js new file mode 100644 index 0000000..5bef704 --- /dev/null +++ b/node_modules/babel-loader/lib/utils/relative.js @@ -0,0 +1,15 @@ +"use strict"; + +var path = require("path"); + +module.exports = function relative(sourceRoot, filename) { + var rootPath = sourceRoot.replace(/\\/g, "/").split("/")[1]; + var fileRootPath = filename.replace(/\\/g, "/").split("/")[1]; + + // If the file is in a completely different root folder use the absolute path of file. + if (rootPath && rootPath !== fileRootPath) { + return filename; + } + + return path.relative(sourceRoot, filename); +};
\ No newline at end of file |