@navita/vite-plugin 0.1.5 → 1.0.0

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/index.cjs +40 -23
  2. package/index.mjs +40 -23
  3. package/package.json +3 -3
package/index.cjs CHANGED
@@ -23,10 +23,13 @@ function _interopNamespaceDefault(e) {
23
23
 
24
24
  var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
25
25
 
26
- const VIRTUAL_MODULE_NAME = 'virtual:navita';
27
- const VIRTUAL_CSS_NAME = 'virtual:navita.css';
26
+ let renderer;
27
+ /*
28
+ Some information for anyone wondering why we have duplicate css for the initial load
29
+ during development in remix.
30
+ https://github.com/remix-run/remix/discussions/8070#discussioncomment-7625870
31
+ */ const VIRTUAL_CSS_NAME = '\0virtual:navita.css';
28
32
  function navita(options) {
29
- let renderer;
30
33
  const importMap = [
31
34
  ...css.importMap,
32
35
  ...options?.importMap || []
@@ -34,16 +37,12 @@ function navita(options) {
34
37
  let server;
35
38
  let lastCssContent;
36
39
  let context;
40
+ let isSSR = false;
37
41
  return {
38
42
  name: "navita",
39
43
  enforce: "pre",
40
- config (_, env) {
44
+ config () {
41
45
  return {
42
- optimizeDeps: {
43
- include: env.command === 'serve' ? [
44
- '@navita/css'
45
- ] : []
46
- },
47
46
  ssr: {
48
47
  external: [
49
48
  '@navita/css',
@@ -54,6 +53,7 @@ function navita(options) {
54
53
  },
55
54
  configResolved (config) {
56
55
  context = config.root;
56
+ isSSR = !!config.build.ssr;
57
57
  },
58
58
  configureServer (_server) {
59
59
  lastCssContent = undefined;
@@ -78,17 +78,17 @@ function navita(options) {
78
78
  }
79
79
  });
80
80
  },
81
- async resolveId (source) {
82
- if (source === VIRTUAL_MODULE_NAME) {
81
+ async resolveId (id) {
82
+ if (id === VIRTUAL_CSS_NAME) {
83
83
  return VIRTUAL_CSS_NAME;
84
84
  }
85
- return null;
85
+ return;
86
86
  },
87
87
  async load (id) {
88
88
  if (id === VIRTUAL_CSS_NAME) {
89
89
  return lastCssContent;
90
90
  }
91
- return null;
91
+ return;
92
92
  },
93
93
  async transform (code, id) {
94
94
  // Bail as early as we can
@@ -96,28 +96,31 @@ function navita(options) {
96
96
  renderer.clearCache(id);
97
97
  return null;
98
98
  }
99
- const { result , sourceMap } = await renderer.transformAndProcess({
99
+ const { result , sourceMap , dependencies } = await renderer.transformAndProcess({
100
100
  content: code,
101
101
  filePath: id
102
102
  });
103
103
  const newCssContent = renderer.engine.renderCssToString();
104
104
  if (lastCssContent !== newCssContent) {
105
- if (server) {
106
- const { moduleGraph } = server;
107
- const virtualModule = moduleGraph.getModuleById(VIRTUAL_CSS_NAME);
108
- if (virtualModule) {
109
- moduleGraph.invalidateModule(virtualModule);
110
- virtualModule.lastHMRTimestamp = Date.now();
105
+ invalidateModule(VIRTUAL_CSS_NAME);
106
+ this.addWatchFile(VIRTUAL_CSS_NAME);
107
+ lastCssContent = newCssContent;
108
+ for (const file of dependencies){
109
+ if (!file.includes('node_modules')) {
110
+ this.addWatchFile(file);
111
111
  }
112
+ invalidateModule(file);
112
113
  }
113
- lastCssContent = newCssContent;
114
114
  }
115
115
  return {
116
- code: `${result} import "${VIRTUAL_MODULE_NAME}";`,
116
+ code: `${result} import "${VIRTUAL_CSS_NAME}";`,
117
117
  map: sourceMap
118
118
  };
119
119
  },
120
- renderChunk (_, chunk) {
120
+ renderChunk (_, chunk, third) {
121
+ if (isSSR) {
122
+ return;
123
+ }
121
124
  for (const id of Object.keys(chunk.modules)){
122
125
  if (id.startsWith(VIRTUAL_CSS_NAME)) {
123
126
  delete chunk.modules[id];
@@ -130,6 +133,20 @@ function navita(options) {
130
133
  })));
131
134
  }
132
135
  };
136
+ function invalidateModule(absoluteId) {
137
+ if (!server) {
138
+ return;
139
+ }
140
+ const { moduleGraph } = server;
141
+ const modules = moduleGraph.getModulesByFile(absoluteId);
142
+ if (modules) {
143
+ for (const module of modules){
144
+ moduleGraph.invalidateModule(module);
145
+ // Vite uses this timestamp to add `?t=` query string automatically for HMR.
146
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
147
+ }
148
+ }
149
+ }
133
150
  }
134
151
 
135
152
  exports.navita = navita;
package/index.mjs CHANGED
@@ -2,10 +2,13 @@ import * as fs from 'node:fs';
2
2
  import { createRenderer } from '@navita/core/createRenderer';
3
3
  import { importMap } from '@navita/css';
4
4
 
5
- const VIRTUAL_MODULE_NAME = 'virtual:navita';
6
- const VIRTUAL_CSS_NAME = 'virtual:navita.css';
5
+ let renderer;
6
+ /*
7
+ Some information for anyone wondering why we have duplicate css for the initial load
8
+ during development in remix.
9
+ https://github.com/remix-run/remix/discussions/8070#discussioncomment-7625870
10
+ */ const VIRTUAL_CSS_NAME = '\0virtual:navita.css';
7
11
  function navita(options) {
8
- let renderer;
9
12
  const importMap$1 = [
10
13
  ...importMap,
11
14
  ...options?.importMap || []
@@ -13,16 +16,12 @@ function navita(options) {
13
16
  let server;
14
17
  let lastCssContent;
15
18
  let context;
19
+ let isSSR = false;
16
20
  return {
17
21
  name: "navita",
18
22
  enforce: "pre",
19
- config (_, env) {
23
+ config () {
20
24
  return {
21
- optimizeDeps: {
22
- include: env.command === 'serve' ? [
23
- '@navita/css'
24
- ] : []
25
- },
26
25
  ssr: {
27
26
  external: [
28
27
  '@navita/css',
@@ -33,6 +32,7 @@ function navita(options) {
33
32
  },
34
33
  configResolved (config) {
35
34
  context = config.root;
35
+ isSSR = !!config.build.ssr;
36
36
  },
37
37
  configureServer (_server) {
38
38
  lastCssContent = undefined;
@@ -57,17 +57,17 @@ function navita(options) {
57
57
  }
58
58
  });
59
59
  },
60
- async resolveId (source) {
61
- if (source === VIRTUAL_MODULE_NAME) {
60
+ async resolveId (id) {
61
+ if (id === VIRTUAL_CSS_NAME) {
62
62
  return VIRTUAL_CSS_NAME;
63
63
  }
64
- return null;
64
+ return;
65
65
  },
66
66
  async load (id) {
67
67
  if (id === VIRTUAL_CSS_NAME) {
68
68
  return lastCssContent;
69
69
  }
70
- return null;
70
+ return;
71
71
  },
72
72
  async transform (code, id) {
73
73
  // Bail as early as we can
@@ -75,28 +75,31 @@ function navita(options) {
75
75
  renderer.clearCache(id);
76
76
  return null;
77
77
  }
78
- const { result , sourceMap } = await renderer.transformAndProcess({
78
+ const { result , sourceMap , dependencies } = await renderer.transformAndProcess({
79
79
  content: code,
80
80
  filePath: id
81
81
  });
82
82
  const newCssContent = renderer.engine.renderCssToString();
83
83
  if (lastCssContent !== newCssContent) {
84
- if (server) {
85
- const { moduleGraph } = server;
86
- const virtualModule = moduleGraph.getModuleById(VIRTUAL_CSS_NAME);
87
- if (virtualModule) {
88
- moduleGraph.invalidateModule(virtualModule);
89
- virtualModule.lastHMRTimestamp = Date.now();
84
+ invalidateModule(VIRTUAL_CSS_NAME);
85
+ this.addWatchFile(VIRTUAL_CSS_NAME);
86
+ lastCssContent = newCssContent;
87
+ for (const file of dependencies){
88
+ if (!file.includes('node_modules')) {
89
+ this.addWatchFile(file);
90
90
  }
91
+ invalidateModule(file);
91
92
  }
92
- lastCssContent = newCssContent;
93
93
  }
94
94
  return {
95
- code: `${result} import "${VIRTUAL_MODULE_NAME}";`,
95
+ code: `${result} import "${VIRTUAL_CSS_NAME}";`,
96
96
  map: sourceMap
97
97
  };
98
98
  },
99
- renderChunk (_, chunk) {
99
+ renderChunk (_, chunk, third) {
100
+ if (isSSR) {
101
+ return;
102
+ }
100
103
  for (const id of Object.keys(chunk.modules)){
101
104
  if (id.startsWith(VIRTUAL_CSS_NAME)) {
102
105
  delete chunk.modules[id];
@@ -109,6 +112,20 @@ function navita(options) {
109
112
  })));
110
113
  }
111
114
  };
115
+ function invalidateModule(absoluteId) {
116
+ if (!server) {
117
+ return;
118
+ }
119
+ const { moduleGraph } = server;
120
+ const modules = moduleGraph.getModulesByFile(absoluteId);
121
+ if (modules) {
122
+ for (const module of modules){
123
+ moduleGraph.invalidateModule(module);
124
+ // Vite uses this timestamp to add `?t=` query string automatically for HMR.
125
+ module.lastHMRTimestamp = module.lastInvalidationTimestamp || Date.now();
126
+ }
127
+ }
128
+ }
112
129
  }
113
130
 
114
131
  export { navita };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/vite-plugin",
3
- "version": "0.1.5",
3
+ "version": "1.0.0",
4
4
  "description": "Navita Vite Plugin",
5
5
  "keywords": [
6
6
  "vite",
@@ -17,8 +17,8 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "@navita/core": "0.2.1",
21
- "@navita/css": "0.1.0"
20
+ "@navita/css": "0.2.0",
21
+ "@navita/core": "1.0.0"
22
22
  },
23
23
  "license": "MIT",
24
24
  "author": "Eagerpatch",