-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
224 lines (184 loc) · 6.01 KB
/
build.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// SPDX-FileCopyrightText: 2023 Redradix - development@redradix.com
//
// SPDX-License-Identifier: MIT
const svelte = require('rollup-plugin-svelte')
const sveltePreprocess = require('svelte-preprocess')
const { babel } = require('@rollup/plugin-babel')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const commonjs = require('@rollup/plugin-commonjs')
const json = require('@rollup/plugin-json')
const css = require('rollup-plugin-css-only')
const replace = require('@rollup/plugin-replace')
const livereload = require('rollup-plugin-livereload')
const terser = require('rollup-plugin-terser').terser
const shell = require('shelljs')
const fs = require('fs').promises
const path = require('path')
const rollup = require('rollup')
const packageJson = require('../package.json')
const appPath = path.resolve(__dirname, '../')
const production = !process.env.ROLLUP_WATCH
const buildPath = 'dist'
const entryPoint = `${appPath}/src/index.svelte`
const entryPointRegexp = /index\.svelte$/
const moduleName = packageJson.name
// Replace special characters for allowing scoped packages like "@scope/package"
const displayModuleName = moduleName.replace('@', '').replace('/', '-')
const moduleVersion = packageJson.version
// Used for package.json "main" property
const moduleFile = `${buildPath}/${displayModuleName}.js`
const bundleName = `${displayModuleName}.${moduleVersion}.js`
const outputOptions = {
sourcemap: true,
format: 'iife',
name: 'App',
file: `${buildPath}/${bundleName}`,
}
const commonRollupPlugins = [
json(),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
nodeResolve({
browser: true,
dedupe: ['svelte'],
}),
commonjs(),
// transpile to ES2015+
babel({
extensions: ['.js', '.mjs', '.html', '.svelte'],
babelHelpers: 'runtime',
}),
]
async function extractCSS() {
let cssChunk = ''
const bundle = await rollup.rollup({
input: entryPoint,
plugins: [
svelte({
compilerOptions: {
dev: false,
// all nested child elements are built as normal svelte components
customElement: false,
},
emitCss: true,
preprocess: sveltePreprocess(),
}),
// HACK! Inject nested CSS into custom element shadow root
css({
output(nestedCSS, styleNodes, bundle) {
const escapedCssChunk = nestedCSS
.replace(/\n/g, '')
.replace(/[\\"']/g, '\\$&')
.replace(/\u0000/g, '\\0')
cssChunk = escapedCssChunk
},
}),
...commonRollupPlugins,
],
})
await bundle.generate(outputOptions)
return cssChunk
}
async function buildWebComponent({ minify, cssChunk }) {
// 1. Create a bundle
const bundle = await rollup.rollup({
input: entryPoint,
plugins: [
svelte({
compilerOptions: {
dev: false,
// all nested child elements are built as normal svelte components
customElement: false,
},
emitCss: true,
exclude: entryPointRegexp,
preprocess: sveltePreprocess(),
}),
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
// we're generating a -- Web Component -- from index.svelte
customElement: true,
},
emitCss: false,
include: entryPointRegexp,
preprocess: sveltePreprocess(),
}),
// HACK! Inject nested CSS into custom element shadow root
css({
output(nestedCSS, styleNodes, bundle) {
const code = bundle[bundleName].code
const matches = code.match(
minify
? /.shadowRoot.innerHTML='<style>(.*)<\/style>'/
: /.shadowRoot.innerHTML = "<style>(.*)<\/style>"/,
)
if (matches && matches[1]) {
const style = matches[1]
bundle[bundleName].code = code.replace(style, cssChunk)
} else {
throw new Error(
"Couldn't shadowRoot <style> tag for injecting styles",
)
}
},
}),
// HACK! Fix svelte/transitions in web components
// Use shadow root instead of document for transition style injecting
replace({
'.ownerDocument': '.getRootNode()',
delimiters: ['', ''],
}),
// Append styles to shadow root
replace({
'.head.appendChild(e': '.appendChild(e',
delimiters: ['', ''],
}),
// END HACK
...commonRollupPlugins,
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload(buildPath),
// If we're building for production (npm run build
// instead of npm run dev), minify
minify && terser(),
],
})
// 2. Generate output specific code in-memory
// you can call this function multiple times on the same bundle object
const { output } = await bundle.generate(outputOptions)
const { code, map } = output[0]
// 3. Write bundles into files
const fileName = minify
? `${outputOptions.file.replace('.js', '.min.js')}`
: outputOptions.file
// Normal bundle
await fs.writeFile(fileName, code)
// Only create .map.js when code is minified
if (minify) {
await fs.writeFile(fileName.replace('.js', '.js.map'), map.toString())
}
// Generic bundle for using as module entry point
if (!minify) {
await fs.writeFile(moduleFile, code)
}
}
async function main() {
try {
shell.mkdir('-p', buildPath)
shell.rm('dist/*')
const cssChunk = await extractCSS()
// builds readable bundle of the web component
await buildWebComponent({ minify: false, cssChunk })
// builds minified bundle with sourcemap
await buildWebComponent({ minify: true, cssChunk })
} catch (ex) {
console.error(ex)
process.exit(1)
}
}
main()