deno.land / x / lume@v2.1.4 / plugins / postcss.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
import { autoprefixer, postcss, postcssImport, postcssNesting,} from "../deps/postcss.ts";import { merge } from "../core/utils/object.ts";import { concurrent } from "../core/utils/concurrent.ts";import { resolveInclude } from "../core/utils/path.ts";import { Page } from "../core/file.ts";import { prepareAsset, saveAsset } from "./source_maps.ts";import textLoader from "../core/loaders/text.ts";
import type Site from "../core/site.ts";import type { SourceMap } from "./source_maps.ts";
export interface Options { /** The list of extensions this plugin applies to */ extensions?: string[];
/** * Custom includes path for `postcss-import` * @default `site.options.includes` */ includes?: string | false;
/** * Plugins to use by postcss * @default `[postcssNesting(), autoprefixer()]` */ plugins?: unknown[];
/** Set `false` to remove the default plugins */ useDefaultPlugins?: boolean;
/** The name of the helper */ name?: string;}
// Default optionsexport const defaults: Options = { extensions: [".css"], useDefaultPlugins: true, name: "postcss",};
const defaultPlugins = [ postcssNesting(), autoprefixer(),];
/** A plugin to load all CSS files and process them using PostCSS */export default function (userOptions?: Options) { return (site: Site) => { const options = merge<Options>( { ...defaults, includes: site.options.includes }, userOptions, );
const plugins = [...options.plugins ?? []];
if (options.useDefaultPlugins) { plugins.unshift(...defaultPlugins); }
if (options.includes) { plugins.unshift(configureImport(site, options.includes)); site.ignore(options.includes); }
// @ts-ignore: Argument of type 'unknown[]' is not assignable to parameter of type 'AcceptedPlugin[]'. const runner = postcss(plugins);
site.hooks.addPostcssPlugin = (plugin) => { runner.use(plugin); }; site.hooks.postcss = (callback) => callback(runner);
site.loadAssets(options.extensions); site.process(options.extensions, (pages) => concurrent(pages, postCss)); site.filter(options.name, filter, true);
async function postCss(file: Page) { const { content, filename, sourceMap, enableSourceMap } = prepareAsset( site, file, ); const to = site.dest(file.outputPath); const map = enableSourceMap ? { inline: false, prev: sourceMap, annotation: false, } : undefined;
// Process the code with PostCSS const result = await runner.process(content, { from: filename, to, map });
saveAsset( site, file, result.css, result.map?.toJSON() as unknown as SourceMap, ); }
async function filter(code: string): Promise<string> { const result = await runner.process(code, { from: undefined }); return result.css; } };}
/** * Function to configure the postcssImport * using the Lume reader and the includes loader */function configureImport(site: Site, includes: string) { return postcssImport({ /** Resolve the import path */ resolve(id: string, basedir: string) { return resolveInclude(id, includes, basedir); },
/** Load the content (using the Lume reader) */ async load(file: string) { const content = await site.getContent(file, textLoader); if (content === undefined) { throw new Error(`File ${file} not found`); } return content; }, });}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/postcss/ */ postcss: (code: string) => Promise<string>; } }}
lume

Version Info

Tagged at
5 months ago