Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding esbuild #167

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"plugin:prettier/recommended"
],
"rules": {
"no-shadow": "off",
"@typescript-eslint/no-shadow": ["error"],
"@typescript-eslint/no-use-before-define": 0,
"class-methods-use-this": 0,
"no-useless-constructor": 0,
Expand All @@ -55,6 +57,12 @@
}
]
}
},
{
"files": ["esbuild/**/*"],
"rules": {
"no-console": "off"
}
}
],

Expand Down Expand Up @@ -85,7 +93,7 @@
"comma-dangle": ["error", "always-multiline"],
"import/no-extraneous-dependencies": [
"error",
{ "devDependencies": ["**/*.test.js", "**/*.test.ts", "**/testutils/**", "cypress.config.ts"] }
{ "devDependencies": ["**/*.test.js", "**/*.test.ts", "**/testutils/**", "cypress.config.ts", "esbuild/**/*"] }
],
"no-only-tests/no-only-tests": "error",
"prettier/prettier": [
Expand Down
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ENV GIT_BRANCH ${GIT_BRANCH}

COPY package*.json ./
RUN CYPRESS_INSTALL_BINARY=0 npm ci --no-audit
ENV NODE_ENV='production'

COPY . .
RUN npm run build
Expand All @@ -49,17 +50,13 @@ COPY --from=build --chown=appuser:appgroup \
/app/package-lock.json \
./

COPY --from=build --chown=appuser:appgroup \
/app/assets ./assets

COPY --from=build --chown=appuser:appgroup \
/app/dist ./dist

COPY --from=build --chown=appuser:appgroup \
/app/node_modules ./node_modules

EXPOSE 3000
ENV NODE_ENV='production'
USER 2000

CMD [ "npm", "start" ]
3 changes: 0 additions & 3 deletions assets/js/govukFrontendInit.js

This file was deleted.

9 changes: 9 additions & 0 deletions assets/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as govukFrontend from 'govuk-frontend'
import * as mojFrontend from '@ministryofjustice/frontend'
import * as componentsTable from './componentsTable'
import * as dependenciesTable from './dependenciesTable'
import * as driftRadiator from './driftRadiator'
import * as monitor from './monitor'

govukFrontend.initAll()
mojFrontend.initAll()
1 change: 0 additions & 1 deletion assets/js/mojFrontendInit.js

This file was deleted.

6 changes: 3 additions & 3 deletions assets/scss/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ $path: "/assets/images/";
$moj-page-width: 1170px;
$govuk-page-width: $moj-page-width;

@import 'govuk/all';
@import 'moj/all';
@import "govuk-frontend/dist/govuk/all";
@import "@ministryofjustice/frontend/moj/all";

@import './components/header-bar';
@import 'assets/scss/local';
@import './local';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing newline missing

26 changes: 26 additions & 0 deletions esbuild/app.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { copy } = require('esbuild-plugin-copy')
const { typecheckPlugin } = require('@jgoz/esbuild-plugin-typecheck')
const esbuild = require('esbuild')
const glob = require('glob')

module.exports = buildConfig => {
console.log('\u{1b}[1m\u{2728} Building app....\u{1b}[0m')

esbuild
.build({
entryPoints: glob.sync(buildConfig.app.entryPoints),
outdir: buildConfig.app.outDir,
bundle: false,
sourcemap: true,
platform: 'node',
format: 'cjs',
plugins: [
typecheckPlugin(),
copy({
resolveFrom: 'cwd',
assets: buildConfig.app.copy,
}),
],
})
.catch(() => process.exit(1))
}
49 changes: 49 additions & 0 deletions esbuild/assets.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { copy } = require('esbuild-plugin-copy')
const { sassPlugin } = require('esbuild-sass-plugin')
const { clean } = require('esbuild-plugin-clean')
const esbuild = require('esbuild')
const path = require('path')
const { glob } = require('glob')

const buildAdditionalAssets = buildConfig =>
esbuild.build({
outdir: buildConfig.assets.outDir,
plugins: [
copy({
resolveFrom: 'cwd',
assets: buildConfig.assets.copy,
}),
],
})

const buildAssets = buildConfig => {
return esbuild.build({
entryPoints: buildConfig.assets.entryPoints,
outdir: buildConfig.assets.outDir,
entryNames: '[ext]/app',
minify: buildConfig.isProduction,
sourcemap: !buildConfig.isProduction,
platform: 'browser',
target: 'es2018',
external: ['/assets/*'],
bundle: true,
plugins: [
clean({
patterns: glob.sync(buildConfig.assets.clear),
}),
sassPlugin({
quietDeps: true,
loadPaths: [process.cwd(), path.join(process.cwd(), 'node_modules')],
}),
],
})
}

module.exports = buildConfig => {
console.log('\u{1b}[1m\u{2728} Building assets....\u{1b}[0m')

Promise.all([buildAssets(buildConfig), buildAdditionalAssets(buildConfig)]).catch(e => {
console.log(e)
process.exit(1)
})
}
68 changes: 68 additions & 0 deletions esbuild/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const path = require('path')
const { glob } = require('glob')
const chokidar = require('chokidar')
const { spawn } = require('child_process')
const buildAssets = require('./assets.config')
const buildApp = require('./app.config')

const cwd = process.cwd()
const buildConfig = {
isProduction: process.env.NODE_ENV === 'production',
app: {
outDir: path.join(cwd, 'dist'),
entryPoints: glob
.sync([path.join(cwd, '*.ts'), path.join(cwd, 'server/**/*.ts')])
.filter(file => !file.endsWith('.test.ts')),
copy: [
{
from: path.join(cwd, 'server/views/**/*'),
to: path.join(cwd, 'dist/server/views'),
},
],
},

assets: {
outDir: path.join(cwd, 'dist/assets'),
entryPoints: glob.sync([path.join(cwd, 'assets/js/index.js'), path.join(cwd, 'assets/scss/application.scss')]),
copy: [
{
from: path.join(cwd, 'assets/images/**/*'),
to: path.join(cwd, 'dist/assets/images'),
},
],
clear: glob.sync([path.join(cwd, 'dist/assets/{css,js}')]),
},
}

function main() {
const chokidarOptions = {
persistent: true,
ignoreInitial: true,
}

const args = process.argv
if (args.includes('--build')) {
buildApp(buildConfig)
buildAssets(buildConfig)
}

if (args.includes('--dev-server')) {
let serverProcess = null
chokidar.watch(['dist']).on('all', () => {
if (serverProcess) serverProcess.kill()
serverProcess = spawn('node', ['dist/server.js'], { stdio: 'inherit' })
})
}

if (args.includes('--watch')) {
console.log('\u{1b}[1m\u{1F52D} Watching for changes...\u{1b}[0m')
// Assets
chokidar.watch(['assets/**/*'], chokidarOptions).on('all', () => buildAssets(buildConfig))

// App
chokidar
.watch(['server/**/*'], { ...chokidarOptions, ignored: ['**/*.test.ts'] })
.on('all', () => buildApp(buildConfig))
}
}
main()
Loading