Skip to content

Commit

Permalink
Remove unused code in internal scripts (#11769)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and ematipico committed Aug 21, 2024
1 parent 9b2d802 commit 72be659
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 623 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"**/vendor/**",
"**/.vercel/**"
],
"include": ["test/**", "e2e/**", "packages/**"]
"include": ["test/**", "e2e/**", "packages/**", "/scripts/**"]
},
"formatter": {
"indentStyle": "tab",
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@
],
"scripts": {
"prebuild": "astro-scripts prebuild --to-string \"src/runtime/server/astro-island.ts\" \"src/runtime/client/{idle,load,media,only,visible}.ts\"",
"build": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" && tsc && pnpm run postbuild",
"build:ci": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" && pnpm run postbuild",
"build": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" --copy-wasm && tsc",
"build:ci": "pnpm run prebuild && astro-scripts build \"src/**/*.{ts,js}\" --copy-wasm",
"dev": "astro-scripts dev --copy-wasm --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.{ts,js}\"",
"postbuild": "astro-scripts copy \"src/**/*.astro\" && astro-scripts copy \"src/**/*.wasm\"",
"test": "pnpm run test:node && pnpm run test:types",
"test:match": "pnpm run test:node --match",
"test:e2e": "pnpm test:e2e:chrome && pnpm test:e2e:firefox",
Expand Down
1 change: 0 additions & 1 deletion packages/internal-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"prepublish": "pnpm build",
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"postbuild": "astro-scripts copy \"src/**/*.js\"",
"dev": "astro-scripts dev \"src/**/*.ts\""
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion packages/markdown/remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"prepublish": "pnpm build",
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"postbuild": "astro-scripts copy \"src/**/*.js\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "astro-scripts test \"test/**/*.test.js\""
},
Expand Down
1 change: 0 additions & 1 deletion packages/underscore-redirects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"prepublish": "pnpm build",
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"postbuild": "astro-scripts copy \"src/**/*.js\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "astro-scripts test \"test/**/*.test.js\""
},
Expand Down
50 changes: 22 additions & 28 deletions scripts/cmd/build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { deleteAsync } from 'del';
import fs from 'node:fs/promises';
import esbuild from 'esbuild';
import { copy } from 'esbuild-plugin-copy';
import glob from 'fast-glob';
import { dim, green, red, yellow } from 'kleur/colors';
import { promises as fs } from 'node:fs';
import glob from 'tiny-glob';
import svelte from '../utils/svelte-plugin.js';
import prebuild from './prebuild.js';

/** @type {import('esbuild').BuildOptions} */
Expand Down Expand Up @@ -44,8 +42,8 @@ export default async function build(...args) {
.map((f) => f.replace(/^'/, '').replace(/'$/, '')); // Needed for Windows: glob strings contain surrounding string chars??? remove these
let entryPoints = [].concat(
...(await Promise.all(
patterns.map((pattern) => glob(pattern, { filesOnly: true, absolute: true }))
))
patterns.map((pattern) => glob(pattern, { filesOnly: true, absolute: true })),
)),
);

const noClean = args.includes('--no-clean-dist');
Expand All @@ -67,6 +65,16 @@ export default async function build(...args) {
await clean(outdir);
}

const copyPlugin = copyWASM
? copy({
resolveFrom: 'cwd',
assets: {
from: ['./src/assets/services/vendor/squoosh/**/*.wasm'],
to: ['./dist/assets/services/vendor/squoosh'],
},
})
: null;

if (!isDev) {
await esbuild.build({
...config,
Expand All @@ -76,6 +84,7 @@ export default async function build(...args) {
outdir,
outExtension: forceCJS ? { '.js': '.cjs' } : {},
format,
plugins: [copyPlugin].filter(Boolean),
});
return;
}
Expand All @@ -93,7 +102,7 @@ export default async function build(...args) {
} else {
if (result.warnings.length) {
console.log(
dim(`[${date}] `) + yellow('⚠ updated with warnings:\n' + result.warnings.join('\n'))
dim(`[${date}] `) + yellow('⚠ updated with warnings:\n' + result.warnings.join('\n')),
);
}
console.log(dim(`[${date}] `) + green('✔ updated'));
Expand All @@ -108,21 +117,7 @@ export default async function build(...args) {
outdir,
format,
sourcemap: 'linked',
plugins: [
rebuildPlugin,
svelte({ isDev }),
...(copyWASM
? [
copy({
resolveFrom: 'cwd',
assets: {
from: ['./src/assets/services/vendor/squoosh/**/*.wasm'],
to: ['./dist/assets/services/vendor/squoosh'],
},
}),
]
: []),
],
plugins: [rebuildPlugin, copyPlugin].filter(Boolean),
});

await builder.watch();
Expand All @@ -133,9 +128,8 @@ export default async function build(...args) {
}

async function clean(outdir) {
await deleteAsync([`${outdir}/**`, `!${outdir}/**/*.d.ts`], {
onlyFiles: true,
});
const files = await glob([`${outdir}/**`, `!${outdir}/**/*.d.ts`], { filesOnly: true });
await Promise.all(files.map((file) => fs.rm(file, { force: true })));
}

/**
Expand All @@ -148,7 +142,7 @@ async function getDefinedEntries() {
PACKAGE_VERSION: await getInternalPackageVersion('./package.json'),
/** The current version (at the time of building) for `astro` */
ASTRO_VERSION: await getInternalPackageVersion(
new URL('../../packages/astro/package.json', import.meta.url)
new URL('../../packages/astro/package.json', import.meta.url),
),
/** The current version (at the time of building) for `@astrojs/check` */
ASTRO_CHECK_VERSION: await getWorkspacePackageVersion('@astrojs/check'),
Expand All @@ -173,13 +167,13 @@ async function getInternalPackageVersion(path) {

async function getWorkspacePackageVersion(packageName) {
const { dependencies, devDependencies } = await readPackageJSON(
new URL('../../package.json', import.meta.url)
new URL('../../package.json', import.meta.url),
);
const deps = { ...dependencies, ...devDependencies };
const version = deps[packageName];
if (!version) {
throw new Error(
`Unable to resolve "${packageName}". Is it a dependency of the workspace root?`
`Unable to resolve "${packageName}". Is it a dependency of the workspace root?`,
);
}
return version.replace(/^\D+/, '');
Expand Down
86 changes: 0 additions & 86 deletions scripts/cmd/copy.js

This file was deleted.

21 changes: 11 additions & 10 deletions scripts/cmd/prebuild.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import esbuild from 'esbuild';
import { red } from 'kleur/colors';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import glob from 'tiny-glob';
import esbuild from 'esbuild';
import glob from 'fast-glob';
import { red } from 'kleur/colors';

function escapeTemplateLiterals(str) {
return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${');
Expand All @@ -23,10 +23,11 @@ export default async function prebuild(...args) {
}

let patterns = args;
// NOTE: absolute paths returned are forward slashes on windows
let entryPoints = [].concat(
...(await Promise.all(
patterns.map((pattern) => glob(pattern, { filesOnly: true, absolute: true }))
))
patterns.map((pattern) => glob(pattern, { onlyFiles: true, absolute: true })),
)),
);

function getPrebuildURL(entryfilepath, dev = false) {
Expand All @@ -43,20 +44,20 @@ export default async function prebuild(...args) {
let tscode = await fs.promises.readFile(filepath, 'utf-8');
// If we're bundling a client directive, modify the code to match `packages/astro/src/core/client-directive/build.ts`.
// If updating this code, make sure to also update that file.
if (filepath.includes(`runtime${path.sep}client`)) {
if (filepath.includes('runtime/client')) {
// `export default xxxDirective` is a convention used in the current client directives that we use
// to make sure we bundle this right. We'll error below if this convention isn't followed.
const newTscode = tscode.replace(
/export default (.*?)Directive/,
(_, name) =>
`(self.Astro || (self.Astro = {})).${name} = ${name}Directive;window.dispatchEvent(new Event('astro:${name}'))`
`(self.Astro || (self.Astro = {})).${name} = ${name}Directive;window.dispatchEvent(new Event('astro:${name}'))`,
);
if (newTscode === tscode) {
console.error(
red(
`${filepath} doesn't follow the \`export default xxxDirective\` convention. The prebuilt output may be wrong. ` +
`For more information, check out ${fileURLToPath(import.meta.url)}`
)
`For more information, check out ${fileURLToPath(import.meta.url)}`,
),
);
}
tscode = newTscode;
Expand Down Expand Up @@ -91,7 +92,7 @@ export default async function prebuild(...args) {
dev: true,
}
: undefined,
].filter((entry) => entry)
].filter((entry) => entry),
);

for (const result of results) {
Expand Down
14 changes: 9 additions & 5 deletions scripts/cmd/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { run } from 'node:test';
import { spec } from 'node:test/reporters';
import fs from 'node:fs/promises';
import path from 'node:path';
import { run } from 'node:test';
import { spec } from 'node:test/reporters';
import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import glob from 'tiny-glob';
import glob from 'fast-glob';

const isCI = !!process.env.CI;
const defaultTimeout = isCI ? 1400000 : 600000;
Expand All @@ -31,7 +31,11 @@ export default async function test() {
const pattern = args.positionals[1];
if (!pattern) throw new Error('Missing test glob pattern');

const files = await glob(pattern, { filesOnly: true, absolute: true });
const files = await glob(pattern, {
filesOnly: true,
absolute: true,
ignore: ['**/node_modules/**'],
});

// For some reason, the `only` option does not work and we need to explicitly set the CLI flag instead.
// Node.js requires opt-in to run .only tests :(
Expand All @@ -48,7 +52,7 @@ export default async function test() {
await fs.mkdir(path.dirname(tempTestFile), { recursive: true });
await fs.writeFile(
tempTestFile,
files.map((f) => `import ${JSON.stringify(pathToFileURL(f).toString())};`).join('\n')
files.map((f) => `import ${JSON.stringify(pathToFileURL(f).toString())};`).join('\n'),
);

files.length = 0;
Expand Down
2 changes: 1 addition & 1 deletion scripts/deps/update-example-versions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import path from 'node:path';
import { globby as glob } from 'globby';

/*
Expand Down
5 changes: 0 additions & 5 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ export default async function run() {
await build(...args, cmd === 'dev' ? 'IS_DEV' : undefined);
break;
}
case 'copy': {
const { default: copy } = await import('./cmd/copy.js');
await copy(...args);
break;
}
case 'prebuild': {
const { default: prebuild } = await import('./cmd/prebuild.js');
await prebuild(...args);
Expand Down
14 changes: 3 additions & 11 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@
"astro-scripts": "./index.js"
},
"dependencies": {
"arg": "^5.0.2",
"esbuild": "^0.21.5",
"globby": "^14.0.2",
"kleur": "^4.1.5",
"p-limit": "^6.1.0",
"svelte": "^4.2.18",
"tar": "^7.4.3",
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"@octokit/action": "^7.0.0",
"del": "^7.1.0",
"esbuild-plugin-copy": "^2.1.1",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"kleur": "^4.1.5",
"p-limit": "^6.1.0",
"tsconfck": "^3.1.1"
}
}
Loading

0 comments on commit 72be659

Please sign in to comment.