Skip to content

Commit

Permalink
Suport esbuild context API
Browse files Browse the repository at this point in the history
  • Loading branch information
EMH333 committed Sep 5, 2023
1 parent 231f782 commit 2180995
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 249 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## Unreleased (Breaking)

- Update development esbuild version to `0.19.0`

This means that this plugin now supports additional inhancements when using the `context` esbuild `v0.17.0` API as detailed below.

- **Minorly Breaking** Caching is automatically enabled after two sucessful builds when using the `context` esbuild API

Previously caching was automatically enabled when using the watch or incremental esbuild options, but those were removed in esbuild `v0.17.0`. This change brings back the automatic cache enabling when using the `context` API which supports the same features as the previous watch and incremental options. esbuild does not provide a way for plugins to determine if the `context` API is being used, so this feature is enabled after two successful builds. This should be a reasonable compromise between not enabling the cache at all and enabling it for every build (which wastes time and space if caching isn't needed).

If you are using the `context` API and want to disable the cache, you can set the `cache` option to `false` in the plugin options but this isn't recommended (if you do need to disable the cache for some reason, please open an issue to see if your usecase can be fixed).

## 0.7.4

- Lock Svelte peerDependency to `>=3.43.0 <5` to protect against breaking changes in future Svelte releases
Expand Down
49 changes: 35 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,22 @@ const convertMessage = ({ message, start, end, filename, frame }: Warning) => ({
text: message,
location: start &&
end && {
file: filename,
line: start.line,
column: start.column,
length: start.line === end.line ? end.column - start.column : 0,
lineText: frame,
},
file: filename,
line: start.line,
column: start.column,
length: start.line === end.line ? end.column - start.column : 0,
lineText: frame,
},
});

const shouldCache = (build: PluginBuild) =>
build.initialOptions.incremental || build.initialOptions.watch;
//still support old incremental option if possible, but can still be overriden by cache option
const shouldCache = (build: (PluginBuild & {
initialOptions: {
incremental?: boolean;
watch?: boolean;
}
})) =>
build.initialOptions?.incremental || build.initialOptions?.watch;

// TODO: Hot fix to replace broken e64enc function in svelte on node 16
const b64enc = Buffer
Expand Down Expand Up @@ -194,7 +200,7 @@ export default function sveltePlugin(options?: esbuildSvelteOptions): Plugin {
} catch (e: any) {
// if preprocess failed there are chances that an external dependency caused exception
// to avoid stop watching those files, we keep the previous dependencies if available
if (build.initialOptions.watch && cachedFile) {
if (cachedFile) {
previousWatchFiles = Array.from(cachedFile.dependencies.keys());
}
throw e;
Expand Down Expand Up @@ -274,10 +280,7 @@ export default function sveltePlugin(options?: esbuildSvelteOptions): Plugin {
}

// make sure to tell esbuild to watch any additional files used if supported
if (build.initialOptions.watch) {
// this array does include the orignal file, but esbuild should be smart enough to ignore it
result.watchFiles = Array.from(dependencyModifcationTimes.keys());
}
result.watchFiles = Array.from(dependencyModifcationTimes.keys());

return result;
} catch (e: any) {
Expand All @@ -295,7 +298,25 @@ export default function sveltePlugin(options?: esbuildSvelteOptions): Plugin {
return css ? { contents: css, loader: "css", resolveDir: dirname(path) } : null;
});

// code in this section can use esbuild features <= 0.11.15 because of `onEnd` check
// code in this section can use esbuild features >= 0.11.15 because of `onEnd` check
// this enables the cache at the end of the build. The cache is disabled by default,
// but if this plugin instance is used agian, then the cache will be enabled (because
// we can be confident that the build is incremental or watch).
// This saves enabling caching on every build, which would be a performance hit but
// also makes sure incremental performance is increased.
if (typeof build.onEnd === "function") {
build.onEnd(() => {
if (!options) {
options = {};
}
if (options.cache === undefined) {
options.cache = true;
}
});
}

// code in this section can use esbuild features >= 0.11.15 because of `onEnd` check
// TODO long term overzealous should be deprecated and removed (technically it doesn't work beyond the 0.17.0 context API changes)
if (
shouldCache(build) &&
options?.cache == "overzealous" &&
Expand Down

0 comments on commit 2180995

Please sign in to comment.