-
Notifications
You must be signed in to change notification settings - Fork 316
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
Fix ./run gui watch
#6212
Fix ./run gui watch
#6212
Changes from all commits
07cc373
7fa6aa8
ef2fe39
667ca9f
50ce6c8
7bf567d
4dea6e7
a8d668d
2716386
8d1cc60
ebbd610
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,21 +11,22 @@ | |
|
||
import * as childProcess from 'node:child_process' | ||
import * as fs from 'node:fs/promises' | ||
import * as path from 'node:path' | ||
import * as fsSync from 'node:fs' | ||
import * as pathModule from 'node:path' | ||
import * as url from 'node:url' | ||
|
||
import * as esbuild from 'esbuild' | ||
import * as esbuildPluginCopy from 'enso-copy-plugin' | ||
import * as esbuildPluginNodeGlobals from '@esbuild-plugins/node-globals-polyfill' | ||
import * as esbuildPluginNodeModules from '@esbuild-plugins/node-modules-polyfill' | ||
import esbuildPluginAlias from 'esbuild-plugin-alias' | ||
import esbuildPluginCopyDirectories from 'esbuild-plugin-copy-directories' | ||
import esbuildPluginTime from 'esbuild-plugin-time' | ||
import esbuildPluginYaml from 'esbuild-plugin-yaml' | ||
|
||
import * as utils from '../../utils.js' | ||
import * as utils from '../../utils' | ||
import BUILD_INFO from '../../build.json' assert { type: 'json' } | ||
|
||
export const THIS_PATH = path.resolve(path.dirname(url.fileURLToPath(import.meta.url))) | ||
export const THIS_PATH = pathModule.resolve(pathModule.dirname(url.fileURLToPath(import.meta.url))) | ||
|
||
// ============================= | ||
// === Environment variables === | ||
|
@@ -40,6 +41,8 @@ export interface Arguments { | |
outputPath: string | ||
/** The main JS bundle to load WASM and JS wasm-pack bundles. */ | ||
ensoglAppPath: string | ||
/** `true` if in development mode (live-reload), `false` if in production mode. */ | ||
devMode: boolean | ||
} | ||
|
||
/** | ||
|
@@ -48,9 +51,9 @@ export interface Arguments { | |
export function argumentsFromEnv(): Arguments { | ||
const wasmArtifacts = utils.requireEnv('ENSO_BUILD_GUI_WASM_ARTIFACTS') | ||
const assetsPath = utils.requireEnv('ENSO_BUILD_GUI_ASSETS') | ||
const outputPath = path.resolve(utils.requireEnv('ENSO_BUILD_GUI'), 'assets') | ||
const outputPath = pathModule.resolve(utils.requireEnv('ENSO_BUILD_GUI'), 'assets') | ||
const ensoglAppPath = utils.requireEnv('ENSO_BUILD_GUI_ENSOGL_APP') | ||
return { wasmArtifacts, assetsPath, outputPath, ensoglAppPath } | ||
return { wasmArtifacts, assetsPath, outputPath, ensoglAppPath, devMode: false } | ||
} | ||
|
||
// =================== | ||
|
@@ -68,36 +71,6 @@ function git(command: string): string { | |
return childProcess.execSync(`git ${command}`, { encoding: 'utf8' }).trim() | ||
} | ||
|
||
// ============================== | ||
// === Files to manually copy === | ||
// ============================== | ||
|
||
/** | ||
* Static set of files that are always copied to the output directory. | ||
*/ | ||
export function alwaysCopiedFiles(wasmArtifacts: string) { | ||
return [ | ||
path.resolve(THIS_PATH, 'src', 'index.html'), | ||
path.resolve(THIS_PATH, 'src', 'run.js'), | ||
path.resolve(THIS_PATH, 'src', 'style.css'), | ||
path.resolve(THIS_PATH, 'src', 'docsStyle.css'), | ||
...wasmArtifacts.split(path.delimiter), | ||
] | ||
} | ||
|
||
/** | ||
* Generator that yields all files that should be copied to the output directory. | ||
* @yields {string} The file path of the next file to be copied. | ||
*/ | ||
export async function* filesToCopyProvider(wasmArtifacts: string, assetsPath: string) { | ||
console.log('Preparing a new generator for files to copy.') | ||
yield* alwaysCopiedFiles(wasmArtifacts) | ||
for (const file of await fs.readdir(assetsPath)) { | ||
yield path.resolve(assetsPath, file) | ||
} | ||
console.log('Generator for files to copy finished.') | ||
} | ||
|
||
// ================ | ||
// === Bundling === | ||
// ================ | ||
|
@@ -106,27 +79,62 @@ export async function* filesToCopyProvider(wasmArtifacts: string, assetsPath: st | |
* Generate the builder options. | ||
*/ | ||
export function bundlerOptions(args: Arguments) { | ||
const { outputPath, ensoglAppPath, wasmArtifacts, assetsPath } = args | ||
const { outputPath, ensoglAppPath, wasmArtifacts, assetsPath, devMode } = args | ||
const buildOptions = { | ||
// Disabling naming convention because these are third-party options. | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
absWorkingDir: THIS_PATH, | ||
bundle: true, | ||
entryPoints: [path.resolve(THIS_PATH, 'src', 'index.ts')], | ||
loader: { | ||
'.html': 'copy', | ||
'.css': 'copy', | ||
'.wasm': 'copy', | ||
'.svg': 'copy', | ||
'.png': 'copy', | ||
'.ttf': 'copy', | ||
}, | ||
entryPoints: [ | ||
pathModule.resolve(THIS_PATH, 'src', 'index.ts'), | ||
pathModule.resolve(THIS_PATH, 'src', 'index.html'), | ||
pathModule.resolve(THIS_PATH, 'src', 'run.js'), | ||
pathModule.resolve(THIS_PATH, 'src', 'style.css'), | ||
pathModule.resolve(THIS_PATH, 'src', 'docsStyle.css'), | ||
...wasmArtifacts.split(pathModule.delimiter), | ||
...fsSync | ||
.readdirSync(assetsPath) | ||
.map(fileName => pathModule.resolve(assetsPath, fileName)), | ||
].map(path => ({ in: path, out: pathModule.basename(path, pathModule.extname(path)) })), | ||
outdir: outputPath, | ||
outbase: 'src', | ||
plugins: [ | ||
{ | ||
// This is a workaround that is needed | ||
// because esbuild panics when using `loader: { '.js': 'copy' }`. | ||
// See https://github.com/evanw/esbuild/issues/3041. | ||
// Setting `loader: 'copy'` prevents this file from being converted to ESM | ||
// because of the `"type": "module"` in the `package.json`. | ||
// This file MUST be in CommonJS format because it is loaded using `Function()` | ||
// in `ensogl/pack/js/src/runner/index.ts` | ||
name: 'pkg-js-is-cjs', | ||
setup: build => { | ||
build.onLoad({ filter: /\/pkg.js$/ }, async ({ path }) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we add a special rule just for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do. |
||
contents: await fs.readFile(path), | ||
loader: 'copy', | ||
})) | ||
}, | ||
}, | ||
esbuildPluginCopyDirectories(), | ||
esbuildPluginYaml.yamlPlugin({}), | ||
esbuildPluginNodeModules.NodeModulesPolyfillPlugin(), | ||
esbuildPluginNodeGlobals.NodeGlobalsPolyfillPlugin({ buffer: true, process: true }), | ||
esbuildPluginAlias({ ensogl_app: ensoglAppPath }), | ||
esbuildPluginTime(), | ||
esbuildPluginCopy.create(() => filesToCopyProvider(wasmArtifacts, assetsPath)), | ||
], | ||
define: { | ||
GIT_HASH: JSON.stringify(git('rev-parse HEAD')), | ||
GIT_STATUS: JSON.stringify(git('status --short --porcelain')), | ||
BUILD_INFO: JSON.stringify(BUILD_INFO), | ||
IS_DEV_MODE: JSON.stringify(devMode), | ||
}, | ||
sourcemap: true, | ||
minify: true, | ||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does setting loader to copy mean the file "is cjs"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will add comment -
loader: copy
prevents esbuild from processing the file, otherwise it becomes ESM which breaks the way we load the file