@@ -55,6 +55,68 @@ const renamePlugin = {
5555} ;
5656
5757const prod = process . argv [ 2 ] === "production" ;
58+ const outDir = prod ? "dist" : "." ;
59+
60+ // Ensure dist directory exists in production mode
61+ if ( prod && ! fs . existsSync ( "dist" ) ) {
62+ fs . mkdirSync ( "dist" , { recursive : true } ) ;
63+ }
64+
65+ // Update plugins to handle output directory
66+ const renamePluginWithDir = {
67+ name : "rename-styles" ,
68+ setup ( build ) {
69+ build . onEnd ( ( ) => {
70+ const { outfile } = build . initialOptions ;
71+ const outcss = outfile . replace ( / \. j s $ / , ".css" ) ;
72+ const fixcss = outfile . replace ( / m a i n \. j s $ / , "styles.css" ) ;
73+ if ( fs . existsSync ( outcss ) ) {
74+ console . log ( "Renaming" , outcss , "to" , fixcss ) ;
75+ fs . renameSync ( outcss , fixcss ) ;
76+ }
77+ } ) ;
78+ } ,
79+ } ;
80+
81+ // Update CSS settings plugin to handle output directory
82+ const cssSettingsPluginWithDir = {
83+ name : "css-settings-plugin" ,
84+ setup ( build ) {
85+ build . onEnd ( async ( result ) => {
86+ // Path to the output CSS file
87+ const cssOutfile = path . join ( outDir , "styles.css" ) ;
88+
89+ // The settings comment to prepend
90+ const settingsComment =
91+ fs . readFileSync ( "src/styles/index.css" , "utf8" ) . split ( "*/" ) [ 0 ] +
92+ "*/\n\n" ;
93+
94+ if ( fs . existsSync ( cssOutfile ) ) {
95+ // Read the current content
96+ const cssContent = fs . readFileSync ( cssOutfile , "utf8" ) ;
97+
98+ // Check if the settings comment is already there
99+ if ( ! cssContent . includes ( "/* @settings" ) ) {
100+ // Prepend the settings comment
101+ fs . writeFileSync ( cssOutfile , settingsComment + cssContent ) ;
102+ }
103+ }
104+ } ) ;
105+ } ,
106+ } ;
107+
108+ // Copy manifest to output directory in production
109+ const copyManifestPlugin = {
110+ name : "copy-manifest" ,
111+ setup ( build ) {
112+ build . onEnd ( ( ) => {
113+ if ( prod ) {
114+ fs . copyFileSync ( "manifest.json" , path . join ( outDir , "manifest.json" ) ) ;
115+ console . log ( "Copied manifest.json to" , outDir ) ;
116+ }
117+ } ) ;
118+ } ,
119+ } ;
58120
59121esbuild
60122 . build ( {
@@ -65,9 +127,9 @@ esbuild
65127 entryPoints : [ "src/index.ts" ] ,
66128 plugins : [
67129 inlineWorkerPlugin ( { workerName : "Task Genius Indexer" } ) ,
68-
69- renamePlugin ,
70- cssSettingsPlugin ,
130+ renamePluginWithDir ,
131+ cssSettingsPluginWithDir ,
132+ copyManifestPlugin ,
71133 ] ,
72134 bundle : true ,
73135 external : [
@@ -107,7 +169,7 @@ esbuild
107169 logLevel : "info" ,
108170 sourcemap : prod ? false : "inline" ,
109171 treeShaking : true ,
110- outfile : "main.js" ,
172+ outfile : path . join ( outDir , "main.js" ) ,
111173 pure : prod ? [ "console.log" ] : [ ] ,
112174 } )
113175 . catch ( ( ) => process . exit ( 1 ) ) ;
0 commit comments