deno.land / x / lume@v2.1.4 / plugins / source_maps.ts

source_maps.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { isUrl, normalizePath } from "../core/utils/path.ts";import { merge } from "../core/utils/object.ts";import { log } from "../core/utils/log.ts";import { read } from "../core/utils/read.ts";import { concurrent } from "../core/utils/concurrent.ts";import { encodeBase64 } from "../deps/base64.ts";import { Page } from "../core/file.ts";import { basename, join, toFileUrl } from "../deps/path.ts";
import type Site from "../core/site.ts";
export interface Options { /** Set true to inline the source map in the output file */ inline?: boolean;
/** Set true to include the content of the source files */ sourceContent?: boolean;}
export const defaults: Options = { inline: false, sourceContent: false,};
/** Generate the source map files of assets */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site._data.enableSourceMap = true;
site.process( "*", (pages, allPages) => concurrent(pages, (page) => processSourceMap(page, allPages)), );
async function processSourceMap(file: Page, files: Page[]) { const sourceMap = file.data.sourceMap as SourceMap | undefined; file.data.sourceMap = undefined;
if (!sourceMap) { return; }
// Add the content of the source files try { if (options.sourceContent) { sourceMap.sourcesContent = await Promise.all( sourceMap.sources.map((url: string) => { const content = sourceMap[dynamicSourcesSymbol]?.[url];
return content ? content : read(url, false); }), ); } } catch (err) { log.error(`${err.message}\n${sourceMap.sources.join("\n")}`); }
// Relative paths (eg. "../bar") look better in the dev-tools. sourceMap.sourceRoot = toFileUrl(site.root()).href; sourceMap.sources = sourceMap.sources.map((url: string) => url.replace(sourceMap.sourceRoot!, "") );
// Inline the source map in the output file if (options.inline) { const url = `data:application/json;charset=utf-8;base64,${ encodeBase64(JSON.stringify(sourceMap)) }`; file.content += addSourceMap(file.outputPath, url); return; }
// Create a source map file const url = file.outputPath + ".map"; sourceMap.file = url; file.content += addSourceMap(file.outputPath, `./${basename(url)}`); files.push(Page.create({ url, content: JSON.stringify(sourceMap) })); } };}
/** Utilities to use by other plugins to manage source maps */export const dynamicSourcesSymbol = Symbol.for("dynamicSources");
/** SourceMap with a property to store dynamic sources */export interface SourceMap { version: number; file?: string; sources: readonly string[]; sourceRoot?: string; sourcesContent?: readonly (string | null)[]; names: readonly string[]; mappings: string; [dynamicSourcesSymbol]?: Record<string, string>;}
export interface PrepareResult { content: string; sourceMap?: SourceMap; filename: string; enableSourceMap: boolean;}
/** Return the required info to process a file */export function prepareAsset(site: Site, page: Page): PrepareResult { const enableSourceMap = !!site._data.enableSourceMap; const content = page.content as string; const sourceMap = enableSourceMap ? page.data.sourceMap as SourceMap | undefined : undefined; const filename = page.src.path ? site.src(page.src.path + page.src.ext) : site.src(page.outputPath); return { content, sourceMap, filename, enableSourceMap };}
/** Save the process result */export function saveAsset( site: Site, page: Page, content: string, sourceMap?: SourceMap | string,) { if (!site._data.enableSourceMap) { sourceMap = undefined; }
// There's no source map if (!sourceMap) { page.content = content; return; }
const root = site.root();
// Ensure the sourceMap is an object if (typeof sourceMap === "string") { sourceMap = JSON.parse(sourceMap) as SourceMap; }
// Normalize any source url function normalizeSource(source: string): string { if (source.startsWith("deno:")) { // esbuild source = source.substring(5); } if (isUrl(source)) { return source; }
source = normalizePath(source);
return source.startsWith(root) ? toFileUrl(decodeURIComponent(source)).href : toFileUrl(decodeURIComponent(join(root, source))).href; }
sourceMap.sources = sourceMap.sources .filter((source: string) => source !== "<no source>") // tailwindcss .map(normalizeSource);
// Inherit the dynamic sources from the previous source map const previousSourceMap = page.data.sourceMap as SourceMap | undefined; if (previousSourceMap) { sourceMap[dynamicSourcesSymbol] = previousSourceMap[dynamicSourcesSymbol]; }
// If it's a dynamic source (not from the file system), store it in the source map if (!page.src.path) { const sources = sourceMap[dynamicSourcesSymbol] || {}; const file = normalizeSource(site.src(page.outputPath)); sourceMap[dynamicSourcesSymbol] = sources;
if (!sources[file]) { sources[file] = page.content as string; } }
// Store the new content and source map page.data.sourceMap = sourceMap; page.content = content;}
function addSourceMap(url: string, sourceMap: string): string { if (url.endsWith(".js")) { return `\n//# sourceMappingURL=${sourceMap}`; }
// It's CSS return `\n/*# sourceMappingURL=${sourceMap} */`;}
/** Extends Data interface */declare global { namespace Lume { export interface Data { /** * The source map data (if it's an asset) * @see https://lume.land/plugins/source_maps/ */ sourceMap?: SourceMap; } }}
lume

Version Info

Tagged at
6 months ago