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

Full reloading happening on every change (including css-only changes) #131

Open
vccoffey opened this issue Aug 13, 2024 · 2 comments
Open

Comments

@vccoffey
Copy link

My application is doing a full reload on css changes. How can I prevent that?

What version of esbuild will this work with? I'm using
"esbuild-envfile-plugin": "^1.0.3",
"esbuild-plugin-import-glob": "^0.1.1",
"esbuild-sass-plugin": "^3.3.1",
"@jgoz/esbuild-plugin-livereload": "^2.1.1",

my esbuild.config.js

/* eslint-disable no-console */

const path = require('path')
const esbuild = require('esbuild')
const esbuildEnvfilePlugin = require('esbuild-envfile-plugin')
const esbuildPluginImportGlob = require('esbuild-plugin-import-glob')
const esbuildPluginSvgr = require('@imacdonald/esbuild-plugin-svgr')
const esbuildSassPlugin = require('esbuild-sass-plugin')
const { livereloadPlugin } = require('@jgoz/esbuild-plugin-livereload')

const isWatchMode = process.argv.includes('--watch')

const onEndPlugin = {
  name: 'on-end',
  setup(build) {
    build.onEnd((result) => {
      if (result.errors.length > 0) {
        console.error('build failed with errors')
        console.log(result.errors)
      } else console.log(`build finished successfully`)
    })
  },
}

const define = {}
for (const k in process.env) {
  if (Object.prototype.hasOwnProperty.call(process.env, k))
    define[`process.env.${k}`] = JSON.stringify(process.env[k])
}

const esbuildConfig = {
  absWorkingDir: path.join(process.cwd(), 'app/javascript'),
  bundle: true,
  entryPoints: ['application.js'],
  loader: { '.png': 'dataurl' },
  metafile: true, // Enable metafile to track dependencies
  outdir: path.join(process.cwd(), 'app/assets/builds'),
  sourcemap: 'linked',
  write: true,
  plugins: [
    esbuildEnvfilePlugin,
    esbuildPluginImportGlob.default(),
    esbuildPluginSvgr(),
    esbuildSassPlugin.sassPlugin(),
    livereloadPlugin({ fullReloadOnCssUpdates: false }),
    onEndPlugin,
  ],
  define,
}

async function buildProject() {
  try {
    if (isWatchMode) {
      console.log('Starting watch mode...')
      const context = await esbuild.context(esbuildConfig)
      await context.watch()
    } else {
      await esbuild.build(esbuildConfig)
      console.log('Build complete.')
    }
  } catch (err) {
    console.error('esbuild.config.js ERROR in buildProject')
    console.error(err)
    process.exit(1)
  }
}

// Execute the build function
buildProject()
@jgoz
Copy link
Owner

jgoz commented Aug 16, 2024

Are the CSS files being included via imports from JS/TS files? If so, esbuild might be invalidating the JS portion of the dependency graph as well as the CSS portion, which would trigger a full page reload.

@vccoffey
Copy link
Author

Yes @jgoz that is correct. The css files are imported into .JSX files. Is there a way avoid the JS becoming invalidated? Coupling .scss and .jsx files in this way is a file architecture that we are pretty attached to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants