Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F9582240
build.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
build.ts
View Options
import
{
build
,
BuildResult
}
from
"esbuild"
;
import
{
promises
as
fs
}
from
"fs"
;
import
{
sassPlugin
}
from
"esbuild-sass-plugin"
;
import
*
as
chokidar
from
"chokidar"
;
import
{
default
as
glob
}
from
"tiny-glob"
;
import
{
generateScssIncludes
}
from
"./find-scss-includes"
;
import
{
sleep
}
from
"./utils/sleep"
;
import
{
generateCollections
}
from
"./generate-collections"
;
import
{
generateRoutes
}
from
"./generate-routes"
;
const
generate_names
=
(
function
*
generate_names
()
{
let
base_name
=
"______unique_import_"
;
let
count
=
0
;
while
(
true
)
{
yield
base_name
+
count
;
count
++
;
}
})();
async
function
build_ts
()
{
await
Promise
.
all
([
generateCollections
(),
generateRoutes
()]);
const
entryPoints
=
(
await
glob
(
"./src/back/**/*.ts"
)).
filter
(
(
path
)
=>
!
path
.
includes
(
".#"
)
);
try
{
await
build
({
entryPoints
,
sourcemap
:
true
,
outdir
:
"./dist/back"
,
logLevel
:
"info"
,
platform
:
"node"
,
target
:
"es2022"
,
format
:
"esm"
,
plugins
:
[
{
name
:
"remove-named-imports"
,
// named imports sometime cause some esm incompatibilities, like the "is a CommonJS module, which may not support all module.exports as named exports." error
setup
(
build
)
{
build
.
onLoad
({
filter
:
/.*/
},
async
({
path
})
=>
{
const
named_and_default_regex
=
/^import\s+((\S+),\s+)?({\s*[^}]+})\s+from\s+("[^"]+");\s*$/gm
;
const
old_content
=
await
fs
.
readFile
(
path
,
"utf-8"
);
const
contents
=
old_content
.
replace
(
named_and_default_regex
,
(
entire_string
,
default_import_name
,
_
,
all_named
,
module_path
)
=>
{
const
new_name
=
generate_names
.
next
().
value
;
return
`import * as
${
new_name
}
from
${
module_path
}
;
${
default_import_name
?
`const
${
default_import_name
.
replace
(
","
,
""
)
}
=
${
new_name
}
`
:
""
}
; const
${
all_named
.
replaceAll
(
/\s+as\s+/g
,
": "
)
}
=
${
/
\
s
+
default
\
s
+
as
/
.
test
(
all_named
)
?
`
${
new_name
}
;`
:
`
${
new_name
}
.default ||
${
new_name
}
;`
}
`
;
}
);
return
{
contents
,
loader
:
"ts"
};
});
},
},
],
});
}
catch
(
e
)
{
console
.
error
(
e
.
message
);
}
}
async
function
complex_build
(
watch
:
boolean
)
:
Promise
<
void
>
{
let
scss_build
:
BuildResult
|
null
=
null
;
if
(
watch
)
{
const
watcher
=
chokidar
.
watch
(
"src"
,
{
ignoreInitial
:
true
});
watcher
.
on
(
"all"
,
async
(
_
,
path
)
=>
{
if
(
!
scss_build
)
return
;
if
(
path
.
includes
(
".#"
))
return
;
if
(
path
.
endsWith
(
".scss"
)
&&
!
path
.
endsWith
(
"/includes.scss"
))
{
// refresh the list of all scss files in includes.scss
await
generateScssIncludes
();
try
{
await
scss_build
?
.
rebuild
?
.();
console
.
log
(
`Built main.scss [on
${
path
}
]`
);
}
catch
(
e
)
{
console
.
error
(
e
.
message
);
await
sleep
(
200
);
scss_build
?
.
rebuild
?
.()
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.
catch
((
e
)
=>
console
.
error
(
e
?
.
message
));
}
}
if
(
path
.
endsWith
(
".ts"
)
&&
!
path
.
endsWith
(
"src/back/collections/collections.ts"
)
&&
!
path
.
endsWith
(
"src/back/routes/routes.ts"
)
&&
!
path
.
endsWith
(
"src/back/routes/urls.ts"
)
)
{
await
build_ts
();
console
.
log
(
`Finished TS build [on
${
path
}
]`
);
}
});
}
await
generateScssIncludes
();
scss_build
=
await
build
({
entryPoints
:
[
"./src/main.scss"
],
sourcemap
:
true
,
outfile
:
"./public/dist/style.css"
,
logLevel
:
"info"
,
incremental
:
watch
,
plugins
:
[
sassPlugin
()],
});
await
build_ts
();
watch
&&
scss_build
?
.
rebuild
?
.();
}
export
async
function
buildProject
({
watch
,
}
:
{
watch
:
boolean
;
})
:
Promise
<
void
>
{
try
{
await
build
({
entryPoints
:
[
"./src/front/index.ts"
],
sourcemap
:
true
,
outfile
:
"./public/dist/bundle.js"
,
logLevel
:
"info"
,
bundle
:
true
,
watch
,
});
await
complex_build
(
watch
);
}
catch
(
e
)
{
console
.
log
(
"CAUGHT!"
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
console
.
error
(
e
.
message
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Oct 11, 06:22 (4 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
977445
Default Alt Text
build.ts (4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment