forked from StarpTech/sveltejs-brunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (107 loc) · 3.11 KB
/
index.js
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
'use strict'
const svelte = require('svelte/compiler')
const fs = require('fs')
const path = require('path')
const combine = require('combine-source-map')
function sanitize(input) {
return path
.basename(input)
.replace(path.extname(input), '')
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1')
}
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1)
}
class SvelteCompiler {
constructor(cfg) {
this.opts = cfg.plugins.sveltejs || {}
// svelte 3 throws error if unknown key exists in compile options
this.preProcessOpts = this.opts.preprocess || {}
delete this.opts.preprocess
this.opts.format = this.opts.format || 'cjs'
this.cssLookup = new Map()
if (this.opts.pattern) {
this.pattern = this.opts.pattern
delete this.opts.pattern
}
if (this.opts.extractCSS) this.opts.css = false
}
onCompile() {
if (this.opts.extractCSS) {
this.extractCSS()
}
}
compile(args) {
if (!this.opts.name) this.opts.name = capitalize(sanitize(args.path))
return (
svelte
/**
* {@since 1.0.4} - Make sure to pass through {filename: args.path} to svelte preprocessor
*/
.preprocess(args.data, this.preProcessOpts, { filename: args.path })
.then((result) => {
let { js, css, ast } = svelte.compile(
result.toString(),
Object.assign({ filename: args.path }, this.opts)
)
if (this.opts.extractCSS && css) {
this.cssLookup.set(args.path, {
code: css,
map: cssMap,
path: args.path,
})
}
/**
* {@since 1.0.2 - Preserve the correct file path for source map generation in brunch}
*/
js.map.sources = [args.path]
return {
data: js.code,
map: js.map,
}
})
)
}
extractCSS() {
const outPath = this.opts.out || this.opts.o || 'bundle.css'
let css = ''
let sourceMapCombiner = combine.create(path.basename(outPath))
let offset = 0
for (let chunk of this.cssLookup.values()) {
if (!chunk.code) continue
css += chunk.code + '\n'
if (this.opts.combineSourceMapCSS) {
sourceMapCombiner.addFile(
{
source:
chunk.code + '\n/*# sourceMappingURL=' + chunk.map.toUrl() + '*/',
sourceFile: chunk.path,
},
{
line: offset,
}
)
offset += 1
}
}
if (this.opts.combineSourceMapCSS) {
css +=
'\n/*# sourceMappingURL=data:application/json;base64,' +
sourceMapCombiner.base64() +
'*/'
}
if (typeof outPath === 'object' && outPath.write) {
outPath.write(css)
outPath.end()
} else if (typeof outPath === 'string') {
fs.writeFileSync(outPath, css)
}
}
}
SvelteCompiler.prototype.brunchPlugin = true
SvelteCompiler.prototype.type = 'javascript'
SvelteCompiler.prototype.pattern = /\.(svelte|svelte\.html)$/
module.exports = SvelteCompiler