From 7583a8b42a6db44ca589398dba0cd9fe53643bef Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 12 Jul 2021 17:01:33 -0500 Subject: [PATCH] Update build script to avoid bundling (#304) * fix: simplify astro-scripts build * fix: remove source-map-support, add missing `.js` extensions * refactor: use `module` for `@astrojs/markdown-support` * fix: missing `.js` extension * fix: missed @astrojs/parser * fix: remove old file * fix: merge conflict * fix: CJS weirdness --- packages/astro/package.json | 3 +-- packages/astro/src/ast.ts | 1 - packages/astro/src/build.ts | 3 +-- packages/astro/src/build/sitemap.ts | 2 +- packages/astro/src/cli.ts | 1 - packages/astro/src/compiler/codegen/index.ts | 4 ++-- packages/astro/src/compiler/index.ts | 1 - packages/astro/src/config.ts | 2 -- packages/astro/src/dev.ts | 3 +-- packages/astro/src/logger.ts | 1 - packages/astro/src/runtime.ts | 9 +++++---- packages/astro/src/search.ts | 2 -- packages/markdown-support/package.json | 2 +- packages/markdown-support/tsconfig.json | 2 +- scripts/cmd/build.js | 6 ++---- yarn.lock | 2 +- 16 files changed, 16 insertions(+), 28 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 9974ec6d8..e0ef89326 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -34,7 +34,7 @@ "astro.mjs" ], "scripts": { - "build": "astro-scripts build \"src/*.ts\" \"src/compiler/index.ts\" \"src/frontend/**/*.ts\" \"src/internal/**/*.ts\" && tsc", + "build": "astro-scripts build \"src/**/*.ts\" && tsc", "postbuild": "astro-scripts copy \"src/**/*.astro\"", "dev": "astro-scripts dev \"src/**/*.ts\"", "benchmark": "node test/benchmark/dev.bench.js && node test/benchmark/build.bench.js", @@ -87,7 +87,6 @@ "shorthash": "^0.0.2", "slash": "^4.0.0", "snowpack": "^3.8.1", - "source-map-support": "^0.5.19", "string-width": "^5.0.0", "tiny-glob": "^0.2.8", "unified": "^9.2.1", diff --git a/packages/astro/src/ast.ts b/packages/astro/src/ast.ts index 4de7a6d16..bf73c8508 100644 --- a/packages/astro/src/ast.ts +++ b/packages/astro/src/ast.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; import type { Attribute } from '@astrojs/parser'; // AST utility functions diff --git a/packages/astro/src/build.ts b/packages/astro/src/build.ts index c5aac23c7..be7b0219b 100644 --- a/packages/astro/src/build.ts +++ b/packages/astro/src/build.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; import type { AstroConfig, BundleMap, BuildOutput, RuntimeMode, PageDependencies } from './@types/astro'; import type { LogOptions } from './logger'; @@ -13,7 +12,7 @@ import { bold, green, yellow, red, dim, underline } from 'kleur/colors'; import mime from 'mime'; import glob from 'tiny-glob'; import { bundleCSS } from './build/bundle/css.js'; -import { bundleJS, collectJSImports } from './build/bundle/js'; +import { bundleJS, collectJSImports } from './build/bundle/js.js'; import { buildCollectionPage, buildStaticPage, getPageType } from './build/page.js'; import { generateSitemap } from './build/sitemap.js'; import { logURLStats, collectBundleStats, mapBundleStatsToURLStats } from './build/stats.js'; diff --git a/packages/astro/src/build/sitemap.ts b/packages/astro/src/build/sitemap.ts index 7d6bf62a8..bca69dd8a 100644 --- a/packages/astro/src/build/sitemap.ts +++ b/packages/astro/src/build/sitemap.ts @@ -1,6 +1,6 @@ import type { BuildOutput } from '../@types/astro'; -import { canonicalURL } from './util'; +import { canonicalURL } from './util.js'; /** Construct sitemap.xml given a set of URLs */ export function generateSitemap(buildState: BuildOutput, site: string): string { diff --git a/packages/astro/src/cli.ts b/packages/astro/src/cli.ts index 65d55c120..6c33f3106 100644 --- a/packages/astro/src/cli.ts +++ b/packages/astro/src/cli.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; /* eslint-disable no-console */ import type { AstroConfig } from './@types/astro'; diff --git a/packages/astro/src/compiler/codegen/index.ts b/packages/astro/src/compiler/codegen/index.ts index 79d8049fa..7f027b85c 100644 --- a/packages/astro/src/compiler/codegen/index.ts +++ b/packages/astro/src/compiler/codegen/index.ts @@ -3,11 +3,10 @@ import type { CompileOptions } from '../../@types/compiler'; import type { AstroConfig, AstroMarkdownOptions, TransformResult, ComponentInfo, Components } from '../../@types/astro'; import type { ImportDeclaration, ExportNamedDeclaration, VariableDeclarator, Identifier, ImportDefaultSpecifier } from '@babel/types'; -import 'source-map-support/register.js'; import eslexer from 'es-module-lexer'; import esbuild from 'esbuild'; import path from 'path'; -import { parse, FEATURE_CUSTOM_ELEMENT } from '@astrojs/parser'; +import astroParser from '@astrojs/parser'; import { walk, asyncWalk } from 'estree-walker'; import _babelGenerator from '@babel/generator'; import babelParser from '@babel/parser'; @@ -26,6 +25,7 @@ import { nodeBuiltinsSet } from '../../node_builtins.js'; import { readFileSync } from 'fs'; import { pathToFileURL } from 'url'; +const { parse, FEATURE_CUSTOM_ELEMENT } = astroParser; const traverse: typeof babelTraverse.default = (babelTraverse.default as any).default; // @ts-ignore diff --git a/packages/astro/src/compiler/index.ts b/packages/astro/src/compiler/index.ts index 5f295e044..5af082914 100644 --- a/packages/astro/src/compiler/index.ts +++ b/packages/astro/src/compiler/index.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; import type { CompileResult, TransformResult } from '../@types/astro'; import type { CompileOptions } from '../@types/compiler.js'; diff --git a/packages/astro/src/config.ts b/packages/astro/src/config.ts index f4315aff6..a1e5556a4 100644 --- a/packages/astro/src/config.ts +++ b/packages/astro/src/config.ts @@ -1,6 +1,4 @@ import type { AstroConfig } from './@types/astro'; - -import 'source-map-support/register.js'; import path from 'path'; import { existsSync } from 'fs'; diff --git a/packages/astro/src/dev.ts b/packages/astro/src/dev.ts index f6a765ba4..f85d4dda1 100644 --- a/packages/astro/src/dev.ts +++ b/packages/astro/src/dev.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; import type { AstroConfig } from './@types/astro'; import type { LogOptions } from './logger.js'; @@ -8,7 +7,7 @@ import path from 'path'; import { performance } from 'perf_hooks'; import { defaultLogDestination, defaultLogLevel, debug, error, info, parseError } from './logger.js'; import { createRuntime } from './runtime.js'; -import { stopTimer } from './build/util'; +import { stopTimer } from './build/util.js'; const hostname = '127.0.0.1'; diff --git a/packages/astro/src/logger.ts b/packages/astro/src/logger.ts index 88f591b4d..7ded0696f 100644 --- a/packages/astro/src/logger.ts +++ b/packages/astro/src/logger.ts @@ -1,4 +1,3 @@ -import 'source-map-support/register.js'; import type { CompileError } from '@astrojs/parser'; import { bold, blue, red, grey, underline, yellow } from 'kleur/colors'; import { Writable } from 'stream'; diff --git a/packages/astro/src/runtime.ts b/packages/astro/src/runtime.ts index 52601216a..545c2f7b7 100644 --- a/packages/astro/src/runtime.ts +++ b/packages/astro/src/runtime.ts @@ -1,6 +1,6 @@ -import 'source-map-support/register.js'; import type { LogOptions } from './logger'; import type { AstroConfig, CollectionResult, CollectionRSS, CreateCollection, Params, RuntimeMode } from './@types/astro'; +import type { CompileError as ICompileError } from '@astrojs/parser'; import resolve from 'resolve'; import { existsSync, promises as fs } from 'fs'; @@ -16,7 +16,8 @@ import { SnowpackConfig, startServer as startSnowpackServer, } from 'snowpack'; -import { CompileError } from '@astrojs/parser'; +import parser from '@astrojs/parser'; +const { CompileError } = parser; import { canonicalURL, getSrcPath, stopTimer } from './build/util.js'; import { debug, info } from './logger.js'; import { configureSnowpackLogger } from './snowpack-logger.js'; @@ -49,9 +50,9 @@ type LoadResultSuccess = { type LoadResultNotFound = { statusCode: 404; error: Error; collectionInfo?: CollectionInfo }; type LoadResultRedirect = { statusCode: 301 | 302; location: string; collectionInfo?: CollectionInfo }; type LoadResultError = { statusCode: 500 } & ( - | { type: 'parse-error'; error: CompileError } + | { type: 'parse-error'; error: ICompileError } | { type: 'ssr'; error: Error } - | { type: 'not-found'; error: CompileError } + | { type: 'not-found'; error: ICompileError } | { type: 'unknown'; error: Error } ); diff --git a/packages/astro/src/search.ts b/packages/astro/src/search.ts index c9f4f436b..5e15d2817 100644 --- a/packages/astro/src/search.ts +++ b/packages/astro/src/search.ts @@ -1,6 +1,4 @@ import type { AstroConfig } from './@types/astro'; - -import 'source-map-support/register.js'; import { existsSync } from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; diff --git a/packages/markdown-support/package.json b/packages/markdown-support/package.json index bf7e7d5c9..d5f10add1 100644 --- a/packages/markdown-support/package.json +++ b/packages/markdown-support/package.json @@ -13,7 +13,7 @@ }, "scripts": { "prepublish": "yarn build", - "build": "astro-scripts build --format cjs \"src/**/*.ts\" && tsc -p tsconfig.json", + "build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json", "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { diff --git a/packages/markdown-support/tsconfig.json b/packages/markdown-support/tsconfig.json index 13d42008a..c56abb57e 100644 --- a/packages/markdown-support/tsconfig.json +++ b/packages/markdown-support/tsconfig.json @@ -4,7 +4,7 @@ "compilerOptions": { "allowJs": true, "target": "ES2020", - "module": "CommonJS", + "module": "ES2020", "outDir": "./dist" } } diff --git a/scripts/cmd/build.js b/scripts/cmd/build.js index dd2b2fd82..27941b514 100644 --- a/scripts/cmd/build.js +++ b/scripts/cmd/build.js @@ -7,11 +7,10 @@ import glob from 'tiny-glob'; /** @type {import('esbuild').BuildOptions} */ const defaultConfig = { - bundle: true, minify: false, format: 'esm', platform: 'node', - target: 'node14', + target: 'node14.16.1', sourcemap: 'inline', sourcesContent: false, }; @@ -26,16 +25,15 @@ export default async function build(...args) { const { type = 'module', dependencies = {} } = await fs.readFile('./package.json').then((res) => JSON.parse(res.toString())); const format = type === 'module' ? 'esm' : 'cjs'; - const external = [...Object.keys(dependencies), '@astrojs/language-server/bin/server.js', 'source-map-support', 'source-map-support/register.js', 'vscode']; const outdir = 'dist'; await clean(outdir); if (!isDev) { await esbuild.build({ ...config, + bundle: entryPoints.length === 1, // Note: only use `bundle` with a single entrypoint! entryPoints, outdir, - external, format, plugins: [svelte({ isDev })], }); diff --git a/yarn.lock b/yarn.lock index 0917f82eb..6ccd19aed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9895,7 +9895,7 @@ source-map-js@^0.6.2: resolved "https://registry.npmjs.org/source-map-js/-/source-map-js-0.6.2.tgz" integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug== -source-map-support@^0.5.19, source-map-support@~0.5.19: +source-map-support@~0.5.19: version "0.5.19" resolved "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz" integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==