-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglsl-loader.mjs
85 lines (82 loc) · 2.02 KB
/
glsl-loader.mjs
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
/* eslint-disable */
import fs from "fs"
import path from "path"
import { SourceMapGenerator } from "source-map"
function loadContent(
content,
filename,
dependencies,
resultLines,
sourceMapGenerator,
) {
const contentLines = content.split("\n")
for (let i = 0; i < contentLines.length; i++) {
const line = contentLines[i]
const match = /^\s*#pragma\s+webpack\s+include\s+(.*)$/g.exec(line)
if (match !== null) {
const [substr, what] = match
const what2 = path.resolve(path.dirname(filename), what)
if (!dependencies.includes(what2)) {
dependencies.push(what2)
resultLines.push(`// START ${what}`)
loadContent(
fs.readFileSync(what2, { encoding: "utf8" }),
what2,
dependencies,
resultLines,
sourceMapGenerator,
)
resultLines.push(`// END ${what}`)
}
} else {
resultLines.push(line)
try {
sourceMapGenerator.addMapping({
generated: { line: resultLines.length, column: 0 },
original: { line: i + 1, column: 0 },
source: filename,
})
} catch (e) {
console.error(e)
throw e
}
}
}
}
/**
*
* @param content {string}
* @param filename {string}
* @returns {[string,import("source-map").RawSourceMap]}
*/
function load(content, filename) {
const resultLines = []
const sourceMapGenerator = new SourceMapGenerator()
loadContent(content, filename, [], resultLines, sourceMapGenerator)
return [resultLines.join("\n"), sourceMapGenerator.toJSON()]
}
export default function (options, loaderContext, content) {
const dependencies = []
const [mod, sourceMap] = load(
content,
loaderContext.resourcePath,
dependencies,
)
let code = `
export default ${JSON.stringify(mod)}
const sourceMap = ${JSON.stringify(sourceMap)}
export { sourceMap };
`
if (loaderContext.hot) {
code += `
if (import.meta) {
import.meta.webpackHot.accept()
}
`
}
return {
cacheable: true,
dependencies,
code,
}
}