Skip to content

Commit

Permalink
Make type checking happier
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Feb 13, 2022
1 parent b1b559b commit 9c8cea6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/tasks/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Task {
* Watch all relevant files for changes.
*
* @param {boolean} usePolling
* @param {function(Task)} onFileChange Will be called on every file that changes
* @param {(task: Task<TData>) => void|Promise<void>} onFileChange Will be called on every file that changes
*/
watch(usePolling = false, onFileChange = Function()) {
watch(usePolling = false, onFileChange = NOOP) {
if (this.isBeingWatched) return;

let files = this.files.get();
Expand Down
41 changes: 18 additions & 23 deletions src/webpackPlugins/CustomTasksPlugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let Log = require('../Log');
let collect = require('collect.js');
const VersionFilesTask = require('../tasks/VersionFilesTask');
const { debounce } = require('lodash');
const webpack = require('webpack');
const Task = require('../tasks/Task');

class CustomTasksPlugin {
/**
Expand Down Expand Up @@ -43,12 +45,11 @@ class CustomTasksPlugin {
/**
* Execute the task.
*
* @param {import("../tasks/Task")} task
* @param {import("../tasks/Task")<any>} task
* @param {import("webpack").Stats} stats
*/
async runTask(task, stats) {
await Promise.resolve(task.run());

await Promise.allSettled(task.assets.map(asset => this.addAsset(asset, stats)));
}

Expand All @@ -71,34 +72,28 @@ class CustomTasksPlugin {
this.mix.manifest.add(path);

// Update the Webpack assets list for better terminal output.
stats.compilation.assets[path] = {
size: () => asset.size(),
emitted: true
};
stats.compilation.assets[path] = new webpack.sources.SizeOnlySource(asset.size());
}

/**
* Execute potentially asynchronous tasks sequentially.
*
* @param stats
* @param index
* @param {import("webpack").Stats} stats
*/
runTasks(stats, index = 0) {
if (index === this.mix.tasks.length) return Promise.resolve();

const task = this.mix.tasks[index];

return this.runTask(task, stats).then(() => this.runTasks(stats, index + 1));
async runTasks(stats) {
for (const task of this.mix.tasks) {
await this.runTask(task, stats);
}
}

/**
* Minify the given asset file.
* @param {Task} task If specified, only assets of this task will be minified
* @param {Task<any> | null} task If specified, only assets of this task will be minified
*/
async minifyAssets(task = null) {
const assets = collect(task ? [task] : this.mix.tasks)
.where('constructor.name', '!==', 'VersionFilesTask')
.flatMap(({ assets }) => assets);
const assets = (task ? [task] : this.mix.tasks)
.filter(task => !(task instanceof VersionFilesTask))
.flatMap(task => task.assets);

const tasks = assets.map(async asset => {
try {
Expand All @@ -120,14 +115,14 @@ class CustomTasksPlugin {
* Version all files that are present in the manifest.
*/
applyVersioning() {
collect(this.mix.manifest.get()).each((value, key) =>
this.mix.manifest.hash(key)
);
Object.keys(this.mix.manifest.get()).forEach(path => {
this.mix.manifest.hash(path);
});
}

/**
* Performs manifest and minification updates on files after running tasks
* @param {Task|null} task If specified, only files for this task will be minified
* @param {Task<any>|null} task If specified, only files for this task will be minified
* @return {Promise<void>}
*/
async afterChange(task = null) {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ interface Api {

declare const exports: Api;
declare namespace exports {
export { Api, ReactConfig, VueConfig };
export { Api, Component, ReactConfig, VueConfig };
}

declare global {
Expand Down

0 comments on commit 9c8cea6

Please sign in to comment.