@cypress/vite-dev-server 3.3.1 → 4.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.
@@ -0,0 +1 @@
1
+ export declare const dynamicImport: <T>(module: string) => Promise<T>;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dynamicImport = void 0;
4
+ // tsc will compile `import(...)` calls to require unless a different tsconfig.module value
5
+ // is used (e.g. module=node16). To change this, we would also have to change the ts-node behavior when requiring the
6
+ // Cypress config file. This hack for keeping dynamic imports from being converted works across all
7
+ // of our supported node versions
8
+ const _dynamicImport = new Function('specifier', 'return import(specifier)');
9
+ const dynamicImport = (module) => _dynamicImport(module);
10
+ exports.dynamicImport = dynamicImport;
@@ -3,12 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cypress = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const debug_1 = tslib_1.__importDefault(require("debug"));
6
- const pathe_1 = require("pathe");
7
6
  const node_html_parser_1 = require("node-html-parser");
8
7
  const fs_1 = tslib_1.__importDefault(require("fs"));
9
8
  const path_1 = tslib_1.__importDefault(require("path"));
10
9
  const debug = (0, debug_1.default)('cypress:vite-dev-server:plugins:cypress');
11
- const INIT_FILEPATH = (0, pathe_1.resolve)(__dirname, '../../client/initCypressTests.js');
10
+ const INIT_FILEPATH = path_1.default.resolve(__dirname, '../../client/initCypressTests.js');
12
11
  const HMR_DEPENDENCY_LOOKUP_MAX_ITERATION = 50;
13
12
  function getSpecsPathsSet(specs) {
14
13
  return new Set(specs.map((spec) => spec.absolute));
@@ -44,7 +43,7 @@ const Cypress = (options, vite) => {
44
43
  const scriptTagsToInject = root.childNodes.filter((node) => {
45
44
  return node instanceof node_html_parser_1.HTMLElement && node.rawTagName === 'script';
46
45
  });
47
- const indexHtmlPath = (0, pathe_1.resolve)(projectRoot, indexHtmlFile);
46
+ const indexHtmlPath = path_1.default.resolve(projectRoot, indexHtmlFile);
48
47
  debug('resolved the indexHtmlPath as', indexHtmlPath, 'from', indexHtmlFile);
49
48
  let indexHtmlContent = await fs_1.default.promises.readFile(indexHtmlPath, { encoding: 'utf8' });
50
49
  // Inject the script tags
@@ -75,7 +74,7 @@ const Cypress = (options, vite) => {
75
74
  handleHotUpdate: ({ server, file }) => {
76
75
  debug('handleHotUpdate - file', file);
77
76
  // If the user provided IndexHtml is changed, do a full-reload
78
- if (vite.normalizePath(file) === (0, pathe_1.resolve)(projectRoot, indexHtmlFile)) {
77
+ if (vite.normalizePath(file) === path_1.default.resolve(projectRoot, indexHtmlFile)) {
79
78
  server.ws.send({
80
79
  type: 'full-reload',
81
80
  });
@@ -1,3 +1,2 @@
1
- export * from './inspect';
2
1
  export * from './cypress';
3
2
  export * from './sourcemap';
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./inspect"), exports);
5
4
  tslib_1.__exportStar(require("./cypress"), exports);
6
5
  tslib_1.__exportStar(require("./sourcemap"), exports);
@@ -8,45 +8,57 @@ const tslib_1 = require("tslib");
8
8
  * You can find it here https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/create.ts
9
9
  */
10
10
  const debug_1 = tslib_1.__importDefault(require("debug"));
11
- const local_pkg_1 = require("local-pkg");
12
- const pathe_1 = require("pathe");
13
11
  const path_1 = tslib_1.__importDefault(require("path"));
14
12
  const constants_1 = require("./constants");
15
13
  const index_1 = require("./plugins/index");
14
+ const dynamic_import_1 = require("./dynamic-import");
16
15
  const debug = (0, debug_1.default)('cypress:vite-dev-server:resolve-config');
17
16
  const createViteDevServerConfig = async (config, vite) => {
18
- const { specs, cypressConfig, viteConfig: viteOverrides } = config;
19
- const root = cypressConfig.projectRoot;
20
- const { default: findUp } = await (0, local_pkg_1.importModule)('find-up');
21
- const configFile = await findUp(constants_1.configFiles, { cwd: root });
22
- // INFO logging, a lot is logged here.
23
- // debug('all dev-server options are', options)
24
- if (configFile) {
25
- debug('resolved config file at', configFile, 'using root', root);
26
- }
27
- else if (viteOverrides) {
28
- debug(`Couldn't find a Vite config file, however we received a custom viteConfig`, viteOverrides);
17
+ const { viteConfig: inlineViteConfig, cypressConfig: { projectRoot } } = config;
18
+ let resolvedOverrides = {};
19
+ if (inlineViteConfig) {
20
+ debug(`Received a custom viteConfig`, inlineViteConfig);
21
+ if (typeof inlineViteConfig === 'function') {
22
+ resolvedOverrides = await inlineViteConfig();
23
+ }
24
+ else if (typeof inlineViteConfig === 'object') {
25
+ resolvedOverrides = inlineViteConfig;
26
+ }
27
+ // Set "configFile: false" to disable auto resolution of <project-root>/vite.config.js
28
+ resolvedOverrides = Object.assign({ configFile: false }, resolvedOverrides);
29
29
  }
30
30
  else {
31
- if (config.onConfigNotFound) {
32
- config.onConfigNotFound('vite', root, constants_1.configFiles);
33
- // The config process will be killed from the parent, but we want to early exit so we don't get
34
- // any additional errors related to not having a config
35
- process.exit(0);
36
- }
37
- else {
38
- throw new Error(`Your component devServer config for vite is missing a required viteConfig property, since we could not automatically detect one.\n Please add one to your ${config.cypressConfig.configFile}`);
31
+ const { findUp } = await (0, dynamic_import_1.dynamicImport)('find-up');
32
+ const configFile = await findUp(constants_1.configFiles, { cwd: projectRoot });
33
+ if (!configFile) {
34
+ if (config.onConfigNotFound) {
35
+ config.onConfigNotFound('vite', projectRoot, constants_1.configFiles);
36
+ // The config process will be killed from the parent, but we want to early exit so we don't get
37
+ // any additional errors related to not having a config
38
+ process.exit(0);
39
+ }
40
+ else {
41
+ throw new Error(`Your component devServer config for vite is missing a required viteConfig property, since we could not automatically detect one.\n Please add one to your ${config.cypressConfig.configFile}`);
42
+ }
39
43
  }
44
+ debug('Resolved config file at', configFile, 'using root', projectRoot);
45
+ resolvedOverrides = { configFile };
40
46
  }
47
+ const finalConfig = vite.mergeConfig(resolvedOverrides, makeCypressViteConfig(config, vite));
48
+ debug('The resolved server config is', JSON.stringify(finalConfig, null, 2));
49
+ return finalConfig;
50
+ };
51
+ exports.createViteDevServerConfig = createViteDevServerConfig;
52
+ function makeCypressViteConfig(config, vite) {
53
+ const { cypressConfig: { projectRoot, devServerPublicPathRoute, supportFile, cypressBinaryRoot, isTextTerminal, }, specs, } = config;
41
54
  // Vite caches its output in the .vite directory in the node_modules where vite lives.
42
55
  // So we want to find that node_modules path and ensure it's added to the "allow" list
43
56
  const vitePathNodeModules = path_1.default.dirname(path_1.default.dirname(require.resolve(`vite/package.json`, {
44
- paths: [root],
57
+ paths: [projectRoot],
45
58
  })));
46
- const viteBaseConfig = {
47
- root,
48
- base: `${cypressConfig.devServerPublicPathRoute}/`,
49
- configFile,
59
+ return {
60
+ root: projectRoot,
61
+ base: `${devServerPublicPathRoute}/`,
50
62
  optimizeDeps: {
51
63
  esbuildOptions: {
52
64
  incremental: true,
@@ -67,42 +79,22 @@ const createViteDevServerConfig = async (config, vite) => {
67
79
  ],
68
80
  },
69
81
  entries: [
70
- ...specs.map((s) => (0, pathe_1.relative)(root, s.relative)),
71
- ...(cypressConfig.supportFile ? [(0, pathe_1.resolve)(root, cypressConfig.supportFile)] : []),
82
+ ...specs.map((s) => path_1.default.relative(projectRoot, s.relative)),
83
+ ...(supportFile ? [path_1.default.resolve(projectRoot, supportFile)] : []),
72
84
  ].filter((v) => v != null),
73
85
  },
74
- server: {
75
- fs: {
86
+ server: Object.assign({ fs: {
76
87
  allow: [
77
- root,
88
+ projectRoot,
78
89
  vitePathNodeModules,
79
- cypressConfig.cypressBinaryRoot,
90
+ cypressBinaryRoot,
80
91
  ],
81
- },
82
- host: '127.0.0.1',
83
- },
92
+ }, host: '127.0.0.1' }, (isTextTerminal
93
+ ? { watch: { ignored: '**/*' }, hmr: false }
94
+ : {})),
84
95
  plugins: [
85
96
  (0, index_1.Cypress)(config, vite),
86
- (0, index_1.CypressInspect)(config),
87
97
  (0, index_1.CypressSourcemap)(config, vite),
88
- ].filter((p) => p != null),
98
+ ],
89
99
  };
90
- if (config.cypressConfig.isTextTerminal) {
91
- viteBaseConfig.server = Object.assign(Object.assign({}, (viteBaseConfig.server || {})), {
92
- // Disable file watching and HMR when executing tests in `run` mode
93
- watch: {
94
- ignored: '**/*',
95
- }, hmr: false });
96
- }
97
- let resolvedOverrides = {};
98
- if (typeof viteOverrides === 'function') {
99
- resolvedOverrides = await viteOverrides();
100
- }
101
- else if (typeof viteOverrides === 'object') {
102
- resolvedOverrides = viteOverrides;
103
- }
104
- const finalConfig = vite.mergeConfig(viteBaseConfig, resolvedOverrides);
105
- debug('The resolved server config is', JSON.stringify(finalConfig, null, 2));
106
- return finalConfig;
107
- };
108
- exports.createViteDevServerConfig = createViteDevServerConfig;
100
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cypress/vite-dev-server",
3
- "version": "3.3.1",
3
+ "version": "4.0.0",
4
4
  "description": "Launches Vite Dev Server for Component Testing",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,9 +17,7 @@
17
17
  "dependencies": {
18
18
  "debug": "4.3.3",
19
19
  "find-up": "6.3.0",
20
- "local-pkg": "0.4.1",
21
- "node-html-parser": "5.3.3",
22
- "pathe": "0.2.0"
20
+ "node-html-parser": "5.3.3"
23
21
  },
24
22
  "devDependencies": {
25
23
  "chai": "^4.3.6",
@@ -1,3 +0,0 @@
1
- import type { PluginOption } from 'vite';
2
- import type { ViteDevServerConfig } from '../devServer';
3
- export declare const CypressInspect: (config: ViteDevServerConfig) => PluginOption | null;
@@ -1,27 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CypressInspect = void 0;
4
- const tslib_1 = require("tslib");
5
- const debug_1 = tslib_1.__importDefault(require("debug"));
6
- const debug = (0, debug_1.default)('cypress:vite-dev-server:plugins:inspect');
7
- const CypressInspect = (config) => {
8
- var _a;
9
- if (!process.env.CYPRESS_INTERNAL_VITE_INSPECT) {
10
- debug('skipping vite inspect because CYPRESS_INTERNAL_VITE_INSPECT is not set');
11
- return null;
12
- }
13
- let Inspect;
14
- try {
15
- const inspectPluginPath = require.resolve('vite-plugin-inspect', { paths: [config.cypressConfig.projectRoot] });
16
- const inspectModule = require(inspectPluginPath);
17
- Inspect = (_a = inspectModule.default) !== null && _a !== void 0 ? _a : inspectModule;
18
- debug('inspect was found', Inspect);
19
- }
20
- catch (err) {
21
- debug(`Tried to import the inspect plugin 'vite-plugin-inspect'. It's an optional peerDependency so install it if you'd like.`);
22
- debug(err);
23
- return null;
24
- }
25
- return Object.assign(Object.assign({}, Inspect()), { name: 'cypress:inspect' });
26
- };
27
- exports.CypressInspect = CypressInspect;