-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathweb-dev-server.config.js
83 lines (72 loc) · 2.27 KB
/
web-dev-server.config.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
/* eslint-env node */
const fs = require('fs');
const { esbuildPlugin } = require('@web/dev-server-esbuild');
const path = require('path');
/** @return {import('@web/test-runner').TestRunnerPlugin} */
function generatedLitTestsPlugin() {
return {
name: 'generated-lit-tests',
transformImport({ source, context }) {
if (context.url.includes('-lit.generated.test.')) {
const dependencyPath = path.resolve(path.dirname(context.url), source);
const litDependencyPath = dependencyPath
// /button/vaadin-button.js -> /button/vaadin-lit-button.js
.replace(/\/vaadin-(?!lit)([^/]+)/u, '/vaadin-lit-$1')
// /grid/all-imports.js -> /grid/lit-all-imports.js
.replace(/\/all-imports/u, '/lit-all-imports');
if (litDependencyPath !== dependencyPath && fs.existsSync(`.${litDependencyPath}`)) {
return litDependencyPath;
}
}
return source;
},
};
}
const preventFouc = `
<style>
body:not(.resolved) {
opacity: 0;
}
body {
transition: opacity 0.2s;
}
</style>
<script type="module">
// It's important to use type module for the script so the timing is correct
document.body.classList.add('resolved');
</script>
`;
module.exports = {
plugins: [
{
name: 'dev-page-listing',
transform(context) {
if (context.response.is('html')) {
let body = context.body;
// Fouc prevention
body = body.replace(/<\/body>/u, `${preventFouc}\n</body>`);
// Index page listing
if (['/dev/index.html', '/dev', '/dev/'].includes(context.path)) {
const listing = `
<ul id="listing">
${fs
.readdirSync('./dev')
.filter((file) => file !== 'index.html')
.filter((file) => file.endsWith('.html'))
.map((file) => `<li><a href="/dev/${file}">${file}</a></li>`)
.join('')}
</ul>`;
body = body.replace(/<ul id="listing">.*<\/ul>/u, listing);
}
return { body };
}
},
},
esbuildPlugin({ ts: true }),
generatedLitTestsPlugin(),
],
nodeResolve: {
// Use Lit in production mode
exportConditions: ['default'],
},
};