Skip to content

Commit

Permalink
promisify webpack call
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 27, 2023
1 parent 47aac32 commit 5ec4852
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 570 deletions.
304 changes: 114 additions & 190 deletions test/cache.test.js
Expand Up @@ -2,7 +2,7 @@ import test from "ava";
import fs from "node:fs";
import path from "node:path";
import { rimraf } from "rimraf";
import webpack from "webpack";
import { webpackAsync } from "./helpers/webpackAsync.js";
import createTestDirectory from "./helpers/createTestDirectory.js";
import { fileURLToPath } from "node:url";
const __dirname = fileURLToPath(path.dirname(import.meta.url));
Expand Down Expand Up @@ -45,7 +45,7 @@ test.beforeEach(async t => {
test.beforeEach(() => rimraf(defaultCacheDir));
test.afterEach(t => rimraf([t.context.directory, t.context.cacheDirectory]));

test("should output files to cache directory", t => {
test("should output files to cache directory", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -65,144 +65,101 @@ test("should output files to cache directory", t => {
},
});

return new Promise(resolve => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
resolve();
});
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
});

test.serial(
"should output json.gz files to standard cache dir by default",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
test("should output json.gz files to standard cache dir by default", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
],
},
});

return new Promise(resolve => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
},
],
},
});

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file => CACHE_FILE_REGEX.test(file));
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

t.is(err, null);
t.true(files.length > 0);
resolve();
});
});
});
},
);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
});

test.serial(
"should output non-compressed files to standard cache dir when cacheCompression is set to false",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
cacheCompression: false,
presets: ["@babel/preset-env"],
},
// eslint-disable-next-line max-len
test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
cacheCompression: false,
presets: ["@babel/preset-env"],
},
],
},
});

return new Promise(resolve => {
webpack(config, err => {
t.is(err, null);

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file =>
UNCOMPRESSED_CACHE_FILE_REGEX.test(file),
);
},
],
},
});

t.is(err, null);
t.true(files.length > 0);
resolve();
});
});
});
},
);
await webpackAsync(config);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => UNCOMPRESSED_CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
});

test.serial(
"should output files to standard cache dir if set to true in query",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
test("should output files to standard cache dir if set to true in query", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
],
},
});

return new Promise(resolve => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file => CACHE_FILE_REGEX.test(file));
},
],
},
});

t.is(err, null);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

t.true(files.length > 0);
resolve();
});
});
});
},
);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
});

test("should read from cache directory if cached file exists", t => {
test("should read from cache directory if cached file exists", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -224,25 +181,16 @@ test("should read from cache directory if cached file exists", t => {

// @TODO Find a way to know if the file as correctly read without relying on
// Istanbul for coverage.
return new Promise(resolve => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

webpack(config, err => {
t.is(err, null);
fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
resolve();
});
});
});
});
await webpackAsync(config);
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
});

test("should have one file per module", t => {
test("should have one file per module", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -262,22 +210,15 @@ test("should have one file per module", t => {
},
});

return new Promise(resolve => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 3);
resolve();
});
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 3);
});

test("should generate a new file if the identifier changes", t => {
test("should generate a new file if the identifier changes", async t => {
const configs = [
Object.assign({}, globalConfig, {
output: {
Expand Down Expand Up @@ -318,29 +259,20 @@ test("should generate a new file if the identifier changes", t => {
},
}),
];
let counter = configs.length;

return new Promise(resolve => {
configs.forEach(config => {
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
counter -= 1;
await Promise.allSettled(
configs.map(async config => {
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
}),
);

if (!counter) {
fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 6);
resolve();
});
}
});
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 6);
});

test("should allow to specify the .babelrc file", t => {
test("should allow to specify the .babelrc file", async t => {
const config = [
Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
Expand Down Expand Up @@ -383,20 +315,12 @@ test("should allow to specify the .babelrc file", t => {
},
}),
];

return new Promise(resolve => {
webpack(config, (err, multiStats) => {
t.is(err, null);
t.deepEqual(multiStats.stats[0].compilation.errors, []);
t.deepEqual(multiStats.stats[0].compilation.warnings, []);
t.deepEqual(multiStats.stats[1].compilation.errors, []);
t.deepEqual(multiStats.stats[1].compilation.warnings, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 2);
resolve();
});
});
});
const multiStats = await webpackAsync(config);
t.deepEqual(multiStats.stats[0].compilation.errors, []);
t.deepEqual(multiStats.stats[0].compilation.warnings, []);
t.deepEqual(multiStats.stats[1].compilation.errors, []);
t.deepEqual(multiStats.stats[1].compilation.warnings, []);

const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 2);
});
3 changes: 3 additions & 0 deletions test/helpers/webpackAsync.js
@@ -0,0 +1,3 @@
import webpack from "webpack";
import { promisify } from "node:util";
export const webpackAsync = promisify(webpack);

0 comments on commit 5ec4852

Please sign in to comment.