-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
165 lines (144 loc) · 5.11 KB
/
index.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import deepFreeze from 'deep-freeze-strict';
import Metalsmith from 'metalsmith';
import path from 'path';
import sass from 'sass';
import {
FileInterface,
MetalsmithStrictFiles,
MetalsmithStrictWritableFiles,
} from '../utils/metalsmith';
import { loadModule } from '../utils/option';
import { FunctionTypeOnly, isReadonlyOrWritableArray } from '../utils/types';
import { normalize as normalizeSassOptions } from './sass';
type OptionsGenerator<T> =
| string
| T
| ((
files: MetalsmithStrictWritableFiles,
metalsmith: Metalsmith,
defaultOptions: DefaultOptionsInterface,
) => T | Promise<T>);
export interface SassOptionsObjectInterface
extends Omit<sass.Options, 'indentedSyntax'> {
/**
* Glob patterns that match files that use the Indented Syntax.
* For files that match the pattern, set Dart Sass's indentedSyntax option to true.
* @see https://sass-lang.com/documentation/syntax#the-indented-syntax
* @see https://sass-lang.com/documentation/js-api#indentedsyntax
*/
indentedSyntax?: string | ReadonlyArray<string>;
}
export interface OptionsInterface {
readonly pattern: ReadonlyArray<string>;
readonly sassOptions:
| SassOptionsObjectInterface
| ((context: {
filename: string;
filedata: FileInterface;
sourceFileFullpath: string;
destinationFileFullpath: string;
metalsmith: Metalsmith;
metalsmithFiles: MetalsmithStrictFiles;
pluginOptions: OptionsInterface;
}) => sass.Options | Promise<sass.Options>);
readonly renamer: (filename: string) => string | Promise<string>;
readonly dependenciesKey: string | false | null;
}
export interface DefaultOptionsInterface
extends Omit<OptionsInterface, 'sassOptions'> {
readonly sassOptions: Exclude<
OptionsInterface['sassOptions'],
FunctionTypeOnly<OptionsInterface['sassOptions']>
>;
}
type InputSassImporter = string | Record<string, unknown> | sass.Importer;
type InputSassFunctionsValue =
| string
| Record<string, unknown>
| Required<SassOptionsObjectInterface>['functions'][string];
export interface InputSassOptionsInterface
extends Omit<
SassOptionsObjectInterface,
'sourceMap' | 'importer' | 'functions'
> {
sourceMap?: Exclude<
Required<SassOptionsObjectInterface>['sourceMap'],
string
>;
importer?: InputSassImporter | InputSassImporter[];
functions?: Record<string, InputSassFunctionsValue>;
}
export interface InputOptionsInterface
extends Omit<OptionsInterface, 'pattern' | 'sassOptions' | 'renamer'> {
readonly pattern: string | OptionsInterface['pattern'];
readonly sassOptions:
| string
| Exclude<OptionsInterface['sassOptions'], SassOptionsObjectInterface>
| InputSassOptionsInterface;
readonly renamer: string | OptionsInterface['renamer'] | boolean | null;
}
export type InputOptions = OptionsGenerator<Partial<InputOptionsInterface>>;
export const defaultOptions: DefaultOptionsInterface = deepFreeze({
/**
* Partial files whose names begin with an underscore should be excluded from conversion.
* @see https://sass-lang.com/guide#topic-4
*/
pattern: ['**/*.sass', '**/*.scss', '!**/_*'],
sassOptions: {},
renamer(filename) {
const newFilename =
path.basename(filename, path.extname(filename)) + '.css';
return path.join(path.dirname(filename), newFilename);
},
dependenciesKey: false,
});
function normalizePattern(
inputPattern?: InputOptionsInterface['pattern'],
): OptionsInterface['pattern'] {
if ((Array.isArray as isReadonlyOrWritableArray)(inputPattern)) {
return inputPattern;
}
if (typeof inputPattern === 'string') {
return [inputPattern];
}
return defaultOptions.pattern;
}
function normalizeRenamer(
inputRenamer?: InputOptionsInterface['renamer'],
): OptionsInterface['renamer'] {
if (typeof inputRenamer === 'string' && inputRenamer !== '') {
inputRenamer = loadModule(
inputRenamer,
err => `Loading renamer failed: ${err.message}`,
) as Exclude<typeof inputRenamer, string>;
}
if (typeof inputRenamer === 'function') {
return inputRenamer;
}
if (inputRenamer || inputRenamer === undefined) {
return defaultOptions.renamer;
}
return (filename: string) => filename;
}
export async function normalizeOptions(
files: MetalsmithStrictWritableFiles,
metalsmith: Metalsmith,
opts: InputOptions,
): Promise<OptionsInterface> {
if (typeof opts === 'string') {
opts = loadModule(
opts,
err => `Loading options failed: ${err.message}`,
) as Exclude<InputOptions, string>;
}
if (typeof opts === 'function') {
opts = await opts(files, metalsmith, defaultOptions);
}
return {
...defaultOptions,
...opts,
pattern: normalizePattern(opts.pattern),
sassOptions: normalizeSassOptions(opts.sassOptions),
renamer: normalizeRenamer(opts.renamer),
};
}