import * as esbuild from 'https://deno.land/x/esbuild@v0.14.27/mod.js' import { parse } from 'flags/mod.ts' import { join } from 'path/mod.ts' import resolvePlugin from './esbuild/resolve_plugin.js' const isProd = Deno.env.get('RAILS_ENV') === 'production' const isTest = Deno.env.get('RAILS_ENV') === 'test' const template = `

` export default async (args, { debug }) => { const { _: entrypoints, ...flags } = parse(args) const [cwd, entrypoint] = validatePaths(entrypoints) const params = { entryPoints: [entrypoint], absWorkingDir: cwd, logLevel: 'error', sourcemap: isTest ? false : 'inline', write: false, format: 'esm', // minify: true, bundle: true, plugins: [resolvePlugin({ debug })] } if (entrypoint.endsWith('.jsx')) { try { const stat = Deno.lstatSync(join(cwd, 'lib/react_shim.js')) if (stat.isFile) { params.inject = ['./lib/react_shim.js'] } } catch { // Safe to swallow as this should only throw if file does not exist. } } try { const result = await esbuild.build(params) return result.outputFiles[0].contents // } catch (e) { // if (isProd) { // return new TextEncoder().encode(` // const err = ${JSON.stringify(e.errors[0])} // const location = \`\${err.location.file}:\${err.location.line}:\${err.location.column}\` // console.error('%s at %O', err.text, location); // `) // } else { // return new TextEncoder().encode(` // class ErrorOverlay extends HTMLElement { // constructor(err) { // super() // this.root = this.attachShadow({ mode: 'open' }) // this.root.innerHTML = \`${template}\` // this.root.querySelector('.message-body').textContent = err.text.trim() // if (err.location) { // const location = \`\${err.location.file}:\${err.location.line}:\${err.location.column}\` // this.root.querySelector('.file').textContent = location // console.error('%s at %O', err.text, location) // } else { // console.error(err.text) // } // throw err // } // } // customElements.define('error-overlay', ErrorOverlay) // document.body.appendChild(new ErrorOverlay(${JSON.stringify(e.errors[0])})) // `) // } } finally { esbuild.stop() } } function validatePaths(paths) { const cwd = paths[0] const entrypoint = paths[1] if (!cwd || !entrypoint) { throw new TypeError( 'Current working directory and entrypoint are required as first and second arguments.' ) } try { const stat = Deno.lstatSync(cwd) if (!stat.isDirectory) { throw new TypeError( `Current working directory is required as the first argument - received ${cwd}` ) } } catch { throw new TypeError( `A valid working directory is required as the first argument - received ${cwd}` ) } try { const stat = Deno.lstatSync(join(cwd, entrypoint)) if (!stat.isFile) { throw new TypeError(`Entrypoint is required as the second argument - received ${entrypoint}`) } } catch { throw new TypeError( `A valid entrypoint is required as the second argument - received ${entrypoint}` ) } if (/\.(js|jsx)$/.test(entrypoint) === false) { throw new TypeError( `Only a JS/JSX entrypoint is supported with this CLI - received ${entrypoint}` ) } return [cwd, entrypoint] }