1
+ /* eslint-env node */
1
2
import { fileURLToPath } from 'node:url' ;
2
3
import { readdirSync } from 'node:fs' ;
3
4
import { resolve } from 'node:path' ;
4
5
5
6
import flattenHtmlPagesDirectoryPlugin from './flatten-html-pages-directory' ;
6
7
8
+ const checkIfIsProduction = ( mode ) => {
9
+ if ( mode ) return mode === 'production' ;
10
+ if ( process . env . NODE_ENV ) return process . env . NODE_ENV === 'production' ;
11
+
12
+ return false ;
13
+ } ;
14
+
7
15
/**
8
16
* Creates a valid vite config set up for a Connect extension that uses Vite + Vue
9
17
*
@@ -14,63 +22,72 @@ import flattenHtmlPagesDirectoryPlugin from './flatten-html-pages-directory';
14
22
* @param {object } config.vuePlugin - '@vitejs/vue' plugin instance
15
23
* @param {object } viteOptions - your custom vite config options
16
24
*
17
- * @returns {object } - Valid vite config set up for a connect extension
25
+ * @returns {function({mode: ('development'|'production')}): {resolve: *&{alias: *&{"~": string}}, build: *&{minify: string|boolean, emptyOutDir: boolean, sourcemap, rollupOptions: *&{output: *&{manualChunks(*): (string|undefined), format: string, dir: *}, input: {}}, outDir: *}, plugins, root: *, base: string} } - Valid vite config set up for a connect extension
18
26
*/
19
- export const defineExtensionConfig = ( config , viteOptions = { } ) => {
20
- const { srcDir, srcUrl, outputDir, vuePlugin } = config ;
27
+ export const defineExtensionConfig =
28
+ ( config , viteOptions = { } ) =>
29
+ ( { mode } ) => {
30
+ const { srcDir, srcUrl, outputDir, vuePlugin } = config ;
31
+
32
+ if ( ! srcDir ) throw new Error ( '"srcDir" is required' ) ;
33
+ if ( ! outputDir ) throw new Error ( '"outputDir" is required' ) ;
34
+ if ( ! vuePlugin ) throw new Error ( '"vuePlugin" is required' ) ;
35
+ if ( ! srcUrl ) throw new Error ( '"srcUrl" is required' ) ;
21
36
22
- if ( ! srcDir ) throw new Error ( '"srcDir" is required' ) ;
23
- if ( ! outputDir ) throw new Error ( '"outputDir" is required' ) ;
24
- if ( ! vuePlugin ) throw new Error ( '"vuePlugin" is required' ) ;
25
- if ( ! srcUrl ) throw new Error ( '"srcUrl" is required' ) ;
37
+ const isProduction = checkIfIsProduction ( mode ) ;
26
38
27
- return {
28
- ...viteOptions ,
39
+ return {
40
+ ...viteOptions ,
29
41
30
- resolve : {
31
- ...viteOptions . resolve ,
42
+ resolve : {
43
+ ...viteOptions . resolve ,
32
44
33
- alias : {
34
- ...viteOptions . resolve ?. alias ,
45
+ alias : {
46
+ ...viteOptions . resolve ?. alias ,
35
47
36
- '~' : fileURLToPath ( srcUrl ) ,
48
+ '~' : fileURLToPath ( srcUrl ) ,
49
+ } ,
37
50
} ,
38
- } ,
39
51
40
- plugins : [ vuePlugin , flattenHtmlPagesDirectoryPlugin , ...( viteOptions . plugins || [ ] ) ] ,
52
+ plugins : [ vuePlugin , flattenHtmlPagesDirectoryPlugin , ...( viteOptions . plugins || [ ] ) ] ,
53
+
54
+ root : srcDir ,
55
+ base : '/static' ,
41
56
42
- root : srcDir ,
43
- base : '/static' ,
57
+ build : {
58
+ // Enable minification on production builds
59
+ minify : isProduction ? 'esbuild' : false ,
60
+ // Enable sourcemaps on non-production builds
61
+ sourcemap : ! isProduction ,
44
62
45
- build : {
46
- ...viteOptions . build ,
63
+ ...viteOptions . build ,
47
64
48
- outDir : outputDir ,
49
- emptyOutDir : true ,
65
+ outDir : outputDir ,
66
+ emptyOutDir : true ,
50
67
51
- rollupOptions : {
52
- ...viteOptions . build ?. rollupOptions ,
68
+ rollupOptions : {
69
+ ...viteOptions . build ?. rollupOptions ,
53
70
54
- // Load all pages in {{srcDir}}/pages/{{pageName}}/index.html as entrypoints
55
- input : readdirSync ( resolve ( srcDir , 'pages' ) ) . reduce ( ( entryPoints , pageName ) => {
56
- entryPoints [ pageName ] = resolve ( srcDir , 'pages/' , pageName , 'index.html' ) ;
71
+ // Load all pages in {{srcDir}}/pages/{{pageName}}/index.html as entrypoints
72
+ input : readdirSync ( resolve ( srcDir , 'pages' ) ) . reduce ( ( entryPoints , pageName ) => {
73
+ entryPoints [ pageName ] = resolve ( srcDir , 'pages/' , pageName , 'index.html' ) ;
57
74
58
- return entryPoints ;
59
- } , { } ) ,
75
+ return entryPoints ;
76
+ } , { } ) ,
60
77
61
- output : {
62
- ...viteOptions . build ?. rollupOptions ?. output ,
78
+ output : {
79
+ ...viteOptions . build ?. rollupOptions ?. output ,
63
80
64
- format : 'es' ,
65
- dir : outputDir ,
81
+ format : 'es' ,
82
+ dir : outputDir ,
66
83
67
- // Split node_modules into a "vendor" chunk, and @cloudblueconnect modules into a "connect" chunk
68
- manualChunks ( id ) {
69
- if ( id . includes ( '@cloudblueconnect' ) ) return 'connect' ;
70
- if ( id . includes ( 'node_modules' ) ) return 'vendor' ;
84
+ // Split node_modules into a "vendor" chunk, and @cloudblueconnect modules into a "connect" chunk
85
+ manualChunks ( id ) {
86
+ if ( id . includes ( '@cloudblueconnect' ) ) return 'connect' ;
87
+ if ( id . includes ( 'node_modules' ) ) return 'vendor' ;
88
+ } ,
71
89
} ,
72
90
} ,
73
91
} ,
74
- } ,
92
+ } ;
75
93
} ;
76
- } ;
0 commit comments