update
This commit is contained in:
+28
-32
@@ -23,31 +23,40 @@ const zlib = require("zlib");
|
||||
|
||||
const crypto = require("crypto");
|
||||
|
||||
const mkdirpOrig = require("mkdirp");
|
||||
|
||||
const findCacheDir = require("find-cache-dir");
|
||||
|
||||
const promisify = require("pify");
|
||||
const {
|
||||
promisify
|
||||
} = require("util");
|
||||
|
||||
const transform = require("./transform"); // Lazily instantiated when needed
|
||||
|
||||
|
||||
let defaultCacheDirectory = null;
|
||||
let hashType = "md4"; // use md5 hashing if md4 is not available
|
||||
|
||||
try {
|
||||
crypto.createHash(hashType);
|
||||
} catch (err) {
|
||||
hashType = "md5";
|
||||
}
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
const writeFile = promisify(fs.writeFile);
|
||||
const gunzip = promisify(zlib.gunzip);
|
||||
const gzip = promisify(zlib.gzip);
|
||||
const mkdirp = promisify(mkdirpOrig);
|
||||
|
||||
const makeDir = require("make-dir");
|
||||
/**
|
||||
* Read the contents from the compressed file.
|
||||
*
|
||||
* @async
|
||||
* @params {String} filename
|
||||
* @params {Boolean} compress
|
||||
*/
|
||||
|
||||
const read =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
|
||||
const read = /*#__PURE__*/function () {
|
||||
var _ref = _asyncToGenerator(function* (filename, compress) {
|
||||
const data = yield readFile(filename + (compress ? ".gz" : ""));
|
||||
const content = compress ? yield gunzip(data) : data;
|
||||
@@ -63,13 +72,12 @@ function () {
|
||||
*
|
||||
* @async
|
||||
* @params {String} filename
|
||||
* @params {Boolean} compress
|
||||
* @params {String} result
|
||||
*/
|
||||
|
||||
|
||||
const write =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
const write = /*#__PURE__*/function () {
|
||||
var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
|
||||
const content = JSON.stringify(result);
|
||||
const data = compress ? yield gzip(content) : content;
|
||||
@@ -91,7 +99,7 @@ function () {
|
||||
|
||||
|
||||
const filename = function (source, identifier, options) {
|
||||
const hash = crypto.createHash("md4");
|
||||
const hash = crypto.createHash(hashType);
|
||||
const contents = JSON.stringify({
|
||||
source,
|
||||
options,
|
||||
@@ -108,9 +116,7 @@ const filename = function (source, identifier, options) {
|
||||
*/
|
||||
|
||||
|
||||
const handleCache =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
const handleCache = /*#__PURE__*/function () {
|
||||
var _ref3 = _asyncToGenerator(function* (directory, params) {
|
||||
const {
|
||||
source,
|
||||
@@ -130,7 +136,7 @@ function () {
|
||||
const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
|
||||
|
||||
try {
|
||||
yield mkdirp(directory);
|
||||
yield makeDir(directory);
|
||||
} catch (err) {
|
||||
if (fallback) {
|
||||
return handleCache(os.tmpdir(), params);
|
||||
@@ -166,38 +172,28 @@ function () {
|
||||
*
|
||||
* @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.cacheDirectory Directory to store cached files
|
||||
* @param {String} params.cacheIdentifier Unique identifier to bust cache
|
||||
* @param {Boolean} params.cacheCompression Whether compressing cached files
|
||||
* @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
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* cache({
|
||||
* directory: '.tmp/cache',
|
||||
* identifier: 'babel-loader-cachefile',
|
||||
* const result = await cache({
|
||||
* cacheDirectory: '.tmp/cache',
|
||||
* cacheIdentifier: 'babel-loader-cachefile',
|
||||
* cacheCompression: false,
|
||||
* 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 =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
module.exports = /*#__PURE__*/function () {
|
||||
var _ref4 = _asyncToGenerator(function* (params) {
|
||||
let directory;
|
||||
|
||||
|
||||
+22
-8
@@ -67,7 +67,7 @@ function loader(_x, _x2, _x3) {
|
||||
function _loader() {
|
||||
_loader = _asyncToGenerator(function* (source, inputSourceMap, overrides) {
|
||||
const filename = this.resourcePath;
|
||||
let loaderOptions = loaderUtils.getOptions(this) || {};
|
||||
let loaderOptions = loaderUtils.getOptions(this);
|
||||
validateOptions(schema, loaderOptions, {
|
||||
name: "Babel loader"
|
||||
});
|
||||
@@ -128,7 +128,7 @@ function _loader() {
|
||||
|
||||
const programmaticOptions = Object.assign({}, loaderOptions, {
|
||||
filename,
|
||||
inputSourceMap: inputSourceMap || undefined,
|
||||
inputSourceMap: inputSourceMap || loaderOptions.inputSourceMap,
|
||||
// Set the default sourcemap behavior based on Webpack's mapping flag,
|
||||
// but allow users to override if they want.
|
||||
sourceMaps: loaderOptions.sourceMaps === undefined ? this.sourceMap : loaderOptions.sourceMaps,
|
||||
@@ -146,9 +146,13 @@ function _loader() {
|
||||
|
||||
if (!babel.loadPartialConfig) {
|
||||
throw new Error(`babel-loader ^8.0.0-beta.3 requires @babel/core@7.0.0-beta.41, but ` + `you appear to be using "${babel.version}". Either update your ` + `@babel/core version, or pin you babel-loader version to 8.0.0-beta.2`);
|
||||
}
|
||||
} // babel.loadPartialConfigAsync is available in v7.8.0+
|
||||
|
||||
const config = babel.loadPartialConfig(injectCaller(programmaticOptions, this.target));
|
||||
|
||||
const {
|
||||
loadPartialConfigAsync = babel.loadPartialConfig
|
||||
} = babel;
|
||||
const config = yield loadPartialConfigAsync(injectCaller(programmaticOptions, this.target));
|
||||
|
||||
if (config) {
|
||||
let options = config.options;
|
||||
@@ -194,12 +198,22 @@ function _loader() {
|
||||
});
|
||||
} else {
|
||||
result = yield transform(source, options);
|
||||
} // TODO: Babel should really provide the full list of config files that
|
||||
// were used so that this can also handle files loaded with 'extends'.
|
||||
} // Availabe since Babel 7.12
|
||||
// https://github.com/babel/babel/pull/11907
|
||||
|
||||
|
||||
if (typeof config.babelrc === "string") {
|
||||
this.addDependency(config.babelrc);
|
||||
if (config.files) {
|
||||
config.files.forEach(configFile => this.addDependency(configFile));
|
||||
} else {
|
||||
// .babelrc.json
|
||||
if (typeof config.babelrc === "string") {
|
||||
this.addDependency(config.babelrc);
|
||||
} // babel.config.js
|
||||
|
||||
|
||||
if (config.config) {
|
||||
this.addDependency(config.config);
|
||||
}
|
||||
}
|
||||
|
||||
if (result) {
|
||||
|
||||
+5
-5
@@ -6,15 +6,15 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
|
||||
|
||||
const babel = require("@babel/core");
|
||||
|
||||
const promisify = require("pify");
|
||||
const {
|
||||
promisify
|
||||
} = require("util");
|
||||
|
||||
const LoaderError = require("./Error");
|
||||
|
||||
const transform = promisify(babel.transform);
|
||||
|
||||
module.exports =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
module.exports = /*#__PURE__*/function () {
|
||||
var _ref = _asyncToGenerator(function* (source, options) {
|
||||
let result;
|
||||
|
||||
@@ -26,7 +26,7 @@ function () {
|
||||
|
||||
if (!result) return null; // We don't return the full result here because some entries are not
|
||||
// really serializable. For a full list of properties see here:
|
||||
// https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/index.js
|
||||
// https://github.com/babel/babel/blob/main/packages/babel-core/src/transformation/index.js
|
||||
// For discussion on this topic see here:
|
||||
// https://github.com/babel/babel-loader/pull/629
|
||||
|
||||
|
||||
Reference in New Issue
Block a user