Skip to content

Commit

Permalink
Merge pull request #193 from chialab/empty-virtual-modules
Browse files Browse the repository at this point in the history
Correctly handle empty virtual modules
  • Loading branch information
edoardocavazza authored Jul 9, 2024
2 parents c81a59b + a5ef23d commit 7c3e4cb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-experts-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/esbuild-plugin-html': patch
---

Ignore empty `<script>` and `<style>` elements.
5 changes: 5 additions & 0 deletions .changeset/twelve-dragons-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/esbuild-rna': patch
---

Correctly handle empty (`contents = ''`) virtual modules.
4 changes: 4 additions & 0 deletions packages/esbuild-plugin-html/lib/collectScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ async function innerCollect($, dom, elements, target, format, type, attrs = {},
builds.set(element, resolvedFile.path);
entrypoints.set(resolvedFile.path, element);
} else {
const contents = $(element).html() || '';
if (!contents) {
return;
}
const entryPoint = path.join(options.sourceDir, helpers.createEntry('js'));
builds.set(element, {
path: entryPoint,
Expand Down
4 changes: 4 additions & 0 deletions packages/esbuild-plugin-html/lib/collectStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export async function collectStyles($, dom, options, helpers) {
builds.set(element, resolvedFile.path);
entrypoints.set(resolvedFile.path, element);
} else {
const contents = $(element).html() || '';
if (!contents) {
return;
}
const entryPoint = path.join(options.sourceDir, helpers.createEntry('css'));
builds.set(element, {
path: entryPoint,
Expand Down
2 changes: 2 additions & 0 deletions packages/esbuild-plugin-html/test/fixture/index.css.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
color: red;
}
</style>
<!-- Empty style tag -->
<style></style>
</head>
<body>

Expand Down
4 changes: 4 additions & 0 deletions packages/esbuild-plugin-html/test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ body {
<style>
@import '1-EKADEBHI.css'
</style>
<!-- Empty style tag -->
<style></style>
</head>
<body>
Expand Down Expand Up @@ -770,6 +772,8 @@ body {
<style>
@import '/public/1-VXBR4AUQ.css'
</style>
<!-- Empty style tag -->
<style></style>
</head>
<body>
Expand Down
8 changes: 4 additions & 4 deletions packages/esbuild-rna/lib/Build.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export class Build {
resolveDir = args.resolveDir;
} else {
const loadResult = await this.load(args);
if (!loadResult || !loadResult.contents) {
if (!loadResult || loadResult.contents == null) {
return;
}

Expand Down Expand Up @@ -1025,7 +1025,7 @@ export class Build {
with: {},
});

if (result && result.contents) {
if (result && result.contents != null) {
buffer = Buffer.from(result.contents);
} else {
buffer = await readFile(source);
Expand Down Expand Up @@ -1119,7 +1119,7 @@ export class Build {
value: true,
});

if (options.contents) {
if (options.contents != null) {
delete config.entryPoints;
config.stdin = {
sourcefile: options.path,
Expand Down Expand Up @@ -1207,7 +1207,7 @@ export class Build {
if (typeof entryPoint !== 'object') {
return;
}
if (entryPoint.contents) {
if (entryPoint.contents != null) {
build.addVirtualModule(entryPoint);
}
});
Expand Down

0 comments on commit 7c3e4cb

Please sign in to comment.