-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathvite.config.js
More file actions
144 lines (125 loc) · 4.15 KB
/
vite.config.js
File metadata and controls
144 lines (125 loc) · 4.15 KB
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
import { defineConfig } from 'vite'
import path from 'path'
import fs from 'fs'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
const PACKAGE_VERSION = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version
// Build configuration
const buildConfig = {
isProd: process.env.NODE_ENV?.trim() === 'production',
isDev: process.env.NODE_ENV?.trim() === 'development' || !process.env.NODE_ENV,
// Output configuration
distDir: 'dist',
srcDir: 'src',
loaderDir: 'src/lex-web-ui-loader'
}
// Banner plugin for adding license headers
const bannerPlugin = () => ({
name: 'banner-plugin',
generateBundle(options, bundle) {
if (!buildConfig.isProd) return
const banner = `/*!
* lex-web-ui-loader v${PACKAGE_VERSION}
* (c) 2017-2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Released under the Amazon Software License.
*/`
for (const fileName in bundle) {
const chunk = bundle[fileName]
if (chunk.type === 'chunk' && fileName.endsWith('.js')) {
chunk.code = banner + '\n' + chunk.code
}
}
}
})
// HTML plugin to copy and process HTML files to dist
const htmlPlugin = () => ({
name: 'html-plugin',
closeBundle() {
const websiteDir = path.join(buildConfig.srcDir, 'website')
const htmlFiles = ['index.html', 'parent.html']
// Determine which files to inject based on build mode
const loaderJs = buildConfig.isProd ? 'lex-web-ui-loader.min.js' : 'lex-web-ui-loader.js'
const loaderCss = buildConfig.isProd ? 'lex-web-ui-loader.min.css' : 'lex-web-ui-loader.css'
htmlFiles.forEach(file => {
const srcPath = path.join(websiteDir, file)
const destPath = path.join(buildConfig.distDir, file)
if (fs.existsSync(srcPath)) {
let htmlContent = fs.readFileSync(srcPath, 'utf8')
// Replace Webpack template tags with actual script/link tags
const headTags = `<link href="./${loaderCss}" rel="stylesheet">`
const bodyTags = `<script src="./${loaderJs}"></script>`
htmlContent = htmlContent.replace('<%= htmlWebpackPlugin.tags.headTags %>', headTags)
htmlContent = htmlContent.replace('<%= htmlWebpackPlugin.tags.bodyTags %>', bodyTags)
fs.writeFileSync(destPath, htmlContent)
console.log(` ✓ Processed and copied HTML: ${file}`)
}
})
}
})
export default defineConfig({
plugins: [
// Node.js polyfills for browser compatibility
nodePolyfills({
include: ['buffer', 'process', 'util'],
globals: {
Buffer: true,
global: true,
process: true
}
}),
bannerPlugin(),
htmlPlugin()
],
define: {
'process.env.PACKAGE_VERSION': JSON.stringify(PACKAGE_VERSION),
'process.env.NODE_ENV': JSON.stringify(buildConfig.isProd ? 'production' : 'development')
},
resolve: {
alias: {
'@': path.resolve(__dirname, buildConfig.srcDir)
},
extensions: ['.mjs', '.js', '.json']
},
build: {
lib: {
entry: path.resolve(__dirname, buildConfig.loaderDir, 'js', 'index.js'),
name: 'ChatBotUiLoader',
formats: ['umd'],
fileName: () => buildConfig.isProd ? 'lex-web-ui-loader.min.js' : 'lex-web-ui-loader.js'
},
outDir: buildConfig.distDir,
emptyOutDir: false, // Don't clean dist as it contains other files
sourcemap: true,
minify: buildConfig.isProd,
cssCodeSplit: false,
rollupOptions: {
output: {
// Ensure the global variable is properly set
exports: 'named',
// Ensure CSS files are named consistently
assetFileNames: (assetInfo) => {
if (assetInfo.name === 'style.css') {
return buildConfig.isProd ? 'lex-web-ui-loader.min.css' : 'lex-web-ui-loader.css'
}
return '[name].[ext]'
}
}
}
},
// Development server configuration
server: {
port: 8000,
host: true,
open: '/src/website/index.html',
strictPort: false,
cors: true,
// Serve static files from multiple directories
fs: {
allow: ['.']
}
},
// CSS configuration
css: {
devSourcemap: buildConfig.isDev,
postcss: './postcss.config.js'
}
})