保誠-保戶業務員媒合平台
Tomas
2022-05-19 957a1f10a06fdbb76f1a0ba94fe44126c613fee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import path from 'path';
import util from 'util';
 
import glob from 'glob';
 
import type {LoaderContext, StyleResourcesLoaderNormalizedOptions} from '..';
 
import {isStyleFile} from './type-guards';
 
/* eslint-disable-next-line @typescript-eslint/no-unsafe-member-access */
const isLegacyWebpack = (ctx: any): ctx is {options: {context: string}} => !!ctx.options;
 
const getRootContext = (ctx: LoaderContext) => {
    /* istanbul ignore if: will be deprecated soon */
    if (isLegacyWebpack(ctx)) {
        return ctx.options.context;
    }
 
    return ctx.rootContext;
};
 
const flatten = <T>(items: T[][]) => {
    const emptyItems: T[] = [];
 
    return emptyItems.concat(...items);
};
 
export const matchFiles = async (ctx: LoaderContext, options: StyleResourcesLoaderNormalizedOptions) => {
    const {patterns, globOptions} = options;
 
    const files = await Promise.all(
        patterns.map(async pattern => {
            const rootContext = getRootContext(ctx);
            const absolutePattern = path.isAbsolute(pattern) ? pattern : path.resolve(rootContext, pattern);
            const partialFiles = await util.promisify(glob)(absolutePattern, globOptions);
 
            return partialFiles.filter(isStyleFile);
        }),
    );
 
    /**
     * Glob always returns Unix-style file paths which would have cache invalidation problems on Windows.
     * Use `path.resolve()` to convert Unix-style file paths to system-compatible ones.
     *
     * @see {@link https://github.com/yenshih/style-resources-loader/issues/17}
     */
    return [...new Set(flatten(files))].map(file => path.resolve(file));
};