Skip to content

Commit

Permalink
chore: Update tools
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Apr 8, 2024
1 parent df9fcf9 commit 2bc8760
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^7.0.0",
"css-minimizer-webpack-plugin": "6.0.0",
"css-modules-types-loader": "^0.4.1",
"css-modules-types-loader": "^0.5.0",
"find-free-ports": "^3.1.1",
"html-webpack-plugin": "^5.6.0",
"koa": "^2.15.2",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 53 additions & 24 deletions tools/bin/webpack.config.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,72 @@

import webpack from 'webpack';
import { join, resolve } from 'path';
import { readdir } from 'fs/promises';
import resolveRules from '../lib/rules.js';
import appConfig from '../../app.config.js';
import { readdir, stat } from 'fs/promises';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';

/**
* @function read
* @param {string} path
* @returns {Promise<string[]>}
* @return {Promise<import('fs').Dirent[]>}
*/
async function getFilesAsync(path) {
// 文件结果列表
const result = [];
// 获取目录内容
const files = await readdir(path);

// 遍历目录内容
for (const file of files) {
const filePath = join(path, file);
const stats = await stat(filePath);

// 如果文件是目录,则递归调用
if (stats.isDirectory()) {
const subFiles = await getFilesAsync(filePath);

// 将子目录的文件合并到结果中
result.push(...subFiles);
async function read(path) {
const entries = await readdir(path, {
withFileTypes: true
});

return entries.values();
}

/**
* @function getFiles
* @param {string} root
* @return {AsyncGenerator<string>}
*/
export async function* getFiles(root) {
const waiting = [];

root = resolve(root);

let current = [root, await read(root)];

while (current) {
const [, iterator] = current;
const item = iterator.next();

if (item.done) {
current = waiting.pop();
} else {
// 如果文件不是目录,则添加到结果中
result.push(filePath);
const [dirname] = current;
const { value: stat } = item;
const path = join(dirname, stat.name);

if (stat.isFile()) {
yield path;
} else if (stat.isDirectory()) {
waiting.push([path, await read(path)]);
}
}
}
}

/**
* @function arrayFromAsync
* @param {ArrayLike<T> | Iterable<T> | AsyncIterable<T>} iterator
* @return {Promise<T[]>}
*/
async function arrayFromAsync(iterator) {
const array = [];

for await (const item of iterator) {
array.push(item);
}

// 返回结果
return result;
return array;
}

/**
Expand Down Expand Up @@ -131,7 +160,7 @@ export default async mode => {
resolve('app.config.js'),
resolve('.browserslistrc')
],
tools: await getFilesAsync(resolve('tools'))
tools: await arrayFromAsync(getFiles('tools'))
}
},
stats: {
Expand Down
2 changes: 1 addition & 1 deletion tools/bin/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function createMemfs() {
/**
* @function resolvePort
* @param {import('../interface').AppConfig['ports']} ports
* @returns {number}
* @return {number}
*/
async function resolvePort(ports = [8000, 9000]) {
if (!Array.isArray(ports)) {
Expand Down
4 changes: 2 additions & 2 deletions tools/lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import MiniCssExtractPlugin from 'mini-css-extract-plugin';
export default async mode => {
const isDevelopment = mode !== 'production';
const swcOptions = { ...(await swcrc()), swcrc: false };
const cssIdentName = isDevelopment ? '[local]-[hash:8]' : '[hash:8]';
const localIdentName = isDevelopment ? '[local]-[hash:8]' : '[hash:8]';
const postcssOptions = { postcssOptions: { ...(await postcssrc(mode)), config: false } };
const cssModulesOptions = { auto: true, localIdentName: cssIdentName, exportLocalsConvention: 'camelCaseOnly' };
const cssModulesOptions = { auto: true, namedExport: true, localIdentName, exportLocalsConvention: 'camelCaseOnly' };

return [
{
Expand Down

0 comments on commit 2bc8760

Please sign in to comment.