Skip to content

Commit

Permalink
Inline hydration directive scripts (withastro#3605)
Browse files Browse the repository at this point in the history
* Inline hydration scripts

* Adds a changeset

* Update directiveAstroKeys type
  • Loading branch information
matthewp authored Jun 16, 2022
1 parent 552622d commit 04555ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import del from 'del';
import { promises as fs } from 'fs';
import { dim, green, red, yellow } from 'kleur/colors';
import glob from 'tiny-glob';
import prebuild from './prebuild.js';

/** @type {import('esbuild').BuildOptions} */
const defaultConfig = {
Expand All @@ -20,9 +21,23 @@ const dt = new Intl.DateTimeFormat('en-us', {
minute: '2-digit',
});

function getPrebuilds(isDev, args) {
let prebuilds = [];
while(args.includes('--prebuild')) {
let idx = args.indexOf('--prebuild');
prebuilds.push(args[idx + 1]);
args.splice(idx, 2);
}
if(prebuilds.length && isDev) {
prebuilds.unshift('--no-minify');
}
return prebuilds;
}

export default async function build(...args) {
const config = Object.assign({}, defaultConfig);
const isDev = args.slice(-1)[0] === 'IS_DEV';
const prebuilds = getPrebuilds(isDev, args);
const patterns = args
.filter((f) => !!f) // remove empty args
.map((f) => f.replace(/^'/, '').replace(/'$/, '')); // Needed for Windows: glob strings contain surrounding string chars??? remove these
Expand Down Expand Up @@ -59,6 +74,9 @@ export default async function build(...args) {
...config,
watch: {
onRebuild(error, result) {
if(prebuilds.length) {
prebuild(...prebuilds);
}
const date = dt.format(new Date());
if (error || (result && result.errors.length)) {
console.error(dim(`[${date}] `) + red(error || result.errors.join('\n')));
Expand Down
8 changes: 7 additions & 1 deletion cmd/prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default async function prebuild(...args) {
args.splice(buildToString, 1);
buildToString = true;
}
let minify = true;
let minifyIdx = args.indexOf('--no-minify');
if(minifyIdx !== -1) {
minify = false;
args.splice(minifyIdx, 1);
}

let patterns = args;
let entryPoints = [].concat(
Expand All @@ -33,7 +39,7 @@ export default async function prebuild(...args) {
const tscode = await fs.promises.readFile(filepath, 'utf-8');
const esbuildresult = await esbuild.transform(tscode, {
loader: 'ts',
minify: true,
minify,
});
const rootURL = new URL('../../', import.meta.url);
const rel = path.relative(fileURLToPath(rootURL), filepath);
Expand Down

0 comments on commit 04555ed

Please sign in to comment.