diff options
author | 2024-08-20 14:29:50 +0200 | |
---|---|---|
committer | 2024-08-20 14:29:50 +0200 | |
commit | 787fed8504e7ec604d96ff266f58db715e84f736 (patch) | |
tree | ef29a659ddf6511e2564ee4d442f992cc9695241 /scripts/cmd | |
parent | 6617491c3bc2bde87f7867d7dec2580781852cfc (diff) | |
parent | c6622adaeb405e961b12c91f0e5d02c7333d01cf (diff) | |
download | astro-787fed8504e7ec604d96ff266f58db715e84f736.tar.gz astro-787fed8504e7ec604d96ff266f58db715e84f736.tar.zst astro-787fed8504e7ec604d96ff266f58db715e84f736.zip |
Merge branch 'main' into next
Diffstat (limited to 'scripts/cmd')
-rw-r--r-- | scripts/cmd/build.js | 50 | ||||
-rw-r--r-- | scripts/cmd/copy.js | 86 | ||||
-rw-r--r-- | scripts/cmd/prebuild.js | 21 | ||||
-rw-r--r-- | scripts/cmd/test.js | 14 |
4 files changed, 42 insertions, 129 deletions
diff --git a/scripts/cmd/build.js b/scripts/cmd/build.js index 5dd200643..e1c712993 100644 --- a/scripts/cmd/build.js +++ b/scripts/cmd/build.js @@ -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} */ @@ -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'); @@ -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, @@ -76,6 +84,7 @@ export default async function build(...args) { outdir, outExtension: forceCJS ? { '.js': '.cjs' } : {}, format, + plugins: [copyPlugin].filter(Boolean), }); return; } @@ -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')); @@ -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(); @@ -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 }))); } /** @@ -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'), @@ -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+/, ''); diff --git a/scripts/cmd/copy.js b/scripts/cmd/copy.js deleted file mode 100644 index 948ed114f..000000000 --- a/scripts/cmd/copy.js +++ /dev/null @@ -1,86 +0,0 @@ -import { globby as glob } from 'globby'; -import { promises as fs, readFileSync } from 'node:fs'; -import { posix } from 'node:path'; -import { parseArgs } from 'node:util'; -import * as tar from 'tar/create'; - -const { resolve, dirname, sep, join } = posix; - -export default async function copy() { - const args = parseArgs({ - allowPositionals: true, - options: { - tgz: { type: 'boolean' }, - }, - }); - const patterns = args.positionals.slice(1); - const isCompress = args.values.tgz; - - if (isCompress) { - const files = await glob(patterns, { gitignore: true }); - const rootDir = resolveRootDir(files); - const destDir = rootDir.replace(/^[^/]+/, 'dist'); - - const templates = files.reduce((acc, curr) => { - const name = curr.replace(rootDir, '').slice(1).split(sep)[0]; - if (acc[name]) { - acc[name].push(resolve(curr)); - } else { - acc[name] = [resolve(curr)]; - } - return acc; - }, {}); - - let meta = {}; - return Promise.all( - Object.entries(templates).map(([template, files]) => { - const cwd = resolve(join(rootDir, template)); - const dest = join(destDir, `${template}.tgz`); - const metafile = files.find((f) => f.endsWith('meta.json')); - if (metafile) { - files = files.filter((f) => f !== metafile); - meta[template] = JSON.parse(readFileSync(metafile).toString()); - } - return fs.mkdir(dirname(dest), { recursive: true }).then(() => - tar.create( - { - gzip: true, - portable: true, - file: dest, - cwd, - }, - files.map((f) => f.replace(cwd, '').slice(1)) - ) - ); - }) - ).then(() => { - if (Object.keys(meta).length > 0) { - return fs.writeFile(resolve(destDir, 'meta.json'), JSON.stringify(meta, null, 2)); - } - }); - } - - const files = await glob(patterns); - await Promise.all( - files.map((file) => { - const dest = resolve(file.replace(/^[^/]+/, 'dist')); - return fs - .mkdir(dirname(dest), { recursive: true }) - .then(() => fs.copyFile(resolve(file), dest, fs.constants.COPYFILE_FICLONE)); - }) - ); -} - -function resolveRootDir(files) { - return files - .reduce((acc, curr) => { - const currParts = curr.split(sep); - if (acc.length === 0) return currParts; - const result = []; - currParts.forEach((part, i) => { - if (acc[i] === part) result.push(part); - }); - return result; - }, []) - .join(sep); -} diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index de3a36910..7c4174abf 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -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, '\\${'); @@ -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) { @@ -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; @@ -91,7 +92,7 @@ export default async function prebuild(...args) { dev: true, } : undefined, - ].filter((entry) => entry) + ].filter((entry) => entry), ); for (const result of results) { diff --git a/scripts/cmd/test.js b/scripts/cmd/test.js index 17f6ecd04..3182c4b90 100644 --- a/scripts/cmd/test.js +++ b/scripts/cmd/test.js @@ -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; @@ -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 :( @@ -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; |