commit af8966216131f2d212f6fa0313fe3d4c7b9672ab Author: fretuks Date: Sat Oct 31 00:31:06 2026 +0100 feat: implement phase one iceberg build tool diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e3941b --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules/ +dist/ +coverage/ +.DS_Store +*.log +npm-debug.log* +*.tgz +test/fixtures/*/dist/ +work/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..34f6625 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Fretux + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..db004c2 --- /dev/null +++ b/README.md @@ -0,0 +1,71 @@ +# Iceberg + +Iceberg is an early-stage, framework-independent JavaScript build tool for production frontend builds. It uses [Rolldown](https://rolldown.rs/) for JavaScript and TypeScript bundling and [Lightning CSS](https://lightningcss.dev/) for CSS processing. + +## Current support + +- `iceberg build`, `--help`, and `--version` +- `index.html` with one local module script entry +- JavaScript, TypeScript, JSX/TSX syntax supported by Rolldown +- npm and relative ESM imports +- CSS imported from JavaScript and linked from HTML, including CSS `@import` +- common image and font asset imports +- hashed JavaScript, CSS, and emitted asset filenames +- minification, tree shaking, optional source maps, base paths, and a small `iceberg.config.js` +- programmatic `build()` and `defineConfig()` APIs + +## Installation and quick start + +```bash +npm install -D @fretuks/iceberg +``` + +```json +{ "scripts": { "build": "iceberg build" } } +``` + +Run `npm run build` from a project containing `index.html` and a module script such as `/src/main.js`. Iceberg writes production files to `dist/`. + +## Configuration + +Iceberg works without configuration. The optional `iceberg.config.js` file supports: + +```js +import { defineConfig } from '@fretuks/iceberg'; + +export default defineConfig({ + root: process.cwd(), + base: '/site/', + build: { outDir: 'build', minify: true, sourcemap: false, emptyOutDir: true } +}); +``` + +## Frost CSS + +Frost is supported through its normal npm export; Iceberg contains no Frost-specific logic: + +```bash +npm install @fretuks/frost +``` + +```js +import '@fretuks/frost'; +``` + +The inspected Frost package is `@fretuks/frost@0.3.0`. Its `.` and `./frost.css` exports point to precompiled `frost.css`, which imports the other CSS modules through ordinary CSS imports. The working example is in `examples/frost`. + +## Programmatic API + +```js +import { build, defineConfig } from '@fretuks/iceberg'; + +await build(defineConfig({ build: { outDir: 'dist', minify: true } })); +``` + +## Known limitations + +Phase 1 intentionally has no development server, HMR, preview command, framework adapters, SSR, plugin API, or scaffolding. HTML currently supports one local module script entry. CSS asset URL rewriting beyond the standard relative output layout is still conservative; projects should keep CSS assets next to the stylesheet or use imported assets where possible. + +## License + +MIT. Iceberg is in early development; APIs may evolve before the 1.0 release. diff --git a/examples/frost/index.html b/examples/frost/index.html new file mode 100644 index 0000000..e5a5f43 --- /dev/null +++ b/examples/frost/index.html @@ -0,0 +1,2 @@ + +Iceberg + Frost

Frost

diff --git a/examples/frost/package-lock.json b/examples/frost/package-lock.json new file mode 100644 index 0000000..802dbdd --- /dev/null +++ b/examples/frost/package-lock.json @@ -0,0 +1,22 @@ +{ + "name": "iceberg-frost-example", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "iceberg-frost-example", + "dependencies": { + "@fretuks/frost": "file:../../../../WebstormProjects/Frost" + } + }, + "../../../Frost": { + "name": "@fretuks/frost", + "version": "0.3.0", + "license": "MIT" + }, + "node_modules/@fretuks/frost": { + "resolved": "../../../Frost", + "link": true + } + } +} diff --git a/examples/frost/package.json b/examples/frost/package.json new file mode 100644 index 0000000..8af8aee --- /dev/null +++ b/examples/frost/package.json @@ -0,0 +1 @@ +{ "name": "iceberg-frost-example", "private": true, "type": "module", "dependencies": { "@fretuks/frost": "file:../../../../WebstormProjects/Frost" }, "scripts": { "build": "iceberg build" } } diff --git a/examples/frost/src/main.js b/examples/frost/src/main.js new file mode 100644 index 0000000..9edb987 --- /dev/null +++ b/examples/frost/src/main.js @@ -0,0 +1,2 @@ +import '@fretuks/frost'; +document.querySelector('#app').dataset.built = 'true'; diff --git a/examples/vanilla/index.html b/examples/vanilla/index.html new file mode 100644 index 0000000..983e5a8 --- /dev/null +++ b/examples/vanilla/index.html @@ -0,0 +1,5 @@ + + + Iceberg Vanilla +
+ diff --git a/examples/vanilla/package.json b/examples/vanilla/package.json new file mode 100644 index 0000000..acce1eb --- /dev/null +++ b/examples/vanilla/package.json @@ -0,0 +1 @@ +{ "name": "iceberg-vanilla-example", "private": true, "type": "module", "scripts": { "build": "iceberg build" } } diff --git a/examples/vanilla/src/components/card.css b/examples/vanilla/src/components/card.css new file mode 100644 index 0000000..50bc23c --- /dev/null +++ b/examples/vanilla/src/components/card.css @@ -0,0 +1 @@ +.card { padding: 2rem; } diff --git a/examples/vanilla/src/main.js b/examples/vanilla/src/main.js new file mode 100644 index 0000000..01a653c --- /dev/null +++ b/examples/vanilla/src/main.js @@ -0,0 +1,3 @@ +import './style.css'; +import { message } from './utils.js'; +document.querySelector('#app').textContent = message; diff --git a/examples/vanilla/src/style.css b/examples/vanilla/src/style.css new file mode 100644 index 0000000..5ff6bd3 --- /dev/null +++ b/examples/vanilla/src/style.css @@ -0,0 +1,2 @@ +@import './components/card.css'; +body { margin: 0; font-family: system-ui, sans-serif; } diff --git a/examples/vanilla/src/utils.js b/examples/vanilla/src/utils.js new file mode 100644 index 0000000..7110140 --- /dev/null +++ b/examples/vanilla/src/utils.js @@ -0,0 +1 @@ +export const message = 'Built with Iceberg'; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..81c4e9a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,659 @@ +{ + "name": "@fretuks/iceberg", + "version": "0.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@fretuks/iceberg", + "version": "0.1.0", + "license": "MIT", + "dependencies": { + "lightningcss": "^1.29.2", + "parse5": "^7.2.1", + "rolldown": "^1.2.16" + }, + "bin": { + "iceberg": "dist/cli/index.js" + }, + "devDependencies": { + "@types/node": "^22.10.2", + "typescript": "^5.7.2" + }, + "engines": { + "node": ">=20.19.0" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.156.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.156.0.tgz", + "integrity": "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/oxc-project" + } + }, + "node_modules/@rolldown/binding-android-arm-eabi": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm-eabi/-/binding-android-arm-eabi-1.2.16.tgz", + "integrity": "sha512-ia3DwAs0OsMGS7+IConptSiyk9pEXkOx/AbJ2n/lAGxH3QkWQus1t8d7gnAT38FCU2sQu8lUbeR6AiZFv4JxaA==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.16.tgz", + "integrity": "sha512-kx5wBiMB6E7TEvpl2B9gjLU004BkvEm2Dr941BFjx/SontPwSzNuQUIoVKxHHoSjpFIfHMmcguyLOtyn0hObRg==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.2.16.tgz", + "integrity": "sha512-qslZrXnKJU4cUpjoAmtAo/KQM9e2IRMlmpig6Zpwj+8KWxW8v+nkAf9216Bsek9lOC1jvg2yCjygCFo2f83msQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.2.16.tgz", + "integrity": "sha512-FdF+y48ND13eAyM2mQn+85kvAqNlvRWROy2rhgA463X/0Gv4vGtazZtfaF1mRBLscYvtOlh4QnLjOWiFzX69OQ==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.2.16.tgz", + "integrity": "sha512-ddjqFM5e95AgLbUPtKIS7V1KTVlA0RUgCVr5mn+/IEhsdy3pr+Vuj/HZ7nlMN2FyxIwUktOH1LEPIQXO1v9y1Q==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.2.16.tgz", + "integrity": "sha512-tHdKWbFkFBI687iim3LIyIvyL7j66VvQYkDPss5Stn/qyE2tWkR1YD0dXmXPFKKqT/o5ZEJVsaRsWS2meQwGOA==", + "cpu": [ + "arm" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.2.16.tgz", + "integrity": "sha512-FJiLGAblPBMlBEdbOqw6kW6QcXDWAf/oJu7gYs9ZJZwmg+K+/zjFLxPTIY9izJUzxzc24FHvmseyA0IvJSl17w==", + "cpu": [ + "arm64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.2.16.tgz", + "integrity": "sha512-hrJpblMsrQ58U9veWpbXzkhV5zStunds6HWLprqq1qauvgBr1E6P+24kLj6lSLI9vjRn/RYpjprhkHQPKGWpNg==", + "cpu": [ + "arm64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.2.16.tgz", + "integrity": "sha512-qJd/dC6j6CwLG9/s/RP2nb1RZ0GIgj7Ioj7x/SYa3RzjVrlzrmH0fhIOpfvqQDeIsdP0loi+kHaMhJzY1PBIaA==", + "cpu": [ + "ppc64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.2.16.tgz", + "integrity": "sha512-Vx7Xg5uUJuNcujJjI70OICaH0EYj80BtOLZTSN7uOYg8QOLlyKnaG0IW7Fn/J0IpJsaKzmDKMhOQv1JBM9m9vg==", + "cpu": [ + "s390x" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.2.16.tgz", + "integrity": "sha512-TNKhgE3uJTsTzatUSSYt0Kgo+Oa2uSlSjz4r1aEIea7HX6ULBym4fAlYUhG1i6pjNf2njfCUAfKwr6C3E9Xg1w==", + "cpu": [ + "x64" + ], + "libc": [ + "glibc" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.2.16.tgz", + "integrity": "sha512-7chXdpjXZ8qG0+mIxECN3cT9kvhZ3/oZhMX6Pp876P7CDrIy8nJswva6Mq357JR7eYXfuskwZxMeV1SzoZdHrA==", + "cpu": [ + "x64" + ], + "libc": [ + "musl" + ], + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.2.16.tgz", + "integrity": "sha512-Kwjt+7uNTJK5/MDkGWGVO2KQPnj8yAV75dMpfwyHZrJSXWHrniOB0aUEWlVYf3H1hOqFLIStLdM5QXq6bnKmOQ==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.2.16.tgz", + "integrity": "sha512-/LH6WfjDRXXJz/p5xl4TmU+jpcnwcxbKEQBW/GZE0JHfN0lGIyTtsUEGaQ/q/GikkGJ2yLKe6pwm7AnEWO8pdw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.2.16.tgz", + "integrity": "sha512-1lv1KvX0Z871zfk7vw+dnXMMnHNyDwMzWG8Kv3HxDXMOhzCX4SHc7HK319fInP0i+0p9D8brkfzL7pJOaGl+tw==", + "cpu": [ + "x64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz", + "integrity": "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==", + "license": "MIT" + }, + "node_modules/@types/node": { + "version": "22.20.5", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.20.5.tgz", + "integrity": "sha512-U2+DNr+wSjpsTS/wZGYHq7GcwfuSmKiKvoPvK22zwTlRhU91yOniN4qRR5KhIjvif7ysw/dz/hKmfDH0Ris4aA==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/entities": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-6.0.1.tgz", + "integrity": "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/lightningcss": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz", + "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.33.0", + "lightningcss-darwin-arm64": "1.33.0", + "lightningcss-darwin-x64": "1.33.0", + "lightningcss-freebsd-x64": "1.33.0", + "lightningcss-linux-arm-gnueabihf": "1.33.0", + "lightningcss-linux-arm64-gnu": "1.33.0", + "lightningcss-linux-arm64-musl": "1.33.0", + "lightningcss-linux-x64-gnu": "1.33.0", + "lightningcss-linux-x64-musl": "1.33.0", + "lightningcss-win32-arm64-msvc": "1.33.0", + "lightningcss-win32-x64-msvc": "1.33.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.33.0.tgz", + "integrity": "sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==", + "cpu": [ + "arm64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.33.0.tgz", + "integrity": "sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==", + "cpu": [ + "arm64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.33.0.tgz", + "integrity": "sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==", + "cpu": [ + "x64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.33.0.tgz", + "integrity": "sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==", + "cpu": [ + "x64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.33.0.tgz", + "integrity": "sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==", + "cpu": [ + "arm" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.33.0.tgz", + "integrity": "sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==", + "cpu": [ + "arm64" + ], + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.33.0.tgz", + "integrity": "sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==", + "cpu": [ + "arm64" + ], + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.33.0.tgz", + "integrity": "sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==", + "cpu": [ + "x64" + ], + "libc": [ + "glibc" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.33.0.tgz", + "integrity": "sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==", + "cpu": [ + "x64" + ], + "libc": [ + "musl" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.33.0.tgz", + "integrity": "sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==", + "cpu": [ + "arm64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.33.0.tgz", + "integrity": "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==", + "cpu": [ + "x64" + ], + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/parse5": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-7.3.0.tgz", + "integrity": "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==", + "license": "MIT", + "dependencies": { + "entities": "^6.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/rolldown": { + "version": "1.2.16", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.16.tgz", + "integrity": "sha512-bWvlMpFKiTfDKfnK7FC2MPgPeWCf/Kpwsq49ApdW+FQaBZb6O6dZ4MAPbPUjp7hYQrw/RobqCzn64BRrQecGHw==", + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.156.0", + "@rolldown/pluginutils": "^1.0.0" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm-eabi": "1.2.16", + "@rolldown/binding-android-arm64": "1.2.16", + "@rolldown/binding-darwin-arm64": "1.2.16", + "@rolldown/binding-darwin-x64": "1.2.16", + "@rolldown/binding-freebsd-x64": "1.2.16", + "@rolldown/binding-linux-arm-gnueabihf": "1.2.16", + "@rolldown/binding-linux-arm64-gnu": "1.2.16", + "@rolldown/binding-linux-arm64-musl": "1.2.16", + "@rolldown/binding-linux-ppc64-gnu": "1.2.16", + "@rolldown/binding-linux-s390x-gnu": "1.2.16", + "@rolldown/binding-linux-x64-gnu": "1.2.16", + "@rolldown/binding-linux-x64-musl": "1.2.16", + "@rolldown/binding-openharmony-arm64": "1.2.16", + "@rolldown/binding-win32-arm64-msvc": "1.2.16", + "@rolldown/binding-win32-x64-msvc": "1.2.16" + } + }, + "node_modules/typescript": { + "version": "5.9.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", + "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, + "license": "MIT" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..d93af87 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "@fretuks/iceberg", + "version": "0.1.0", + "description": "A lightweight production build tool powered by Rolldown.", + "type": "module", + "bin": { "iceberg": "./dist/cli/index.js" }, + "main": "./dist/index.js", + "types": "./dist/index.d.ts", + "exports": { + ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" }, + "./package.json": "./package.json" + }, + "files": ["dist", "README.md", "LICENSE"], + "engines": { "node": ">=20.19.0" }, + "publishConfig": { "access": "public" }, + "repository": { "type": "git", "url": "https://git.bachmannj.ch/Fretux/Iceberg.git" }, + "license": "MIT", + "scripts": { + "build": "tsc -p tsconfig.json", + "prepack": "npm run build", + "test": "node --test", + "pack:check": "npm pack --dry-run" + }, + "dependencies": { + "lightningcss": "^1.29.2", + "parse5": "^7.2.1", + "rolldown": "^1.2.16" + }, + "devDependencies": { + "@types/node": "^22.10.2", + "typescript": "^5.7.2" + } +} diff --git a/src/bundler/index.ts b/src/bundler/index.ts new file mode 100644 index 0000000..4ae0900 --- /dev/null +++ b/src/bundler/index.ts @@ -0,0 +1,11 @@ +import path from 'node:path'; +import { mkdir, readdir, rm, writeFile } from 'node:fs/promises'; +import { bundleCss, bundleJavaScript } from './rolldown.js'; +import { loadConfig, type IcebergConfig } from '../core/config.js'; +import { readHtmlEntry, rewriteHtml } from '../core/html.js'; +import { logger } from '../core/logger.js'; +export interface BuildResult { root: string; outDir: string; files: string[]; duration: number; } +export async function build(inlineConfig?: IcebergConfig): Promise { const started = Date.now(); const config = await loadConfig(process.cwd(), inlineConfig); const outDir = path.resolve(config.root, config.build.outDir); const { html, entry } = await readHtmlEntry(config.root); if (config.build.emptyOutDir) await emptyDirectory(outDir); await mkdir(outDir, { recursive: true }); const jsResult = await bundleJavaScript(config, resolveHtmlPath(config.root, entry.script)); const linkedCssFiles: string[] = []; for (const style of entry.styles) { const result = await bundleCss(resolveHtmlPath(config.root, style), config); const target = path.join(outDir, result.fileName); await mkdir(path.dirname(target), { recursive: true }); await writeFile(target, result.code); linkedCssFiles.push(result.fileName); } await writeFile(path.join(outDir, 'index.html'), rewriteHtml(html, jsResult.jsFile, jsResult.cssFiles, linkedCssFiles, config.base)); const files = await listFiles(outDir); logger.info(`built ${files.length} files in ${Date.now() - started}ms`); return { root: config.root, outDir, files, duration: Date.now() - started }; } +function resolveHtmlPath(root: string, value: string) { if (value.startsWith('/')) return path.resolve(root, value.slice(1)); if (/^https?:\/\//.test(value)) throw new Error(`Remote asset is not supported: ${value}`); return path.resolve(root, value); } +async function emptyDirectory(dir: string) { await mkdir(dir, { recursive: true }); for (const item of await readdir(dir)) await rm(path.join(dir, item), { recursive: true, force: true }); } +async function listFiles(dir: string, prefix = ''): Promise { const result: string[] = []; for (const entry of await readdir(dir, { withFileTypes: true })) { const relative = path.join(prefix, entry.name); if (entry.isDirectory()) result.push(...await listFiles(path.join(dir, entry.name), relative)); else result.push(relative); } return result; } diff --git a/src/bundler/rolldown.ts b/src/bundler/rolldown.ts new file mode 100644 index 0000000..2a6cd83 --- /dev/null +++ b/src/bundler/rolldown.ts @@ -0,0 +1,16 @@ +import path from 'node:path'; +import { readFile } from 'node:fs/promises'; +import { createHash } from 'node:crypto'; +import { rolldown, type Plugin, type RolldownOutput } from 'rolldown'; +import { bundleAsync } from 'lightningcss'; +import type { ResolvedIcebergConfig } from '../core/config.js'; +const cssExtensions = /\.css$/i; const assetExtensions = /\.(png|jpe?g|gif|svg|webp|avif|woff2?|ttf|otf|ico)$/i; +export interface BundleResult { output: RolldownOutput; jsFile: string; cssFiles: string[]; } +export async function bundleJavaScript(config: ResolvedIcebergConfig, input: string): Promise { + let css = ''; const cssFiles: string[] = []; + const virtualFiles = new Map(); + const plugin: Plugin = { name: 'iceberg-css-and-assets', async resolveId(source, importer) { const resolved = importer ? await this.resolve(source, importer, { skipSelf: true }) : null; const file = resolved?.id ?? ((cssExtensions.test(source) || assetExtensions.test(source)) ? path.resolve(config.root, source) : null); if (!file || (!cssExtensions.test(file) && !assetExtensions.test(file))) return null; const kind = cssExtensions.test(file) ? 'css' : 'asset'; const virtual = `\0iceberg-${kind}:${virtualFiles.size}`; virtualFiles.set(virtual, { kind, file }); return virtual; }, async load(id) { const virtual = virtualFiles.get(id); if (!virtual) return null; if (virtual.kind === 'css') { const result = await bundleAsync({ filename: virtual.file, minify: config.build.minify, sourceMap: config.build.sourcemap }); css += Buffer.from(result.code).toString('utf8') + '\n'; return 'export default undefined;'; } const reference = this.emitFile({ type: 'asset', name: path.basename(virtual.file), source: await readFile(virtual.file) }); return `export default import.meta.ROLLUP_FILE_URL_${reference};`; }, generateBundle() { if (css) { const hash = createHash('sha256').update(css).digest('hex').slice(0, 8); const fileName = `assets/index-${hash}.css`; this.emitFile({ type: 'asset', fileName, source: css }); cssFiles.push(fileName); } } }; + const bundle = await rolldown({ input, plugins: [plugin], treeshake: true, moduleTypes: { '.ts': 'ts', '.tsx': 'tsx', '.jsx': 'jsx', '.json': 'json' } }); + try { const output = await bundle.write({ dir: config.build.outDir, format: 'esm', sourcemap: config.build.sourcemap, minify: config.build.minify, entryFileNames: 'assets/[name]-[hash].js', chunkFileNames: 'assets/[name]-[hash].js', assetFileNames: 'assets/[name]-[hash][extname]' }); const jsChunk = output.output.find(item => item.type === 'chunk' && item.isEntry); if (!jsChunk) throw new Error('Rolldown did not produce an entry chunk.'); return { output, jsFile: jsChunk.fileName, cssFiles }; } finally { await bundle.close(); } +} +export async function bundleCss(file: string, config: ResolvedIcebergConfig) { const result = await bundleAsync({ filename: file, minify: config.build.minify, sourceMap: config.build.sourcemap }); return { fileName: `assets/${path.basename(file, '.css')}.css`, code: Buffer.from(result.code).toString('utf8') }; } diff --git a/src/cli/index.ts b/src/cli/index.ts new file mode 100644 index 0000000..0ff1dea --- /dev/null +++ b/src/cli/index.ts @@ -0,0 +1,8 @@ +#!/usr/bin/env node +import { build } from '../index.js'; +import { logger } from '../core/logger.js'; +const args = process.argv.slice(2); +if (args.includes('--help') || args.length === 0) { console.log('Usage: iceberg build\n\nCommands:\n build Build index.html and its module graph\n\nOptions:\n --help Show this help\n --version Show the installed version'); process.exit(0); } +if (args.includes('--version')) { const packageJson = await import('../../package.json', { with: { type: 'json' } }); console.log(packageJson.default.version); process.exit(0); } +if (args[0] !== 'build' || args.length > 1) { logger.error('Unknown command. Only `iceberg build` is implemented in Phase 1.'); process.exit(1); } +try { await build(); } catch (error) { logger.error(error instanceof Error ? error.message : String(error)); process.exitCode = 1; } diff --git a/src/core/config.ts b/src/core/config.ts new file mode 100644 index 0000000..df8f60d --- /dev/null +++ b/src/core/config.ts @@ -0,0 +1,20 @@ +import { access } from 'node:fs/promises'; +import path from 'node:path'; + +export interface IcebergBuildOptions { outDir?: string; minify?: boolean; sourcemap?: boolean; emptyOutDir?: boolean; } +export interface IcebergConfig { root?: string; base?: string; build?: IcebergBuildOptions; } +export interface ResolvedIcebergConfig { root: string; base: string; build: Required; } +export function defineConfig(config: IcebergConfig): IcebergConfig { return config; } +export async function loadConfig(cwd: string, inline?: IcebergConfig): Promise { + const configPath = path.join(cwd, 'iceberg.config.js'); let fileConfig: IcebergConfig = {}; + try { await access(configPath); const loaded = await import(`${new URL(`file://${configPath.replace(/\\/g, '/')}`).href}?t=${Date.now()}`); fileConfig = loaded.default ?? loaded; } + catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; } + const merged = { ...fileConfig, ...inline, build: { ...fileConfig.build, ...inline?.build } }; + const root = path.resolve(cwd, merged.root ?? '.'); + const build = { outDir: merged.build?.outDir ?? 'dist', minify: merged.build?.minify ?? true, sourcemap: merged.build?.sourcemap ?? false, emptyOutDir: merged.build?.emptyOutDir ?? true }; + if (merged.base !== undefined && typeof merged.base !== 'string') throw new Error('Iceberg config: base must be a string.'); + if (typeof build.outDir !== 'string' || !build.outDir) throw new Error('Iceberg config: build.outDir must be a non-empty string.'); + for (const key of ['minify', 'sourcemap', 'emptyOutDir'] as const) if (typeof build[key] !== 'boolean') throw new Error(`Iceberg config: build.${key} must be a boolean.`); + return { root, base: normalizeBase(merged.base ?? '/'), build }; +} +function normalizeBase(base: string) { const normalized = base.replace(/\\/g, '/'); return `/${normalized.replace(/^\/+|\/+$/g, '')}/`.replace(/^\/\/$/, '/'); } diff --git a/src/core/errors.ts b/src/core/errors.ts new file mode 100644 index 0000000..a402fbd --- /dev/null +++ b/src/core/errors.ts @@ -0,0 +1 @@ +export class IcebergError extends Error { constructor(message: string, options?: { cause?: unknown }) { super(message, options); this.name = 'IcebergError'; } } diff --git a/src/core/html.ts b/src/core/html.ts new file mode 100644 index 0000000..cd7ee5f --- /dev/null +++ b/src/core/html.ts @@ -0,0 +1,27 @@ +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { parse, serialize } from 'parse5'; +import { IcebergError } from './errors.js'; +type Node = { nodeName?: string; tagName?: string; attrs?: Array<{ name: string; value: string }>; childNodes?: Node[]; parentNode?: Node }; +export interface HtmlEntry { file: string; script: string; styles: string[]; } +export async function readHtmlEntry(root: string): Promise<{ html: string; entry: HtmlEntry }> { + const htmlFile = path.join(root, 'index.html'); let html: string; + try { html = await readFile(htmlFile, 'utf8'); } catch { throw new IcebergError(`Could not find HTML entry point: ${htmlFile}`); } + const document = parse(html) as unknown as Node; const scripts: string[] = []; const styles: string[] = []; + walk(document, node => { if (node.tagName === 'script') { const type = attr(node, 'type'); const src = attr(node, 'src'); if (src && (!type || type === 'module')) scripts.push(src); else if (src && type !== 'module') throw new IcebergError(`Unsupported script type in ${htmlFile}: ${type}`); } if (node.tagName === 'link' && attr(node, 'rel')?.toLowerCase() === 'stylesheet') { const href = attr(node, 'href'); if (href) styles.push(href); } }); + if (!scripts.length) throw new IcebergError('index.html must contain a module script with a src attribute.'); + if (scripts.length > 1) throw new IcebergError('Phase 1 supports one module script entry in index.html.'); + if (/^https?:\/\//.test(scripts[0]!)) throw new IcebergError('Remote module scripts are not supported.'); + return { html, entry: { file: htmlFile, script: scripts[0]!, styles } }; +} +export function rewriteHtml(html: string, jsFile: string, importedCssFiles: string[], linkedCssFiles: string[], base: string): string { + const document = parse(html) as unknown as Node; let linkedIndex = 0; + walk(document, node => { if (node.tagName === 'script' && attr(node, 'src')) setAttr(node, 'src', joinBase(base, jsFile)); if (node.tagName === 'link' && attr(node, 'rel')?.toLowerCase() === 'stylesheet') { const replacement = linkedCssFiles[linkedIndex++]; if (replacement) setAttr(node, 'href', joinBase(base, replacement)); } }); + const head = find(document, 'head'); if (head) for (const css of importedCssFiles) head.childNodes!.push({ nodeName: 'link', tagName: 'link', attrs: [{ name: 'rel', value: 'stylesheet' }, { name: 'href', value: joinBase(base, css) }], childNodes: [], parentNode: head }); + return serialize(document as any); +} +function walk(node: Node, visit: (node: Node) => void) { visit(node); for (const child of node.childNodes ?? []) { child.parentNode = node; walk(child, visit); } } +function find(node: Node, tag: string): Node | undefined { if (node.tagName === tag) return node; for (const child of node.childNodes ?? []) { const found = find(child, tag); if (found) return found; } } +function attr(node: Node, name: string) { return node.attrs?.find(item => item.name === name)?.value; } +function setAttr(node: Node, name: string, value: string) { const found = node.attrs?.find(item => item.name === name); if (found) found.value = value; else node.attrs?.push({ name, value }); } +function joinBase(base: string, file: string) { return `${base.replace(/\/$/, '')}/${file.replace(/^\//, '')}`; } diff --git a/src/core/logger.ts b/src/core/logger.ts new file mode 100644 index 0000000..71b4e1b --- /dev/null +++ b/src/core/logger.ts @@ -0,0 +1 @@ +export const logger = { info: (message: string) => console.log(`[iceberg] ${message}`), error: (message: string) => console.error(`[iceberg] ${message}`) }; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..a8778cc --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +export { build } from './bundler/index.js'; +export { defineConfig } from './core/config.js'; +export type { BuildResult } from './bundler/index.js'; +export type { IcebergConfig, IcebergBuildOptions, ResolvedIcebergConfig } from './core/config.js'; diff --git a/tests/build.test.mjs b/tests/build.test.mjs new file mode 100644 index 0000000..50d111e --- /dev/null +++ b/tests/build.test.mjs @@ -0,0 +1,12 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtemp, mkdir, readFile, readdir, writeFile } from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import { build } from '../dist/index.js'; + +async function fixture(files) { const root = await mkdtemp(path.join(os.tmpdir(), 'iceberg-')); for (const [name, content] of Object.entries(files)) { const file = path.join(root, name); await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, content); } return root; } +async function inRoot(root, fn) { const previous = process.cwd(); process.chdir(root); try { return await fn(); } finally { process.chdir(previous); } } +test('builds JavaScript and CSS and rewrites HTML', async () => { const root = await fixture({ 'index.html': 'Keep me', 'src/main.js': "import './style.css'; document.body.dataset.ready = 'yes';", 'src/style.css': '.unused { color: red } body { color: blue; }' }); await inRoot(root, () => build()); const html = await readFile(path.join(root, 'dist/index.html'), 'utf8'); assert.match(html, /Keep me<\/title>/); assert.match(html, /assets\/main-[^"/]+\.js/); assert.match(html, /assets\/index-[^"/]+\.css/); const assets = await readdir(path.join(root, 'dist/assets')); const css = await readFile(path.join(root, 'dist/assets', assets.find(file => file.endsWith('.css'))), 'utf8'); assert.match(css, /body/); }); +test('supports TypeScript and custom output configuration', async () => { const root = await fixture({ 'index.html': '<script type="module" src="/src/main.ts"></script>', 'src/main.ts': 'const answer: number = 42; console.log(answer);', 'iceberg.config.js': "export default { build: { outDir: 'build', minify: false, sourcemap: true } };" }); await inRoot(root, () => build()); const files = await readdir(path.join(root, 'build/assets')); assert.ok(files.some(file => file.endsWith('.js'))); assert.ok(files.some(file => file.endsWith('.js.map'))); const js = await readFile(path.join(root, 'build/assets', files.find(file => file.endsWith('.js'))), 'utf8'); assert.match(js, /42/); assert.doesNotMatch(js, /: number/); }); +test('rejects an HTML entry without a module script', async () => { const root = await fixture({ 'index.html': '<h1>No script</h1>' }); await assert.rejects(() => inRoot(root, () => build()), /module script/); }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..befa7d4 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "strict": true, + "declaration": true, + "sourceMap": true, + "outDir": "dist", + "rootDir": "src", + "esModuleInterop": true, + "skipLibCheck": true, + "noUncheckedIndexedAccess": true + }, + "include": ["src", "tests/types.d.ts"] +}