@adonisjs/vite 0.0.1-9 → 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.
package/build/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import './src/backend/types/extended.js';
2
1
  export { configure } from './configure.js';
3
2
  export { stubsRoot } from './stubs/index.js';
4
3
  export { Vite } from './src/backend/vite.js';
package/build/index.js CHANGED
@@ -6,7 +6,6 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import './src/backend/types/extended.js';
10
9
  export { configure } from './configure.js';
11
10
  export { stubsRoot } from './stubs/index.js';
12
11
  export { Vite } from './src/backend/vite.js';
@@ -1,15 +1,24 @@
1
- import type { Edge } from 'edge.js';
2
1
  import type { ApplicationService } from '@adonisjs/core/types';
2
+ import type { Vite } from '../src/backend/vite.js';
3
+ /**
4
+ * Extend the container bindings
5
+ */
6
+ declare module '@adonisjs/core/types' {
7
+ interface ContainerBindings {
8
+ vite: Vite;
9
+ }
10
+ }
3
11
  export default class ViteServiceProvider {
4
12
  protected app: ApplicationService;
5
13
  constructor(app: ApplicationService);
6
14
  /**
7
- * Returns edge when it's installed
15
+ * Registers edge plugin when edge is installed
8
16
  */
9
- protected getEdge(): Promise<Edge | null>;
10
- register(): void;
17
+ protected registerEdgePlugin(): Promise<void>;
11
18
  /**
12
- * Extending edge
19
+ * Registers CSP keywords when @adonisjs/shield is installed
13
20
  */
21
+ protected registerShieldKeywords(): Promise<void>;
22
+ register(): void;
14
23
  boot(): Promise<void>;
15
24
  }
@@ -13,16 +13,61 @@ export default class ViteServiceProvider {
13
13
  this.app = app;
14
14
  }
15
15
  /**
16
- * Returns edge when it's installed
16
+ * Registers edge plugin when edge is installed
17
17
  */
18
- async getEdge() {
18
+ async registerEdgePlugin() {
19
+ let edge = null;
19
20
  try {
20
- const { default: edge } = await import('edge.js');
21
- debug('Detected edge.js package. Adding Vite primitives to it');
22
- return edge;
21
+ const edgeExports = await import('edge.js');
22
+ edge = edgeExports.default;
23
23
  }
24
- catch {
25
- return null;
24
+ catch { }
25
+ if (edge) {
26
+ const vite = await this.app.container.make('vite');
27
+ const { edgePluginVite } = await import('../src/backend/plugins/edge.js');
28
+ edge.use(edgePluginVite(vite));
29
+ }
30
+ }
31
+ /**
32
+ * Registers CSP keywords when @adonisjs/shield is installed
33
+ */
34
+ async registerShieldKeywords() {
35
+ let cspKeywords = null;
36
+ try {
37
+ const shieldExports = await import('@adonisjs/shield');
38
+ cspKeywords = shieldExports.cspKeywords;
39
+ }
40
+ catch { }
41
+ if (cspKeywords) {
42
+ debug('Detected @adonisjs/shield package. Adding Vite keywords for CSP policy');
43
+ const vite = await this.app.container.make('vite');
44
+ /**
45
+ * Registering the @viteUrl keyword for CSP directives.
46
+ * Returns http URL to the dev or the CDN server, otherwise
47
+ * an empty string
48
+ */
49
+ cspKeywords.register('@viteUrl', function () {
50
+ const assetsURL = vite.assetsUrl();
51
+ if (!assetsURL || !assetsURL.startsWith('http://') || assetsURL.startsWith('https://')) {
52
+ return '';
53
+ }
54
+ return assetsURL;
55
+ });
56
+ /**
57
+ * Registering the @viteDevUrl keyword for the CSP directives.
58
+ * Returns the dev server URL in development and empty string
59
+ * in prod
60
+ */
61
+ cspKeywords.register('@viteDevUrl', function () {
62
+ return vite.devUrl();
63
+ });
64
+ /**
65
+ * Registering the @viteHmrUrl keyword for the CSP directives.
66
+ * Returns the Websocket URL for the HMR server
67
+ */
68
+ cspKeywords.register('@viteHmrUrl', function () {
69
+ return vite.devUrl().replace('http://', 'ws://').replace('https://', 'wss://');
70
+ });
26
71
  }
27
72
  }
28
73
  register() {
@@ -36,15 +81,8 @@ export default class ViteServiceProvider {
36
81
  });
37
82
  });
38
83
  }
39
- /**
40
- * Extending edge
41
- */
42
84
  async boot() {
43
- const edge = await this.getEdge();
44
- if (edge) {
45
- const vite = await this.app.container.make('vite');
46
- const { edgePluginVite } = await import('../src/backend/edge_plugin_vite.js');
47
- edge.use(edgePluginVite(vite));
48
- }
85
+ await this.registerEdgePlugin();
86
+ await this.registerShieldKeywords();
49
87
  }
50
88
  }
@@ -1,3 +1,3 @@
1
- /// <reference types="@types/node" resolution-mode="require"/>
1
+ /// <reference types="node" resolution-mode="require"/>
2
2
  declare const _default: import("util").DebugLogger;
3
3
  export default _default;
@@ -1,4 +1,4 @@
1
- import { ViteOptions } from './types/main.js';
1
+ import { ViteOptions } from './types.js';
2
2
  /**
3
3
  * Define the backend config for resolving vite assets
4
4
  */
@@ -1,5 +1,5 @@
1
1
  import type { PluginFn } from 'edge.js/types';
2
- import type { Vite } from './vite.js';
2
+ import type { Vite } from '../vite.js';
3
3
  /**
4
4
  * The edge plugin for vite to share vite service with edge
5
5
  * and register custom tags
@@ -7,7 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { EdgeError } from 'edge-error';
10
- import debug from './debug.js';
10
+ import debug from '../debug.js';
11
11
  /**
12
12
  * The edge plugin for vite to share vite service with edge
13
13
  * and register custom tags
@@ -22,11 +22,29 @@ export const edgePluginVite = (vite) => {
22
22
  tagName: 'viteReactRefresh',
23
23
  seekable: true,
24
24
  block: false,
25
- compile(_parser, buffer, token) {
25
+ compile(parser, buffer, token) {
26
+ let attributes = '';
27
+ if (token.properties.jsArg.trim()) {
28
+ /**
29
+ * Converting a single argument to a SequenceExpression so that we
30
+ * work around the following edge cases.
31
+ *
32
+ * - If someone passes an object literal to the tag, ie { nonce: 'foo' }
33
+ * it will be parsed as a LabeledStatement and not an object.
34
+ * - If we wrap the object literal inside parenthesis, ie ({nonce: 'foo'})
35
+ * then we will end up messing other expressions like a variable reference
36
+ * , or a member expression and so on.
37
+ * - So the best bet is to convert user supplied argument to a sequence expression
38
+ * and hence ignore it during stringification.
39
+ */
40
+ const jsArg = `a,${token.properties.jsArg}`;
41
+ const parsed = parser.utils.transformAst(parser.utils.generateAST(jsArg, token.loc, token.filename), token.filename, parser);
42
+ attributes = parser.utils.stringify(parsed.expressions[1]);
43
+ }
26
44
  /**
27
45
  * Get HMR script
28
46
  */
29
- buffer.writeExpression(`const __vite_hmr_script = state.vite.getReactHmrScript()`, token.filename, token.loc.start.line);
47
+ buffer.writeExpression(`const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`, token.filename, token.loc.start.line);
30
48
  /**
31
49
  * Check if the script exists (only in hot mode)
32
50
  */
@@ -58,7 +76,10 @@ export const edgePluginVite = (vite) => {
58
76
  }
59
77
  const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
60
78
  const entrypoints = parser.utils.stringify(parsed);
61
- buffer.outputExpression(`state.vite.generateEntryPointsTags(${entrypoints}).join('\\n')`, token.filename, token.loc.start.line, false);
79
+ const methodCall = parsed.type === 'SequenceExpression'
80
+ ? `generateEntryPointsTags${entrypoints}`
81
+ : `generateEntryPointsTags(${entrypoints})`;
82
+ buffer.outputExpression(`state.vite.${methodCall}.join('\\n')`, token.filename, token.loc.start.line, false);
62
83
  },
63
84
  });
64
85
  };
@@ -1,5 +1,5 @@
1
1
  import type { Manifest } from 'vite';
2
- import type { AdonisViteElement, ViteOptions } from './types/main.js';
2
+ import type { AdonisViteElement, ViteOptions } from './types.js';
3
3
  /**
4
4
  * Vite class exposes the APIs to generate tags and URLs for
5
5
  * assets processed using vite.
@@ -10,7 +10,18 @@ export declare class Vite {
10
10
  /**
11
11
  * Generate tags for the entry points
12
12
  */
13
- generateEntryPointsTags(entryPoints: string[] | string): AdonisViteElement[];
13
+ generateEntryPointsTags(entryPoints: string[] | string, attributes?: Record<string, any>): AdonisViteElement[];
14
+ /**
15
+ * Returns the dev server URL when running in hot
16
+ * mode. Otherwise an empty string
17
+ */
18
+ devUrl(): string;
19
+ /**
20
+ * Returns the dev server URL when running in hot
21
+ * mode, otherwise returns the explicitly configured
22
+ * "assets" URL
23
+ */
24
+ assetsUrl(): string | undefined;
14
25
  /**
15
26
  * Returns path to a given asset file
16
27
  */
@@ -24,5 +35,5 @@ export declare class Vite {
24
35
  /**
25
36
  * Returns the script needed for the HMR working with React
26
37
  */
27
- getReactHmrScript(): AdonisViteElement | null;
38
+ getReactHmrScript(attributes?: Record<string, any>): AdonisViteElement | null;
28
39
  }
@@ -122,12 +122,13 @@ export class Vite {
122
122
  /**
123
123
  * Returns the script needed for the HMR working with Vite
124
124
  */
125
- #getViteHmrScript() {
125
+ #getViteHmrScript(attributes) {
126
126
  return this.#generateElement({
127
127
  tag: 'script',
128
128
  attributes: {
129
129
  type: 'module',
130
130
  src: this.#hotAsset('@vite/client'),
131
+ ...attributes,
131
132
  },
132
133
  children: [],
133
134
  });
@@ -137,7 +138,7 @@ export class Vite {
137
138
  * Also adds the @vite/client script
138
139
  */
139
140
  #generateEntryPointsTagsForHotMode(entryPoints, attributes) {
140
- const viteHmr = this.#getViteHmrScript();
141
+ const viteHmr = this.#getViteHmrScript(attributes);
141
142
  const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
142
143
  return viteHmr ? [viteHmr].concat(tags) : tags;
143
144
  }
@@ -195,12 +196,33 @@ export class Vite {
195
196
  /**
196
197
  * Generate tags for the entry points
197
198
  */
198
- generateEntryPointsTags(entryPoints) {
199
+ generateEntryPointsTags(entryPoints, attributes) {
199
200
  entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
200
201
  if (this.#isRunningHot()) {
201
- return this.#generateEntryPointsTagsForHotMode(entryPoints);
202
+ return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
202
203
  }
203
- return this.#generateEntryPointsTagsWithManifest(entryPoints);
204
+ return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
205
+ }
206
+ /**
207
+ * Returns the dev server URL when running in hot
208
+ * mode. Otherwise an empty string
209
+ */
210
+ devUrl() {
211
+ if (this.#isRunningHot()) {
212
+ return this.#readHotFile().url;
213
+ }
214
+ return '';
215
+ }
216
+ /**
217
+ * Returns the dev server URL when running in hot
218
+ * mode, otherwise returns the explicitly configured
219
+ * "assets" URL
220
+ */
221
+ assetsUrl() {
222
+ if (this.#isRunningHot()) {
223
+ return this.#readHotFile().url;
224
+ }
225
+ return this.#options.assetsUrl;
204
226
  }
205
227
  /**
206
228
  * Returns path to a given asset file
@@ -229,7 +251,7 @@ export class Vite {
229
251
  /**
230
252
  * Returns the script needed for the HMR working with React
231
253
  */
232
- getReactHmrScript() {
254
+ getReactHmrScript(attributes) {
233
255
  if (!this.#isRunningHot()) {
234
256
  return null;
235
257
  }
@@ -237,6 +259,7 @@ export class Vite {
237
259
  tag: 'script',
238
260
  attributes: {
239
261
  type: 'module',
262
+ ...attributes,
240
263
  },
241
264
  children: [
242
265
  '',
@@ -1,5 +1,10 @@
1
1
  import { PluginOption } from 'vite';
2
2
  import type { PluginOptions } from './types.js';
3
+ declare module 'vite' {
4
+ interface ManifestChunk {
5
+ integrity: string;
6
+ }
7
+ }
3
8
  /**
4
9
  * Vite plugin for adonisjs
5
10
  */
@@ -1,6 +1,6 @@
1
- ---
2
- to: {{ app.makePath('vite.config.js') }}
3
- ---
1
+ {{{
2
+ exports({ to: app.makePath('vite.config.js') })
3
+ }}}
4
4
  import { defineConfig } from 'vite'
5
5
  import adonisjs from '@adonisjs/vite/client'
6
6
 
@@ -16,7 +16,7 @@ export default defineConfig({
16
16
  /**
17
17
  * Paths to watch and reload the browser on file change
18
18
  */
19
- reloads: ['resources/views/**/*.edge'],
19
+ reload: ['resources/views/**/*.edge'],
20
20
  }),
21
21
  ],
22
22
  })
@@ -1,6 +1,6 @@
1
- ---
2
- to: {{ app.configPath('vite.ts') }}
3
- ---
1
+ {{{
2
+ exports({ to: app.configPath('vite.ts') })
3
+ }}}
4
4
  import { defineConfig } from '@adonisjs/vite'
5
5
 
6
6
  const viteBackendConfig = defineConfig({
@@ -1,4 +1,4 @@
1
- ---
2
- to: {{ app.makePath('resources/js/app.js') }}
3
- ---
1
+ {{{
2
+ exports({ to: app.makePath('resources/js/app.js') })
3
+ }}}
4
4
  console.log('Log from JS entrypoint')
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/vite",
3
3
  "description": "Vite plugin for Adonis.js",
4
- "version": "0.0.1-9",
4
+ "version": "1.0.0",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -22,7 +22,7 @@
22
22
  "./services/main": "./build/services/vite.js",
23
23
  "./vite_provider": "./build/providers/vite_provider.js",
24
24
  "./client": "./build/src/client/main.js",
25
- "./plugin_edge": "./build/backend/edge_plugin_vite.js",
25
+ "./plugins/edge": "./build/backend/plugins/edge.js",
26
26
  "./types": "./build/src/backend/types.js"
27
27
  },
28
28
  "scripts": {
@@ -42,31 +42,32 @@
42
42
  "prepublishOnly": "npm run build"
43
43
  },
44
44
  "devDependencies": {
45
- "@adonisjs/assembler": "^6.1.3-18",
46
- "@adonisjs/core": "^6.1.5-19",
45
+ "@adonisjs/assembler": "^6.1.3-25",
46
+ "@adonisjs/core": "^6.1.5-28",
47
47
  "@adonisjs/eslint-config": "^1.1.8",
48
48
  "@adonisjs/prettier-config": "^1.1.8",
49
+ "@adonisjs/shield": "^8.0.0-8",
49
50
  "@adonisjs/tsconfig": "^1.1.8",
50
- "@commitlint/cli": "^17.6.7",
51
- "@commitlint/config-conventional": "^17.6.7",
52
- "@japa/assert": "^2.0.0-1",
53
- "@japa/expect-type": "^2.0.0-0",
54
- "@japa/file-system": "^2.0.0-1",
55
- "@japa/runner": "^3.0.0-6",
56
- "@swc/core": "^1.3.78",
57
- "@types/node": "^20.5.1",
51
+ "@commitlint/cli": "^17.8.0",
52
+ "@commitlint/config-conventional": "^17.8.0",
53
+ "@japa/assert": "^2.0.0",
54
+ "@japa/expect-type": "^2.0.0",
55
+ "@japa/file-system": "^2.0.0",
56
+ "@japa/runner": "^3.0.2",
57
+ "@swc/core": "1.3.82",
58
+ "@types/node": "^20.8.6",
58
59
  "c8": "^8.0.0",
59
60
  "copyfiles": "^2.4.1",
60
- "del-cli": "^5.0.0",
61
+ "del-cli": "^5.1.0",
61
62
  "edge.js": "^6.0.0-8",
62
- "eslint": "^8.45.0",
63
+ "eslint": "^8.51.0",
63
64
  "husky": "^8.0.3",
64
65
  "np": "^8.0.4",
65
- "prettier": "^3.0.0",
66
- "rollup": "^3.28.1",
66
+ "prettier": "^3.0.3",
67
+ "rollup": "^4.1.4",
67
68
  "ts-node": "^10.9.1",
68
- "typescript": "^5.1.6",
69
- "vite": "^4.4.9"
69
+ "typescript": "^5.2.2",
70
+ "vite": "^4.4.11"
70
71
  },
71
72
  "dependencies": {
72
73
  "defu": "^6.1.2",
@@ -74,12 +75,20 @@
74
75
  "vite-plugin-restart": "^0.3.1"
75
76
  },
76
77
  "peerDependencies": {
77
- "@adonisjs/core": "^6.1.5-18",
78
- "edge.js": "^6.0.0-8"
78
+ "@adonisjs/core": "^6.1.5-28",
79
+ "@adonisjs/shield": "^8.0.0-8",
80
+ "edge.js": "^6.0.0-8",
81
+ "vite": "^4.4.11"
79
82
  },
80
83
  "peerDependenciesMeta": {
84
+ "vite": {
85
+ "optional": true
86
+ },
81
87
  "edge.js": {
82
88
  "optional": true
89
+ },
90
+ "@adonisjs/shield": {
91
+ "optional": true
83
92
  }
84
93
  },
85
94
  "author": "Julien Ripouteau <julien@ripouteau.com>",
@@ -111,11 +120,11 @@
111
120
  },
112
121
  "publishConfig": {
113
122
  "access": "public",
114
- "tag": "next"
123
+ "tag": "latest"
115
124
  },
116
125
  "np": {
117
126
  "message": "chore(release): %s",
118
- "tag": "next",
127
+ "tag": "latest",
119
128
  "branch": "main",
120
129
  "anyBranch": false
121
130
  },
@@ -1,14 +0,0 @@
1
- import { Vite } from '../vite.js';
2
- /**
3
- * Extend the container bindings
4
- */
5
- declare module '@adonisjs/core/types' {
6
- interface ContainerBindings {
7
- vite: Vite;
8
- }
9
- }
10
- declare module 'vite' {
11
- interface ManifestChunk {
12
- integrity: string;
13
- }
14
- }
@@ -1,9 +0,0 @@
1
- /*
2
- * @adonisjs/vite
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- export {};