Skip to content

Commit

Permalink
feat(dev): build dev preview site on build
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrevda committed Oct 6, 2023
1 parent 5e12523 commit 3aa3432
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 40 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/buildDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ on: push

env:
CI: true
NODE_VER: 19
NODE_VER: 20
NETLIFY_SITE_ID: 89194942-bd48-4c23-a181-7e489c17eabc

jobs:
lint:
Expand Down Expand Up @@ -75,6 +76,25 @@ jobs:
- name: test built libs
run: npm run test:dist

- name: Deploy visual tests to Netlify
uses: nwtgck/actions-netlify@v2
id: deploy-to-netlify
with:
publish-dir: dist/dev
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: '[${{ github.ref_name }}@${{ env.GIT_SHORT_REVISION }}] ${{ env.GIT_SHORT_COMMIT_MESSAGE }}'
enable-pull-request-comment: false
enable-commit-comment: false
enable-commit-status: false
# Use custom alias for non master branch Deploy Previews only
alias: ${{ github.ref_name != 'master' && env.RUN_NAME || '' }}
github-deployment-environment: '[${{ env.GIT_BRANCH }}] visual tests'
fails-without-credentials: true
production-deploy: ${{ github.ref_name == 'master' }}
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ env.NETLIFY_SITE_ID }}

deploy:
runs-on: ubuntu-latest
needs: [lint, test]
Expand Down
53 changes: 42 additions & 11 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {context, build} from 'esbuild'
import {rm} from 'node:fs/promises'
import url from 'node:url'
import {context, build} from 'esbuild'
import open from 'open'

await rm('./dist', {recursive: true, force: true})
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const distOutdir = './dist/dev'

const buildOpts = {
entryPoints: ['./src/index.tsx', './src/Img.tsx', './src/useImage.tsx'],
Expand All @@ -16,16 +19,44 @@ const buildOpts = {
jsx: 'automatic',
}

// build esm version
await build(buildOpts)
const devBuildOpts = {
entryPoints: ['./dev/app.tsx', './dev/index.html', './dev/sw.js'],
bundle: true,
splitting: true,
outdir: distOutdir,
format: 'esm',
sourcemap: true,
minify: process.env.NODE_ENV !== 'development',
jsxDev: true,
jsx: 'automatic',
loader: {'.html': 'copy'},
}

await rm('./dist', {recursive: true, force: true})

// build cjs version
await build({
...buildOpts,
format: 'cjs',
outdir: './dist/cjs',
splitting: false,
})
if (process.env.NODE_ENV !== 'development') {
// build esm version
await Promise.all([
build(buildOpts),

// build cjs version
build({
...buildOpts,
format: 'cjs',
outdir: './dist/cjs',
splitting: false,
}),

// build dev site
build(devBuildOpts),
])
} else {
const ctx = await context(devBuildOpts)
await ctx.watch()
let {port} = await ctx.serve({servedir: distOutdir})
open(`http://localhost:${port}`)
await ctx.dispose()
}

process.on('unhandledRejection', console.error)
process.on('uncaughtException', console.error)
16 changes: 0 additions & 16 deletions dev/Preloader.tsx

This file was deleted.

Binary file removed dev/cat.jpg
Binary file not shown.
22 changes: 17 additions & 5 deletions dev/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import url from 'node:url'
import {parseArgs} from 'node:util'
import {context} from 'esbuild'
import {rm} from 'node:fs/promises'
import open from 'open'

// const app = express()
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const outdir = __dirname + '../dist'
const outdir = __dirname + '../dist/dev'
const {
values: {build},
} = parseArgs({
options: {
build: {type: 'boolean', default: false},
},
})

await rm(outdir, {recursive: true, force: true})

Expand All @@ -27,10 +35,14 @@ const buildOpts = {
}

const ctx = await context(buildOpts)
await ctx.watch()
let {port} = await ctx.serve({servedir: outdir})
open(`http://localhost:${port}`)

if (!build) {
await ctx.watch()
let {port} = await ctx.serve({servedir: outdir})
open(`http://localhost:${port}`)
} else {
await ctx.rebuild()
await ctx.dispose()
}
process.on('unhandledRejection', console.error)
process.on('uncaughtException', console.error)
process.on('exit', () => ctx && ctx.dispose())
Binary file removed dev/loader.gif
Binary file not shown.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"version": "4.1.0",
"description": "React Image is an <img> tag replacement for react, featuring preloader and multiple image fallback support",
"scripts": {
"build": "npm run build:types && node build.js",
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
"build": "node build.js && npm run build:types && mv dist/src/*.d.ts dist",
"build:types": "tsc -p tsconfig.json",
"codecov": "codecov",
"dev": "node --watch dev/index.js",
"dev": "NODE_ENV=development node --watch build.js",
"pretty": "prettier \"**/*.{?(cj|mj|t|j)s?(on|on5|x),y?(a)ml,graphql,md,css}\" --write",
"isNewerThanPublished": "semver `npm -s view $npm_package_name dist-tags.${TAG:-latest}` --range \"<$npm_package_version\" > /dev/null && echo true || echo false",
"test": "jest",
Expand Down Expand Up @@ -46,7 +46,7 @@
],
"module": "dist/esm/index.js",
"main": "dist/cjs/index.js",
"types": "react-image.d.ts",
"types": "dist/index.d.ts",
"type": "module",
"author": "[email protected]",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Img, {ImgProps} from './Img'
import useImage, {useImageProps} from './useImage'
export {Img, useImage, ImgProps, useImageProps}
export {Img, useImage, type ImgProps, type useImageProps}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"compilerOptions": {
"declaration": true, // emit declerations
"declarationDir": ".", // to this dir
"outDir": "./jsSrc", // but all other code to this dir
"emitDeclarationOnly": true, // emit declerations for all files

Check failure on line 4 in tsconfig.json

View workflow job for this annotation

GitHub Actions / lint

Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'.
"declarationDir": "./dist", // to this dir
"outDir": "./dist", // but all other code to this dir
"sourceMap": true, // allow sourcemap support
"strictNullChecks": true, // enable strict null checks as a best practice
// "module": "es6", // specify module code generation
Expand Down

0 comments on commit 3aa3432

Please sign in to comment.