Skip to content

Commit

Permalink
Dev mode tweaks (#198)
Browse files Browse the repository at this point in the history
Tweaker litt for å gjøre det enklere å kjøre dekoratøren i dev-modus
integrert med andre apper lokalt

- Legger inn paths i tsconfig for delte mapper så IDE'er slutter å klage
- Legger inn dummy-verdi for styles-container i dev-modus, for at
nav-dekoratoren-moduler ikke skal forkaste responsen
- Legger inn faste classnames for CSS-modules i dev-modus, for at disse
ikke skal komme ut av sync med html'en ved HMR når CSS'en hot-reloades
  • Loading branch information
anders-nom authored Feb 28, 2024
1 parent 9422b03 commit 7e753b4
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion packages/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"types": ["vite/client", "vitest/globals"]
"types": ["vite/client", "vitest/globals"],
"paths": {
"decorator-shared/*": ["../shared/*"]
}
},
"exclude": ["node_modules", "dist"]
}
6 changes: 6 additions & 0 deletions packages/client/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import minifyLiterals from 'rollup-plugin-minify-html-literals-v3';
import path from 'path';
import { partytownRollup } from '@builder.io/partytown/utils';
import { typedCssModulesPlugin } from './typesafe-css-modules';
import { cssModulesScopedNameOption } from '../shared/css-modules-config';

export const mainBundleConfig = defineConfig({
plugins: [typedCssModulesPlugin()],
Expand Down Expand Up @@ -31,6 +32,11 @@ export const mainBundleConfig = defineConfig({
input: ['src/main.ts'],
},
},
css: {
modules: {
...cssModulesScopedNameOption,
},
},
});

export const lazyConfig = defineConfig({
Expand Down
5 changes: 4 additions & 1 deletion packages/server/css-modules-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { plugin } from 'bun';
import postcss from 'postcss';
import { cssModulesScopedNameOption } from '../shared/css-modules-config';

export async function getPostcssTokens(path: string) {
const val = await postcss([
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('postcss-modules')({
getJSON: () => {},
getJSON: () => {
},
...cssModulesScopedNameOption,
}),
]).process(await Bun.file(path).text(), { from: path });

Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ a.appendChild(r);

/* Merge the two manifests*/
export const getManifest = async () => {
const mainManifest = (await import(`decorator-client/dist/.vite/manifest.json`)).default;
const csrManifest = (await import(`decorator-client/dist/.vite/csr.manifest.json`)).default;
const thirdPartyManifest = (await import(`decorator-client/dist/.vite/analytics.manifest.json`)).default;
const mainManifest = (await import('decorator-client/dist/.vite/manifest.json')).default;
const csrManifest = (await import('decorator-client/dist/.vite/csr.manifest.json')).default;
const thirdPartyManifest = (await import('decorator-client/dist/.vite/analytics.manifest.json')).default;

return {
...mainManifest,
Expand Down Expand Up @@ -59,7 +59,7 @@ export const getEnvAssets = async () => {

const css: EnvAssets = {
production: manifest[entryPointPath].css.map(cdnUrl).map(cssLink).join(''),
development: '',
development: cssLink(''), // Dummy to ensure the styles-container is not empty
};

const scripts: EnvAssets = {
Expand Down
6 changes: 5 additions & 1 deletion packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": false
"isolatedModules": false,
"paths": {
"decorator-shared/*": ["../shared/*"],
"decorator-client/*": ["../client/*"]
}
},
"exclude": ["**/*.stories.ts"]
}
7 changes: 7 additions & 0 deletions packages/shared/css-modules-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Create stable classnames in dev mode, in order to not break in HMR when loaded via other apps
export const cssModulesScopedNameOption =
process.env.NODE_ENV === 'development'
? {
generateScopedName: '[name]__[local]',
}
: {};

0 comments on commit 7e753b4

Please sign in to comment.