@navita/vite-plugin 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +3 -3
  2. package/remix.cjs +27 -29
  3. package/remix.mjs +28 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/vite-plugin",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Navita Vite Plugin",
5
5
  "keywords": [
6
6
  "vite",
@@ -22,8 +22,8 @@
22
22
  }
23
23
  },
24
24
  "dependencies": {
25
- "@navita/core": "1.0.0",
26
- "@navita/css": "0.2.0"
25
+ "@navita/css": "0.2.0",
26
+ "@navita/core": "1.0.0"
27
27
  },
28
28
  "license": "MIT",
29
29
  "author": "Eagerpatch",
package/remix.cjs CHANGED
@@ -22,51 +22,39 @@ function _interopNamespaceDefault(e) {
22
22
 
23
23
  var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
24
24
 
25
+ const remixServerBuildId = '\0virtual:remix/server-build';
25
26
  let cssFileName;
26
27
  function navitaRemix(options) {
27
- let server;
28
+ let isProduction = false;
29
+ let hasEmittedCss = false;
28
30
  const { renderChunk , ...navitaVite } = index.navita(options);
29
31
  return [
30
32
  navitaVite,
31
33
  {
32
34
  name: 'navita-remix',
33
- configureServer (_server) {
34
- server = _server;
35
- server.middlewares.use(async function middleware(_req, _res, next) {
36
- try {
37
- const build = await server.ssrLoadModule('virtual:remix/server-build');
38
- const { module } = build.routes.root;
39
- // We modify the root module, to automatically include the CSS
40
- // when running the dev server.
41
- build.routes.root.module = {
42
- ...module,
43
- links: ()=>[
44
- ...module.links(),
45
- {
46
- rel: 'stylesheet',
47
- href: `/${index.VIRTUAL_MODULE_ID}`
48
- }
49
- ]
50
- };
51
- } catch (e) {
52
- console.error(e);
53
- }
54
- next();
55
- });
35
+ configResolved (config) {
36
+ isProduction = config.mode === 'production';
56
37
  },
57
- renderChunk (_, chunk) {
58
- if (chunk.name === "root") {
38
+ transform (code, id) {
39
+ if (isProduction || id !== remixServerBuildId) {
40
+ return;
41
+ }
42
+ return `${code}\n${remixServerBuildExtension}`;
43
+ },
44
+ renderChunk (_, chunk, options) {
45
+ const isServerChunk = options.dir.endsWith('/server');
46
+ const isClientChunk = options.dir.endsWith('/client');
47
+ if (isClientChunk && chunk.name === "root") {
59
48
  // Generate a random name for the CSS file.
60
49
  // Vite uses a file hash as the name, but since the client build will finish before
61
50
  // the server build, we need to generate a random name for the CSS file.
62
51
  // Ideally we could use a hash, but since the server might contain more styles than the client, we need to do it like this.
63
52
  const random = crypto__namespace.randomBytes(30).toString("base64").replace(/[^a-zA-Z0-9]/g, "").slice(0, 8);
64
53
  cssFileName = `assets/navita-${random}.css`;
65
- // Attach the file to the root chunk so that it's included in the client build.
66
- chunk.viteMetadata?.importedCss.add(cssFileName);
54
+ chunk.viteMetadata.importedCss.add(cssFileName);
67
55
  return;
68
56
  }
69
- if (chunk.name === 'server-build') {
57
+ if (isServerChunk && !hasEmittedCss) {
70
58
  // In the server-build, we'll generate the CSS and emit it as an asset.
71
59
  // Remix will then move it to the client assets.
72
60
  this.emitFile({
@@ -75,10 +63,20 @@ function navitaRemix(options) {
75
63
  type: 'asset',
76
64
  source: index.getRenderer()?.engine.renderCssToString()
77
65
  });
66
+ hasEmittedCss = true;
78
67
  }
79
68
  }
80
69
  }
81
70
  ];
82
71
  }
72
+ const remixServerBuildExtension = `
73
+ routes.root.module = {
74
+ ...route0,
75
+ links: () => [
76
+ ...(route0.links ? route0.links() : []),
77
+ { rel: 'stylesheet', href: '/${index.VIRTUAL_MODULE_ID}' },
78
+ ],
79
+ };
80
+ `;
83
81
 
84
82
  exports.navitaRemix = navitaRemix;
package/remix.mjs CHANGED
@@ -1,51 +1,39 @@
1
1
  import * as crypto from 'node:crypto';
2
- import { navita, VIRTUAL_MODULE_ID, getRenderer } from './index.mjs';
2
+ import { navita, getRenderer, VIRTUAL_MODULE_ID } from './index.mjs';
3
3
 
4
+ const remixServerBuildId = '\0virtual:remix/server-build';
4
5
  let cssFileName;
5
6
  function navitaRemix(options) {
6
- let server;
7
+ let isProduction = false;
8
+ let hasEmittedCss = false;
7
9
  const { renderChunk , ...navitaVite } = navita(options);
8
10
  return [
9
11
  navitaVite,
10
12
  {
11
13
  name: 'navita-remix',
12
- configureServer (_server) {
13
- server = _server;
14
- server.middlewares.use(async function middleware(_req, _res, next) {
15
- try {
16
- const build = await server.ssrLoadModule('virtual:remix/server-build');
17
- const { module } = build.routes.root;
18
- // We modify the root module, to automatically include the CSS
19
- // when running the dev server.
20
- build.routes.root.module = {
21
- ...module,
22
- links: ()=>[
23
- ...module.links(),
24
- {
25
- rel: 'stylesheet',
26
- href: `/${VIRTUAL_MODULE_ID}`
27
- }
28
- ]
29
- };
30
- } catch (e) {
31
- console.error(e);
32
- }
33
- next();
34
- });
14
+ configResolved (config) {
15
+ isProduction = config.mode === 'production';
35
16
  },
36
- renderChunk (_, chunk) {
37
- if (chunk.name === "root") {
17
+ transform (code, id) {
18
+ if (isProduction || id !== remixServerBuildId) {
19
+ return;
20
+ }
21
+ return `${code}\n${remixServerBuildExtension}`;
22
+ },
23
+ renderChunk (_, chunk, options) {
24
+ const isServerChunk = options.dir.endsWith('/server');
25
+ const isClientChunk = options.dir.endsWith('/client');
26
+ if (isClientChunk && chunk.name === "root") {
38
27
  // Generate a random name for the CSS file.
39
28
  // Vite uses a file hash as the name, but since the client build will finish before
40
29
  // the server build, we need to generate a random name for the CSS file.
41
30
  // Ideally we could use a hash, but since the server might contain more styles than the client, we need to do it like this.
42
31
  const random = crypto.randomBytes(30).toString("base64").replace(/[^a-zA-Z0-9]/g, "").slice(0, 8);
43
32
  cssFileName = `assets/navita-${random}.css`;
44
- // Attach the file to the root chunk so that it's included in the client build.
45
- chunk.viteMetadata?.importedCss.add(cssFileName);
33
+ chunk.viteMetadata.importedCss.add(cssFileName);
46
34
  return;
47
35
  }
48
- if (chunk.name === 'server-build') {
36
+ if (isServerChunk && !hasEmittedCss) {
49
37
  // In the server-build, we'll generate the CSS and emit it as an asset.
50
38
  // Remix will then move it to the client assets.
51
39
  this.emitFile({
@@ -54,10 +42,20 @@ function navitaRemix(options) {
54
42
  type: 'asset',
55
43
  source: getRenderer()?.engine.renderCssToString()
56
44
  });
45
+ hasEmittedCss = true;
57
46
  }
58
47
  }
59
48
  }
60
49
  ];
61
50
  }
51
+ const remixServerBuildExtension = `
52
+ routes.root.module = {
53
+ ...route0,
54
+ links: () => [
55
+ ...(route0.links ? route0.links() : []),
56
+ { rel: 'stylesheet', href: '/${VIRTUAL_MODULE_ID}' },
57
+ ],
58
+ };
59
+ `;
62
60
 
63
61
  export { navitaRemix };