Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 15 additions & 6 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const path = require("path");
const zlib = require("zlib");
const { promisify } = require("util");
const { readFile, writeFile, mkdir } = require("fs/promises");
const v8 = require("v8");
const { sync: findUpSync } = require("find-up");
const { env } = process;
const transform = require("./transform");
Expand Down Expand Up @@ -50,10 +51,13 @@ const gzip = promisify(zlib.gzip);
* @param {string} filename
* @param {boolean} compress
*/
const read = async function (filename, compress) {
const read = async function (filename, compress, raw) {
const data = await readFile(filename + (compress ? ".gz" : ""));
const content = compress ? await gunzip(data) : data;

if (raw) {
return v8.deserialize(content);
}
return JSON.parse(content.toString());
};

Expand All @@ -64,11 +68,16 @@ const read = async function (filename, compress) {
* @param {boolean} compress
* @param {any} result
*/
const write = async function (filename, compress, result) {
const content = JSON.stringify(result);

const data = compress ? await gzip(content) : content;
return await writeFile(filename + (compress ? ".gz" : ""), data);
const write = async function (filename, compress, result, raw) {
let data;
if (raw) {
data = v8.serialize(result);
} else {
const content = JSON.stringify(result);
data = compress ? await gzip(content) : content;
}
const path = filename + (compress && !raw ? ".gz" : "");
return await writeFile(path, data);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @property {string} [cacheDirectory] Directory to store cached files.
* @property {string} [cacheIdentifier] Unique identifier to bust cache.
* @property {boolean} [cacheCompression] Whether to compress cached files.
* @property {boolean} [cacheRaw] Whether to use raw v8 serialization for cache files.
* @property {string} [customize] The absolute path of a file that exports a BabelLoaderWrapper.
* @property {Array<string>} [metadataSubscribers] Names of subscribers registered in the loader context.
*/
Expand Down Expand Up @@ -55,6 +56,7 @@ if (/^6\./.test(babel.version)) {

const { version } = require("../package.json");
const cache = require("./cache");
const v8 = require("v8");
const transform = require("./transform");
const injectCaller = require("./injectCaller");
const schema = require("./schema.json");
Expand Down
4 changes: 4 additions & 0 deletions src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"type": "boolean",
"default": true
},
"cacheRaw": {
"type": "boolean",
"default": false
},
"customize": {
"anyOf": [
{
Expand Down