Skip to content

Commit

Permalink
smaller cdn
Browse files Browse the repository at this point in the history
  • Loading branch information
apinet committed Feb 26, 2024
1 parent 33e155b commit 218d847
Show file tree
Hide file tree
Showing 6 changed files with 592 additions and 241 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ or by installing it locally using package manager such as NPM:
npm i lit-line@latest
```

Once installed, you only have to import the desired component and you are good to go.
Once installed, you only have to import the component and you are good to go.

```js
import "lit-line";
```

:::tip[About version management]
It is not recommended to use the `@latest` suffix, as a major release could break your application. Instead, use a fixed version such as `0.3.0`.
It is not recommended to use the `@latest` suffix, as a major release could break your application. Instead, use a fixed version such as `0.3.1`.
:::

## quick start
Expand Down
34 changes: 33 additions & 1 deletion build.cdn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import esbuild from "esbuild"
import esbuild from "esbuild";
import { minifyHTMLLiterals } from 'minify-html-literals';
import { readFile } from 'node:fs/promises';

//import info from "./package.json" assert { type: "json" };
//const externals = Object.keys(info.peerDependencies);

Expand All @@ -12,5 +15,34 @@ esbuild.build({
"./src/lit-line.ts",
],
outdir: "cdn",
plugins: [minifyHTMLLiteralsPlugin()],
external: [], // empty to ensure deps are bundled
});

/*
* From the awesome bennypowers/lit-css
*/
export function minifyHTMLLiteralsPlugin(options) {
const { filter = /\.[jt]s$/, ...minifyOptions } = options ?? {};
return {
name: 'minifyHTMLLiterals',
setup(build) {
const cache = new Map();

build.onLoad({ filter }, async ({ path }) => {
const loader = path.match(/c?tsx?$/) ? 'ts' : 'js';
const input = await readFile(path, 'utf8');
const cached = cache.get(path);
if (cached?.source === input)
return cached.output;
else {
const result = minifyHTMLLiterals(input, minifyOptions) ?? undefined;
const contents = result && `${result.code}\n//# sourceMappingURL=${result.map?.toUrl()}`;
const output = result && { contents, loader };
cache.set(path, { input, output });
return output;
}
});
},
};
}
Loading

0 comments on commit 218d847

Please sign in to comment.