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

CSS/CSS Problems #23

Open
Kapsonfire-DE opened this issue Apr 8, 2021 · 1 comment
Open

CSS/CSS Problems #23

Kapsonfire-DE opened this issue Apr 8, 2021 · 1 comment

Comments

@Kapsonfire-DE
Copy link

Kapsonfire-DE commented Apr 8, 2021

Hello, i tried to get HMR working on my project by moving the rollup config from this repo to my project

import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';
import scss from "rollup-plugin-scss";
import replace from 'rollup-plugin-replace';
import hmr from 'rollup-plugin-hot'
const production = !process.env.ROLLUP_WATCH;
const isNollup = !!process.env.NOLLUP
const isWatch = !!process.env.ROLLUP_WATCH
const isLiveReload = !!process.env.LIVERELOAD

const isDev = isWatch || isLiveReload
const isProduction = !isDev

const isHot = isWatch && !isLiveReload

function serve() {
	let server;

	function toExit() {
		if (server) server.kill(0);
	}

	return {
		writeBundle() {
			if (server) return;
			server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
				stdio: ['ignore', 'inherit', 'inherit'],
				shell: true
			});

			process.on('SIGTERM', toExit);
			process.on('exit', toExit);
		}
	};
}



export default {
	input: 'src/main.ts',
	output: {
		sourcemap: true,
		format: 'iife',
		name: 'app',
		dir: 'public/build/',
		chunkFileNames: '[name].js',
		exports: "named"

	},
	inlineDynamicImports: true,
	plugins: [
		replace({
			'process.env.NODE_ENV': JSON.stringify(
				'production'
			)
		}),
		svelte({
			preprocess: [
				sveltePreprocess({ sourceMap: !production })
			],
			compilerOptions: {
				// enable run-time checks when not in production
				dev: !production,
				cssHash: function(option) {
					return `_${option.hash(option.css)}`;
				},
				css: css => {
					css.write('bundle.css')
				},

			}


		}),

		// we'll extract any component CSS out into
		// a separate file - better for performance
		scss({output: 'public/build/bundle.css'}),
		css({ output: 'bundle.css' }),


		// If you have external dependencies installed from
		// npm, you'll most likely need these plugins. In
		// some cases you'll need additional configuration -
		// consult the documentation for details:
		// https://github.com/rollup/plugins/tree/master/packages/commonjs
		resolve({
			browser: true,
			dedupe: ['svelte']
		}),
		commonjs(),
		typescript({
			include: ['./**/*.ts', './../network/**/*.ts', './../shared/**/*.ts'],
			sourceMap: !production,
			inlineSources: !production
		}),

		// In dev mode, call `npm run start` once
		// the bundle has been generated
		isDev && !isNollup && serve(),

		// Watch the `public` directory and refresh the
		// browser on changes when not in production
		isLiveReload && livereload('public'),

		// If we're building for production (npm run build
		// instead of npm run dev), minify
		isProduction && terser(),
		hmr({
			public: 'public',
			inMemory: true,
			host: '0.0.0.0',
			port: 35729,
			compatModuleHot: !isHot,
		})
	],
	watch: {
		clearScreen: false
	}
};

i ended up this way, and everything works except when i change css/scss Code... The Module reloads with the new CSS hash, but the new CSS Code isnt injected to the dom so I have broken design / missing styles.

@rixo
Copy link
Owner

rixo commented Apr 9, 2021

Yes, indeed, you'd need a HMR-enabled CSS plugin. Typically, it would wrap your CSS files in JS that replaces the stylesheet on hot updates.

Rollup ecosystem's not rich in HMR enabled stuff, unfortunately. You may want to experiment with https://github.com/PepsRyuu/rollup-plugin-hot-css or https://github.com/rixo/rollup-plugin-postcss-hot.

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