-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEleventySvelte.js
138 lines (125 loc) · 3.75 KB
/
EleventySvelte.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
126
127
128
129
130
131
132
133
134
135
136
137
138
const path = require('path')
const globby = require('globby')
const rollup = require('rollup')
const svelte = require('rollup-plugin-svelte')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
class EleventySvelte {
constructor({
assetDir,
rollupPluginSvelteSSROptions,
rollupSSRPlugins,
rollupPluginSvelteClientOptions,
rollupClientPlugins,
useGitIgnore,
outputClient,
}) {
this.workingDir = process.cwd()
this.components = {}
this.rollupPluginSvelteSSROptions = rollupPluginSvelteSSROptions
this.rollupSSRPlugins = rollupSSRPlugins
this.rollupPluginSvelteClientOptions = rollupPluginSvelteClientOptions
this.rollupClientPlugins = rollupClientPlugins
this.clientDir = path.join(assetDir, 'client')
this.clientLegacyDir = path.join(assetDir, 'client_legacy')
this.useGitIgnore = useGitIgnore
this.outputClient = outputClient
}
async build(outputDir, pathPrefix = '') {
const inputs = await globby('**/*.11ty.svelte', { gitignore: this.useGitIgnore })
const ssrOutputs = await this.buildSSR(inputs)
let clientOutput, clientLegacyOutput
if (this.outputClient) {
;[clientOutput, clientLegacyOutput] = await this.buildClient(inputs, outputDir)
}
for (let {
output: [entry],
} of ssrOutputs) {
if (!!entry.facadeModuleId) {
const ssrModule = requireFromString(entry.code, entry.facadeModuleId)
this.components[path.relative(this.workingDir, entry.facadeModuleId)] = {
ssr: ssrModule.default,
client: clientOutput && path.join(pathPrefix, this.clientDir, entry.fileName),
clientLegacy: clientLegacyOutput && path.join(this.clientLegacyDir, entry.fileName),
data: ssrModule.data,
}
}
}
}
buildSSR(inputs) {
return Promise.all(
inputs.map((input) =>
rollup
.rollup({
input,
plugins: [
svelte({
...this.rollupPluginSvelteSSROptions,
compilerOptions: {
generate: 'ssr',
hydratable: this.outputClient,
...this.rollupPluginSvelteSSROptions.compilerOptions
},
}),
...this.rollupSSRPlugins,
],
external: [/^svelte/],
})
.then((build) =>
build.generate({
format: 'cjs',
exports: 'named',
})
)
)
)
}
buildClient(input, outputDir) {
return rollup
.rollup({
input,
plugins: [
svelte({
...this.rollupPluginSvelteClientOptions,
compilerOptions: {
hydratable: true,
...this.rollupPluginSvelteClientOptions.compilerOptions
},
}),
nodeResolve({
browser: true,
dedupe: ['svelte'],
}),
...this.rollupClientPlugins,
],
})
.then((build) =>
Promise.all(
[
{
dir: path.join(outputDir, this.clientDir),
format: 'esm',
exports: 'named',
},
{
dir: path.join(outputDir, this.clientLegacyDir),
format: 'system',
exports: 'named',
},
].map((outputOptions) => build.write(outputOptions))
)
)
}
getComponent(localPath) {
if (!this.components[localPath]) {
throw new Error(`"${localPath}" is not a valid Svelte template.`)
}
return this.components[localPath]
}
}
function requireFromString(src, filename) {
const m = new module.constructor()
m.paths = module.paths
m._compile(src, filename)
return m.exports
}
module.exports = EleventySvelte