1+ /* eslint-env node */
12import { fileURLToPath } from 'node:url' ;
23import { readdirSync } from 'node:fs' ;
34import { resolve } from 'node:path' ;
45
56import flattenHtmlPagesDirectoryPlugin from './flatten-html-pages-directory' ;
67
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+
715/**
816 * Creates a valid vite config set up for a Connect extension that uses Vite + Vue
917 *
@@ -14,63 +22,72 @@ import flattenHtmlPagesDirectoryPlugin from './flatten-html-pages-directory';
1422 * @param {object } config.vuePlugin - '@vitejs/vue' plugin instance
1523 * @param {object } viteOptions - your custom vite config options
1624 *
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
1826 */
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' ) ;
2136
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 ) ;
2638
27- return {
28- ...viteOptions ,
39+ return {
40+ ...viteOptions ,
2941
30- resolve : {
31- ...viteOptions . resolve ,
42+ resolve : {
43+ ...viteOptions . resolve ,
3244
33- alias : {
34- ...viteOptions . resolve ?. alias ,
45+ alias : {
46+ ...viteOptions . resolve ?. alias ,
3547
36- '~' : fileURLToPath ( srcUrl ) ,
48+ '~' : fileURLToPath ( srcUrl ) ,
49+ } ,
3750 } ,
38- } ,
3951
40- plugins : [ vuePlugin , flattenHtmlPagesDirectoryPlugin , ...( viteOptions . plugins || [ ] ) ] ,
52+ plugins : [ vuePlugin , flattenHtmlPagesDirectoryPlugin , ...( viteOptions . plugins || [ ] ) ] ,
53+
54+ root : srcDir ,
55+ base : '/static' ,
4156
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 ,
4462
45- build : {
46- ...viteOptions . build ,
63+ ...viteOptions . build ,
4764
48- outDir : outputDir ,
49- emptyOutDir : true ,
65+ outDir : outputDir ,
66+ emptyOutDir : true ,
5067
51- rollupOptions : {
52- ...viteOptions . build ?. rollupOptions ,
68+ rollupOptions : {
69+ ...viteOptions . build ?. rollupOptions ,
5370
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' ) ;
5774
58- return entryPoints ;
59- } , { } ) ,
75+ return entryPoints ;
76+ } , { } ) ,
6077
61- output : {
62- ...viteOptions . build ?. rollupOptions ?. output ,
78+ output : {
79+ ...viteOptions . build ?. rollupOptions ?. output ,
6380
64- format : 'es' ,
65- dir : outputDir ,
81+ format : 'es' ,
82+ dir : outputDir ,
6683
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+ } ,
7189 } ,
7290 } ,
7391 } ,
74- } ,
92+ } ;
7593 } ;
76- } ;
0 commit comments