@yahoo/uds-create-loader 1.11.0-v3-import.1 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/unplugin.ts DELETED
@@ -1,94 +0,0 @@
1
- import path from 'node:path';
2
- import { createUnplugin } from 'unplugin';
3
- import transformLoader from './loader';
4
-
5
- export interface UdsPluginOptions {
6
- /**
7
- * Path to `.uds/config.json` (default: `<cwd>/.uds/config.json`)
8
- */
9
- dataPath?: string;
10
- /**
11
- * Enable development-only metadata transforms.
12
- * Defaults to `process.env.NODE_ENV !== 'production'`.
13
- */
14
- isDev?: boolean;
15
- /**
16
- * Include filters for files to transform.
17
- * Defaults to JS/TS module extensions.
18
- */
19
- include?: RegExp | RegExp[];
20
- /**
21
- * Exclude filters for files to skip.
22
- */
23
- exclude?: RegExp | RegExp[];
24
- }
25
-
26
- const DEFAULT_INCLUDE = /\.[cm]?[jt]sx?$/;
27
-
28
- function toArray(value: RegExp | RegExp[] | undefined): RegExp[] {
29
- if (!value) return [];
30
- return Array.isArray(value) ? value : [value];
31
- }
32
-
33
- function sanitizeId(id: string): string {
34
- const queryStart = id.indexOf('?');
35
- const hashStart = id.indexOf('#');
36
- const cutIndex =
37
- queryStart === -1
38
- ? hashStart
39
- : hashStart === -1
40
- ? queryStart
41
- : Math.min(queryStart, hashStart);
42
- return cutIndex === -1 ? id : id.slice(0, cutIndex);
43
- }
44
-
45
- function matchesFilters(
46
- id: string,
47
- includes: RegExp[],
48
- excludes: RegExp[],
49
- ): boolean {
50
- if (excludes.some((pattern) => pattern.test(id))) return false;
51
- if (includes.length === 0) return true;
52
- return includes.some((pattern) => pattern.test(id));
53
- }
54
-
55
- export const udsPlugin = createUnplugin<UdsPluginOptions>((options = {}) => {
56
- const includePatterns = toArray(options.include ?? DEFAULT_INCLUDE);
57
- const excludePatterns = toArray(options.exclude);
58
- const dataPath = path.resolve(
59
- process.cwd(),
60
- options.dataPath ?? '.uds/config.json',
61
- );
62
- const isDev = options.isDev ?? process.env.NODE_ENV !== 'production';
63
-
64
- return {
65
- name: 'uds-unplugin',
66
- enforce: 'pre',
67
- transformInclude(id) {
68
- return matchesFilters(sanitizeId(id), includePatterns, excludePatterns);
69
- },
70
- transform(code, id) {
71
- const resourcePath = sanitizeId(id);
72
- if (!matchesFilters(resourcePath, includePatterns, excludePatterns)) {
73
- return null;
74
- }
75
-
76
- const transformedCode = (
77
- transformLoader as unknown as (this: unknown, source: string) => string
78
- ).call(
79
- {
80
- getOptions: () => ({ dataPath, isDev }),
81
- addDependency: () => {},
82
- resourcePath,
83
- },
84
- code,
85
- );
86
-
87
- if (transformedCode === code) return null;
88
- return {
89
- code: transformedCode,
90
- map: null,
91
- };
92
- },
93
- };
94
- });