-
Notifications
You must be signed in to change notification settings - Fork 34
/
config.ts
69 lines (61 loc) · 2.24 KB
/
config.ts
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
import path from 'path'
import { Plugin, UserConfig, mergeConfig, normalizePath } from 'vite'
import glob from 'fast-glob'
import createDebugger from 'debug'
import { ResolvedVitePluginShopifyOptions } from './options'
const debug = createDebugger('vite-plugin-shopify:config')
// Plugin for setting necessary Vite config to support Shopify plugin functionality
export default function shopifyConfig (options: ResolvedVitePluginShopifyOptions): Plugin {
return {
name: 'vite-plugin-shopify-config',
config (config: UserConfig): UserConfig {
const host = config.server?.host ?? 'localhost'
const port = config.server?.port ?? 5173
const https = config.server?.https
const protocol = https === true ? 'https:' : 'http:'
const origin = `${protocol}//${host as string}:${port}`
const socketProtocol = https === true ? 'wss' : 'ws'
let input = glob.sync(normalizePath(path.join(options.entrypointsDir, '**/*')), { onlyFiles: true })
options.additionalEntrypoints.forEach((globPattern) => {
input = input.concat(glob.sync(globPattern, { onlyFiles: true }))
})
const generatedConfig: UserConfig = {
// Use relative base path so to load imported assets from Shopify CDN
base: './',
// Do not use "public" directory
publicDir: false,
build: {
// Output files to "assets" directory
outDir: path.join(options.themeRoot, 'assets'),
// Do not use subfolder for static assets
assetsDir: '',
// Configure bundle entry points
rollupOptions: { input },
// Output manifest file for backend integration
manifest: true
},
resolve: {
// Provide import alias to source code dir for convenience
alias: {
'~': path.resolve(options.sourceCodeDir),
'@': path.resolve(options.sourceCodeDir)
}
},
server: {
host,
https,
port,
origin,
strictPort: true,
hmr: {
host: host as string,
port,
protocol: socketProtocol
}
}
}
debug(generatedConfig)
return mergeConfig(generatedConfig, config)
}
}
}