@adonisjs/vite 2.0.2 → 3.0.0-1

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 (47) hide show
  1. package/LICENSE.md +1 -1
  2. package/build/index.d.ts +2 -2
  3. package/build/index.js +47 -12
  4. package/build/providers/vite_provider.d.ts +4 -10
  5. package/build/providers/vite_provider.js +49 -78
  6. package/build/providers/vite_provider.js.map +1 -0
  7. package/build/services/vite.d.ts +1 -1
  8. package/build/services/vite.js +8 -16
  9. package/build/services/vite.js.map +1 -0
  10. package/build/src/client/config.d.ts +13 -2
  11. package/build/src/client/main.d.ts +1 -1
  12. package/build/src/client/main.js +67 -22
  13. package/build/src/client/main.js.map +1 -0
  14. package/build/src/client/types.d.ts +12 -21
  15. package/build/src/define_config.d.ts +5 -0
  16. package/build/src/hooks/build_hook.d.ts +8 -0
  17. package/build/src/hooks/build_hook.js +16 -0
  18. package/build/src/hooks/build_hook.js.map +1 -0
  19. package/build/src/middlewares/vite_middleware.d.ts +17 -0
  20. package/build/src/{backend/types.d.ts → types.d.ts} +6 -22
  21. package/build/src/types.js +1 -0
  22. package/build/src/types.js.map +1 -0
  23. package/build/src/{backend/utils.d.ts → utils.d.ts} +4 -0
  24. package/build/src/{backend/vite.d.ts → vite.d.ts} +27 -9
  25. package/build/stubs/vite.config.stub +1 -1
  26. package/package.json +52 -55
  27. package/build/configure.js +0 -42
  28. package/build/src/backend/debug.d.ts +0 -3
  29. package/build/src/backend/debug.js +0 -10
  30. package/build/src/backend/define_config.d.ts +0 -5
  31. package/build/src/backend/define_config.js +0 -23
  32. package/build/src/backend/plugins/edge.js +0 -86
  33. package/build/src/backend/types.js +0 -9
  34. package/build/src/backend/utils.js +0 -36
  35. package/build/src/backend/vite.js +0 -259
  36. package/build/src/client/config.js +0 -73
  37. package/build/src/client/config_resolver.d.ts +0 -20
  38. package/build/src/client/config_resolver.js +0 -46
  39. package/build/src/client/helpers/inertia.d.ts +0 -4
  40. package/build/src/client/helpers/inertia.js +0 -22
  41. package/build/src/client/hot_file.d.ts +0 -14
  42. package/build/src/client/hot_file.js +0 -49
  43. package/build/src/client/types.js +0 -9
  44. package/build/src/client/utils.d.ts +0 -11
  45. package/build/src/client/utils.js +0 -44
  46. package/build/stubs/main.js +0 -10
  47. /package/build/src/{backend/plugins → plugins}/edge.d.ts +0 -0
@@ -1,5 +1,5 @@
1
1
  {{{
2
- exports({ to: app.makePath('vite.config.js') })
2
+ exports({ to: app.makePath('vite.config.ts') })
3
3
  }}}
4
4
  import { defineConfig } from 'vite'
5
5
  import adonisjs from '@adonisjs/vite/client'
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
2
  "name": "@adonisjs/vite",
3
- "description": "Vite plugin for Adonis.js",
4
- "version": "2.0.2",
3
+ "description": "Vite plugin for AdonisJS",
4
+ "version": "3.0.0-1",
5
5
  "engines": {
6
- "node": ">=18.16.0"
6
+ "node": ">=20.6.0"
7
7
  },
8
8
  "main": "build/index.js",
9
9
  "type": "module",
10
10
  "files": [
11
+ "build/providers",
12
+ "build/services",
13
+ "build/src",
14
+ "build/stubs",
11
15
  "build/configure.js",
12
16
  "build/configure.d.ts",
13
17
  "build/index.js",
14
- "build/index.d.ts",
15
- "build/stubs",
16
- "build/src",
17
- "build/services",
18
- "build/providers"
18
+ "build/index.d.ts"
19
19
  ],
20
20
  "exports": {
21
21
  ".": "./build/index.js",
22
- "./services/main": "./build/services/vite.js",
23
22
  "./vite_provider": "./build/providers/vite_provider.js",
23
+ "./services/main": "./build/services/vite.js",
24
+ "./types": "./build/src/types.js",
24
25
  "./client": "./build/src/client/main.js",
25
- "./plugins/edge": "./build/backend/plugins/edge.js",
26
- "./types": "./build/src/backend/types.js"
26
+ "./build_hook": "./build/src/hooks/build_hook.js"
27
27
  },
28
28
  "scripts": {
29
29
  "clean": "del-cli build",
@@ -34,58 +34,51 @@
34
34
  "quick:test": "node --enable-source-maps --loader=ts-node/esm bin/test.ts",
35
35
  "pretest": "npm run lint",
36
36
  "test": "c8 npm run quick:test",
37
- "precompile": "npm run lint && npm run clean ",
38
- "compile": "tsc",
39
- "postcompile": "npm run copy:templates",
40
- "build": "npm run compile",
37
+ "prebuild": "npm run lint && npm run clean",
38
+ "build": "tsup-node && tsc --emitDeclarationOnly --declaration",
39
+ "postbuild": "npm run copy:templates",
41
40
  "release": "np",
42
41
  "version": "npm run build",
43
42
  "prepublishOnly": "npm run build"
44
43
  },
45
44
  "devDependencies": {
46
- "@adonisjs/assembler": "^7.0.0",
47
- "@adonisjs/core": "^6.2.0",
45
+ "@adonisjs/application": "8.1.0",
46
+ "@adonisjs/assembler": "^7.2.2",
47
+ "@adonisjs/core": "6.3.1",
48
48
  "@adonisjs/eslint-config": "^1.2.1",
49
49
  "@adonisjs/prettier-config": "^1.2.1",
50
- "@adonisjs/session": "^7.0.0",
51
- "@adonisjs/shield": "^8.0.0",
50
+ "@adonisjs/shield": "^8.1.1",
52
51
  "@adonisjs/tsconfig": "^1.2.1",
53
- "@commitlint/cli": "^18.4.4",
54
- "@commitlint/config-conventional": "^18.4.4",
55
- "@japa/assert": "^2.1.0",
56
- "@japa/expect-type": "^2.0.1",
57
- "@japa/file-system": "^2.1.1",
58
- "@japa/runner": "^3.1.1",
59
- "@swc/core": "^1.3.102",
60
- "@types/node": "^20.10.7",
61
- "c8": "^9.0.0",
52
+ "@japa/assert": "2.1.0",
53
+ "@japa/file-system": "^2.2.0",
54
+ "@japa/runner": "3.1.1",
55
+ "@swc/core": "^1.4.2",
56
+ "@types/node": "^20.11.20",
57
+ "c8": "^9.1.0",
62
58
  "copyfiles": "^2.4.1",
63
59
  "del-cli": "^5.1.0",
64
60
  "edge.js": "^6.0.1",
65
- "eslint": "^8.56.0",
66
- "husky": "^8.0.3",
67
- "np": "^9.2.0",
68
- "prettier": "^3.1.1",
69
- "rollup": "^4.9.4",
61
+ "eslint": "^8.57.0",
62
+ "np": "^10.0.0",
63
+ "prettier": "^3.2.5",
70
64
  "ts-node": "^10.9.2",
71
- "typescript": "^5.3.3",
72
- "vite": "^5.0.11"
65
+ "tsup": "^8.0.2",
66
+ "typescript": "~5.3.3",
67
+ "vite": "^5.1.4"
73
68
  },
74
69
  "dependencies": {
75
- "defu": "^6.1.4",
70
+ "@poppinss/utils": "^6.7.2",
71
+ "@vavite/multibuild": "^4.1.1",
76
72
  "edge-error": "^4.0.1",
77
73
  "vite-plugin-restart": "^0.4.0"
78
74
  },
79
75
  "peerDependencies": {
80
- "@adonisjs/core": "^6.2.0",
76
+ "@adonisjs/core": "^6.3.0",
81
77
  "@adonisjs/shield": "^8.0.0",
82
78
  "edge.js": "^6.0.1",
83
- "vite": "^5.0.11"
79
+ "vite": "^5.1.4"
84
80
  },
85
81
  "peerDependenciesMeta": {
86
- "vite": {
87
- "optional": true
88
- },
89
82
  "edge.js": {
90
83
  "optional": true
91
84
  },
@@ -107,27 +100,18 @@
107
100
  "vite",
108
101
  "adonisjs"
109
102
  ],
110
- "contributors": [
111
- "virk",
112
- "adonisjs"
113
- ],
114
103
  "eslintConfig": {
115
104
  "extends": "@adonisjs/eslint-config/package"
116
105
  },
117
106
  "prettier": "@adonisjs/prettier-config",
118
- "commitlint": {
119
- "extends": [
120
- "@commitlint/config-conventional"
121
- ]
122
- },
123
107
  "publishConfig": {
124
108
  "access": "public",
125
- "tag": "latest"
109
+ "tag": "next"
126
110
  },
127
111
  "np": {
128
112
  "message": "chore(release): %s",
129
- "tag": "latest",
130
- "branch": "main",
113
+ "tag": "next",
114
+ "branch": "next",
131
115
  "anyBranch": false
132
116
  },
133
117
  "c8": {
@@ -140,7 +124,20 @@
140
124
  "tests_helpers/**"
141
125
  ]
142
126
  },
143
- "directories": {
144
- "test": "tests"
127
+ "tsup": {
128
+ "entry": [
129
+ "./index.ts",
130
+ "./providers/vite_provider.ts",
131
+ "./services/vite.ts",
132
+ "./src/types.ts",
133
+ "./src/client/main.ts",
134
+ "./src/hooks/build_hook.ts"
135
+ ],
136
+ "outDir": "./build",
137
+ "clean": true,
138
+ "format": "esm",
139
+ "dts": false,
140
+ "sourcemap": true,
141
+ "target": "esnext"
145
142
  }
146
143
  }
@@ -1,42 +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
- import { stubsRoot } from './stubs/main.js';
10
- /**
11
- * Configures the package
12
- */
13
- export async function configure(command) {
14
- const codemods = await command.createCodemods();
15
- let shouldInstallPackages = command.parsedFlags.install;
16
- /**
17
- * Publish stubs
18
- */
19
- await codemods.makeUsingStub(stubsRoot, 'config/vite.stub', {});
20
- await codemods.makeUsingStub(stubsRoot, 'vite.config.stub', {});
21
- await codemods.makeUsingStub(stubsRoot, 'js_entrypoint.stub', {});
22
- await codemods.updateRcFile((rcFile) => {
23
- rcFile.addProvider('@adonisjs/vite/vite_provider');
24
- rcFile.addMetaFile('public/**', false);
25
- });
26
- /**
27
- * Prompt when `install` or `--no-install` flags are
28
- * not used
29
- */
30
- if (shouldInstallPackages === undefined) {
31
- shouldInstallPackages = await command.prompt.confirm('Do you want to install "vite"?');
32
- }
33
- /**
34
- * Install dependency or list the command to install it
35
- */
36
- if (shouldInstallPackages) {
37
- await codemods.installPackages([{ name: 'vite', isDevDependency: true }]);
38
- }
39
- else {
40
- await codemods.listPackagesToInstall([{ name: 'vite', isDevDependency: true }]);
41
- }
42
- }
@@ -1,3 +0,0 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- declare const _default: import("util").DebugLogger;
3
- export default _default;
@@ -1,10 +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
- import { debuglog } from 'node:util';
10
- export default debuglog('adonisjs:vite');
@@ -1,5 +0,0 @@
1
- import { ViteOptions } from './types.js';
2
- /**
3
- * Define the backend config for resolving vite assets
4
- */
5
- export declare function defineConfig(config: Partial<ViteOptions>): ViteOptions;
@@ -1,23 +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
- import { join } from 'node:path';
10
- /**
11
- * Define the backend config for resolving vite assets
12
- */
13
- export function defineConfig(config) {
14
- return {
15
- buildDirectory: 'public/assets',
16
- hotFile: 'public/assets/hot.json',
17
- assetsUrl: '/assets',
18
- manifestFile: config.buildDirectory
19
- ? join(config.buildDirectory, '.vite/manifest.json')
20
- : 'public/assets/.vite/manifest.json',
21
- ...config,
22
- };
23
- }
@@ -1,86 +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
- import { EdgeError } from 'edge-error';
10
- import debug from '../debug.js';
11
- /**
12
- * The edge plugin for vite to share vite service with edge
13
- * and register custom tags
14
- */
15
- export const edgePluginVite = (vite) => {
16
- return (edge) => {
17
- debug('sharing vite and asset globals with edge');
18
- edge.global('vite', vite);
19
- edge.global('asset', vite.assetPath.bind(vite));
20
- debug('registering vite tags with edge');
21
- edge.registerTag({
22
- tagName: 'viteReactRefresh',
23
- seekable: true,
24
- block: false,
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
- }
44
- /**
45
- * Get HMR script
46
- */
47
- buffer.writeExpression(`const __vite_hmr_script = state.vite.getReactHmrScript(${attributes})`, token.filename, token.loc.start.line);
48
- /**
49
- * Check if the script exists (only in hot mode)
50
- */
51
- buffer.writeStatement('if(__vite_hmr_script) {', token.filename, token.loc.start.line);
52
- /**
53
- * Write output
54
- */
55
- buffer.outputExpression(`__vite_hmr_script.toString()`, token.filename, token.loc.start.line, false);
56
- /**
57
- * Close if block
58
- */
59
- buffer.writeStatement('}', token.filename, token.loc.start.line);
60
- },
61
- });
62
- edge.registerTag({
63
- tagName: 'vite',
64
- seekable: true,
65
- block: false,
66
- compile(parser, buffer, token) {
67
- /**
68
- * Ensure an argument is defined
69
- */
70
- if (!token.properties.jsArg.trim()) {
71
- throw new EdgeError('Missing entrypoint name', 'E_RUNTIME_EXCEPTION', {
72
- filename: token.filename,
73
- line: token.loc.start.line,
74
- col: token.loc.start.col,
75
- });
76
- }
77
- const parsed = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
78
- const entrypoints = parser.utils.stringify(parsed);
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);
83
- },
84
- });
85
- };
86
- };
@@ -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 {};
@@ -1,36 +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
- /**
10
- * Returns a new array with unique items by the given key
11
- */
12
- export function uniqBy(array, key) {
13
- const seen = new Set();
14
- return array.filter((item) => {
15
- const k = item[key];
16
- return seen.has(k) ? false : seen.add(k);
17
- });
18
- }
19
- /**
20
- * Convert Record of attributes to a valid HTML string
21
- */
22
- export function makeAttributes(attributes) {
23
- return Object.keys(attributes)
24
- .map((key) => {
25
- const value = attributes[key];
26
- if (value === true) {
27
- return key;
28
- }
29
- if (!value) {
30
- return null;
31
- }
32
- return `${key}="${value}"`;
33
- })
34
- .filter((attr) => attr !== null)
35
- .join(' ');
36
- }
@@ -1,259 +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
- import { existsSync, readFileSync } from 'node:fs';
10
- import debug from './debug.js';
11
- import { makeAttributes, uniqBy } from './utils.js';
12
- /**
13
- * Vite class exposes the APIs to generate tags and URLs for
14
- * assets processed using vite.
15
- */
16
- export class Vite {
17
- /**
18
- * We cache the manifest file content in production
19
- * to avoid reading the file multiple times
20
- */
21
- #manifestCache = null;
22
- /**
23
- * Configuration options
24
- */
25
- #options;
26
- constructor(options) {
27
- this.#options = options;
28
- this.#options.assetsUrl = (this.#options.assetsUrl || '/').replace(/\/$/, '');
29
- debug('vite config %O', this.#options);
30
- }
31
- /**
32
- * Checks if the application is running in hot mode
33
- */
34
- #isRunningHot() {
35
- return existsSync(this.#options.hotFile);
36
- }
37
- /**
38
- * Reads the file contents as JSON
39
- */
40
- #readFileAsJSON(filePath) {
41
- return JSON.parse(readFileSync(filePath, 'utf-8'));
42
- }
43
- /**
44
- * Returns the parsed hot file content
45
- */
46
- #readHotFile() {
47
- return this.#readFileAsJSON(this.#options.hotFile);
48
- }
49
- /**
50
- * Get the path to an asset when running in hot mode
51
- */
52
- #hotAsset(asset) {
53
- return this.#readHotFile().url + '/' + asset;
54
- }
55
- /**
56
- * Unwrap attributes from the user defined function or return
57
- * the attributes as it is
58
- */
59
- #unwrapAttributes(src, url, attributes) {
60
- if (typeof attributes === 'function') {
61
- return attributes({ src, url });
62
- }
63
- return attributes;
64
- }
65
- /**
66
- * Create a script tag for the given path
67
- */
68
- #makeScriptTag(src, url, attributes) {
69
- const customAttributes = this.#unwrapAttributes(src, url, this.#options.scriptAttributes);
70
- return this.#generateElement({
71
- tag: 'script',
72
- attributes: { type: 'module', ...customAttributes, ...attributes, src: url },
73
- children: [],
74
- });
75
- }
76
- /**
77
- * Create a style tag for the given path
78
- */
79
- #makeStyleTag(src, url, attributes) {
80
- const customAttributes = this.#unwrapAttributes(src, url, this.#options.styleAttributes);
81
- return this.#generateElement({
82
- tag: 'link',
83
- attributes: { rel: 'stylesheet', ...customAttributes, ...attributes, href: url },
84
- });
85
- }
86
- /**
87
- * Generate a HTML tag for the given asset
88
- */
89
- #generateTag(asset, attributes) {
90
- let url = '';
91
- if (this.#isRunningHot()) {
92
- url = this.#hotAsset(asset);
93
- }
94
- else {
95
- url = `${this.#options.assetsUrl}/${asset}`;
96
- }
97
- if (this.#isCssPath(asset)) {
98
- return this.#makeStyleTag(asset, url, attributes);
99
- }
100
- return this.#makeScriptTag(asset, url, attributes);
101
- }
102
- /**
103
- * Generates a JSON element with a custom toString implementation
104
- */
105
- #generateElement(element) {
106
- return {
107
- ...element,
108
- toString() {
109
- const attributes = `${makeAttributes(element.attributes)}`;
110
- if (element.tag === 'link') {
111
- return `<${element.tag} ${attributes}/>`;
112
- }
113
- return `<${element.tag} ${attributes}>${element.children.join('\n')}</${element.tag}>`;
114
- },
115
- };
116
- }
117
- /**
118
- * Returns the script needed for the HMR working with Vite
119
- */
120
- #getViteHmrScript(attributes) {
121
- return this.#generateElement({
122
- tag: 'script',
123
- attributes: {
124
- type: 'module',
125
- src: this.#hotAsset('@vite/client'),
126
- ...attributes,
127
- },
128
- children: [],
129
- });
130
- }
131
- /**
132
- * Generate style and script tags for the given entrypoints
133
- * Also adds the @vite/client script
134
- */
135
- #generateEntryPointsTagsForHotMode(entryPoints, attributes) {
136
- const viteHmr = this.#getViteHmrScript(attributes);
137
- const tags = entryPoints.map((entrypoint) => this.#generateTag(entrypoint, attributes));
138
- return viteHmr ? [viteHmr].concat(tags) : tags;
139
- }
140
- /**
141
- * Generate style and script tags for the given entrypoints
142
- * using the manifest file
143
- */
144
- #generateEntryPointsTagsWithManifest(entryPoints, attributes) {
145
- const manifest = this.manifest();
146
- const tags = [];
147
- for (const entryPoint of entryPoints) {
148
- const chunk = this.#chunk(manifest, entryPoint);
149
- tags.push({
150
- path: chunk.file,
151
- tag: this.#generateTag(chunk.file, { ...attributes, integrity: chunk.integrity }),
152
- });
153
- for (const css of chunk.css || []) {
154
- tags.push({
155
- path: css,
156
- tag: this.#generateTag(css),
157
- });
158
- }
159
- }
160
- return uniqBy(tags, 'path')
161
- .sort((a) => (a.path.endsWith('.css') ? -1 : 1))
162
- .map((tag) => tag.tag);
163
- }
164
- /**
165
- * Get a chunk from the manifest file for a given file name
166
- */
167
- #chunk(manifest, fileName) {
168
- const chunk = manifest[fileName];
169
- if (!chunk) {
170
- throw new Error(`Cannot find "${fileName}" chunk in the manifest file`);
171
- }
172
- return chunk;
173
- }
174
- /**
175
- * Check if the given path is a CSS path
176
- */
177
- #isCssPath(path) {
178
- return path.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss)$/) !== null;
179
- }
180
- /**
181
- * Generate tags for the entry points
182
- */
183
- generateEntryPointsTags(entryPoints, attributes) {
184
- entryPoints = Array.isArray(entryPoints) ? entryPoints : [entryPoints];
185
- if (this.#isRunningHot()) {
186
- return this.#generateEntryPointsTagsForHotMode(entryPoints, attributes);
187
- }
188
- return this.#generateEntryPointsTagsWithManifest(entryPoints, attributes);
189
- }
190
- /**
191
- * Returns the dev server URL when running in hot
192
- * mode. Otherwise an empty string
193
- */
194
- devUrl() {
195
- if (this.#isRunningHot()) {
196
- return this.#readHotFile().url;
197
- }
198
- return '';
199
- }
200
- /**
201
- * Returns the dev server URL when running in hot
202
- * mode, otherwise returns the explicitly configured
203
- * "assets" URL
204
- */
205
- assetsUrl() {
206
- if (this.#isRunningHot()) {
207
- return this.#readHotFile().url;
208
- }
209
- return this.#options.assetsUrl;
210
- }
211
- /**
212
- * Returns path to a given asset file
213
- */
214
- assetPath(asset) {
215
- if (this.#isRunningHot()) {
216
- return this.#hotAsset(asset);
217
- }
218
- const chunk = this.#chunk(this.manifest(), asset);
219
- return `${this.#options.assetsUrl}/${chunk.file}`;
220
- }
221
- /**
222
- * Returns the manifest file contents
223
- *
224
- * @throws Will throw an exception when running in hot mode
225
- */
226
- manifest() {
227
- if (this.#isRunningHot()) {
228
- throw new Error('Cannot read the manifest file when running in hot mode');
229
- }
230
- if (!this.#manifestCache) {
231
- this.#manifestCache = this.#readFileAsJSON(this.#options.manifestFile);
232
- }
233
- return this.#manifestCache;
234
- }
235
- /**
236
- * Returns the script needed for the HMR working with React
237
- */
238
- getReactHmrScript(attributes) {
239
- if (!this.#isRunningHot()) {
240
- return null;
241
- }
242
- return this.#generateElement({
243
- tag: 'script',
244
- attributes: {
245
- type: 'module',
246
- ...attributes,
247
- },
248
- children: [
249
- '',
250
- `import RefreshRuntime from '${this.#hotAsset('@react-refresh')}'`,
251
- `RefreshRuntime.injectIntoGlobalHook(window)`,
252
- `window.$RefreshReg$ = () => {}`,
253
- `window.$RefreshSig$ = () => (type) => type`,
254
- `window.__vite_plugin_react_preamble_installed__ = true`,
255
- '',
256
- ],
257
- });
258
- }
259
- }