vike 0.4.249-commit-d781796 → 0.4.249-commit-03c7a15

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.
@@ -90,10 +90,12 @@ type RemoveTarget = {
90
90
  * }
91
91
  */
92
92
  type StaticReplace = {
93
- /** Type of transformation - currently only 'call' is supported, but can be extended in the future */
94
- type?: 'call';
95
93
  /** Environment filter: 'client' = client only, 'server' = everything except client */
96
- env?: 'server' | 'client';
94
+ env: 'server' | 'client';
95
+ /** Rolldown filter — MUST be narrowing as much as possible, otherwise you'll get significant performance degradation */
96
+ filter: string;
97
+ /** Type of transformation - currently only 'call' is supported, but can be extended in the future */
98
+ type: 'call';
97
99
  /** Match criteria */
98
100
  match: {
99
101
  /**
@@ -110,13 +112,7 @@ type StaticReplace = {
110
112
  /** Remove target (optional) */
111
113
  remove?: RemoveTarget;
112
114
  };
113
- type TransformInput = {
114
- code: string;
115
- id: string;
116
- env: string;
117
- options: StaticReplace[];
118
- };
119
- declare function applyStaticReplace({ code, id, env, options }: TransformInput): Promise<{
115
+ declare function applyStaticReplace(code: string, staticReplaceList: StaticReplace[], id: string, env: 'server' | 'client'): Promise<{
120
116
  code: string;
121
117
  map: {
122
118
  version: number;
@@ -127,4 +123,4 @@ declare function applyStaticReplace({ code, id, env, options }: TransformInput):
127
123
  mappings: string;
128
124
  file: string;
129
125
  } | null | undefined;
130
- } | null>;
126
+ } | null | undefined>;
@@ -2,19 +2,23 @@ export { applyStaticReplace };
2
2
  import { transformAsync } from '@babel/core';
3
3
  import * as t from '@babel/types';
4
4
  import { parseImportString } from '../../shared/importString.js';
5
- async function applyStaticReplace({ code, id, env, options }) {
6
- // 'server' means "not client" (covers ssr, cloudflare, etc.)
7
- const filteredRules = options.filter((rule) => {
8
- if (!rule.env)
9
- return true;
10
- if (rule.env === 'client')
11
- return env === 'client';
12
- if (rule.env === 'server')
13
- return env !== 'client';
14
- return false;
5
+ import { assert } from '../../utils.js';
6
+ // ============================================================================
7
+ // Main transformer
8
+ // ============================================================================
9
+ async function applyStaticReplace(code, staticReplaceList, id, env) {
10
+ assert(staticReplaceList.length > 0);
11
+ const SKIPPED = undefined;
12
+ const NO_CHANGE = null;
13
+ const staticReplaceListFiltered = staticReplaceList.filter((staticReplace) => {
14
+ if (staticReplace.env && staticReplace.env !== env)
15
+ return false;
16
+ if (!code.includes(staticReplace.filter))
17
+ return false;
18
+ return true;
15
19
  });
16
- if (filteredRules.length === 0) {
17
- return null;
20
+ if (staticReplaceListFiltered.length === 0) {
21
+ return SKIPPED;
18
22
  }
19
23
  try {
20
24
  const state = {
@@ -26,16 +30,20 @@ async function applyStaticReplace({ code, id, env, options }) {
26
30
  filename: id,
27
31
  ast: true,
28
32
  sourceMaps: true,
29
- plugins: [collectImportsPlugin(state), applyRulesPlugin(state, filteredRules), removeUnreferencedPlugin(state)],
33
+ plugins: [
34
+ collectImportsPlugin(state),
35
+ applyRulesPlugin(state, staticReplaceListFiltered),
36
+ removeUnreferencedPlugin(state),
37
+ ],
30
38
  });
31
39
  if (!result?.code || !state.modified) {
32
- return null;
40
+ return NO_CHANGE;
33
41
  }
34
42
  return { code: result.code, map: result.map };
35
43
  }
36
44
  catch (error) {
37
45
  console.error(`Error transforming ${id}:`, error);
38
- return null;
46
+ return SKIPPED;
39
47
  }
40
48
  }
41
49
  // ============================================================================
@@ -1,8 +1,7 @@
1
1
  export { pluginStaticReplace };
2
- import { assert, createDebug } from '../utils.js';
2
+ import { assert, assertUsage, createDebug } from '../utils.js';
3
3
  import { isViteServerSide_extraSafe } from '../shared/isViteServerSide.js';
4
4
  import { applyStaticReplace } from './pluginStaticReplace/applyStaticReplace.js';
5
- import { buildFilterRolldown } from './pluginStaticReplace/buildFilterRolldown.js';
6
5
  const debug = createDebug('vike:staticReplace');
7
6
  function pluginStaticReplace(vikeConfig) {
8
7
  let config;
@@ -12,7 +11,7 @@ function pluginStaticReplace(vikeConfig) {
12
11
  return [];
13
12
  // filterRolldown
14
13
  const skipNodeModules = '/node_modules/';
15
- const include = buildFilterRolldown(staticReplaceList);
14
+ const include = getFilterRolldown(staticReplaceList);
16
15
  assert(include);
17
16
  const filterRolldown = {
18
17
  id: {
@@ -25,7 +24,7 @@ function pluginStaticReplace(vikeConfig) {
25
24
  const filterFunction = (id, code) => {
26
25
  if (id.includes(skipNodeModules))
27
26
  return false;
28
- if (!include.test(code))
27
+ if (!include.some(s => code.includes(s)))
29
28
  return false;
30
29
  return true;
31
30
  };
@@ -42,17 +41,21 @@ function pluginStaticReplace(vikeConfig) {
42
41
  filter: filterRolldown,
43
42
  async handler(code, id, options) {
44
43
  assert(filterFunction(id, code));
44
+ debug('id', id);
45
45
  const env = isViteServerSide_extraSafe(config, this.environment, options) ? 'server' : 'client';
46
- const result = await applyStaticReplace({
47
- code,
48
- id,
49
- env,
50
- options: staticReplaceList,
51
- });
52
- if (debug.isActivated && result) {
53
- debug('id', id);
54
- debug('before', code);
55
- debug('after', result.code);
46
+ debug('env', env);
47
+ const result = await applyStaticReplace(code, staticReplaceList, id, env);
48
+ if (debug.isActivated) {
49
+ if (result === undefined) {
50
+ debug('Skipped');
51
+ }
52
+ if (result === null) {
53
+ debug('AST parsed, but no modifications');
54
+ }
55
+ if (result) {
56
+ debug('Before:', code);
57
+ debug('After:', result.code);
58
+ }
56
59
  }
57
60
  return result;
58
61
  },
@@ -75,3 +78,10 @@ function getStaticReplaceList(vikeConfig) {
75
78
  }
76
79
  return staticReplaceList;
77
80
  }
81
+ function getFilterRolldown(staticReplaceList) {
82
+ return staticReplaceList.map(staticReplace => {
83
+ const { filter } = staticReplace;
84
+ assertUsage(filter, '+staticReplace entry is missing rolldown filter');
85
+ return filter;
86
+ });
87
+ }
@@ -1 +1 @@
1
- export declare const PROJECT_VERSION: "0.4.249-commit-d781796";
1
+ export declare const PROJECT_VERSION: "0.4.249-commit-03c7a15";
@@ -1,2 +1,2 @@
1
1
  // Automatically updated by @brillout/release-me
2
- export const PROJECT_VERSION = '0.4.249-commit-d781796';
2
+ export const PROJECT_VERSION = '0.4.249-commit-03c7a15';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vike",
3
- "version": "0.4.249-commit-d781796",
3
+ "version": "0.4.249-commit-03c7a15",
4
4
  "repository": "https://github.com/vikejs/vike",
5
5
  "exports": {
6
6
  "./server": {
@@ -1,9 +0,0 @@
1
- export { buildFilterRolldown };
2
- import type { StaticReplace } from './applyStaticReplace.js';
3
- /**
4
- * Build a filterRolldown from staticReplaceList by extracting all import strings.
5
- * For a single entry, ALL import strings must be present (AND logic),
6
- * except for call.match.function array which is OR logic.
7
- * Between staticReplace entries it's OR logic.
8
- */
9
- declare function buildFilterRolldown(staticReplaceList: StaticReplace[]): RegExp | null;
@@ -1,100 +0,0 @@
1
- export { buildFilterRolldown };
2
- import { escapeRegex } from '../../utils.js';
3
- import { parseImportString } from '../../shared/importString.js';
4
- /**
5
- * Build a filterRolldown from staticReplaceList by extracting all import strings.
6
- * For a single entry, ALL import strings must be present (AND logic),
7
- * except for call.match.function array which is OR logic.
8
- * Between staticReplace entries it's OR logic.
9
- */
10
- function buildFilterRolldown(staticReplaceList) {
11
- const rulePatterns = [];
12
- // Process each entry separately
13
- for (const staticReplaceEntry of staticReplaceList) {
14
- const importStrings = new Set();
15
- const functionImportStrings = new Set();
16
- // Extract function import strings separately
17
- extractImportStrings(staticReplaceEntry.match.function, functionImportStrings);
18
- // Extract arg import strings
19
- if (staticReplaceEntry.match.args) {
20
- for (const condition of Object.values(staticReplaceEntry.match.args)) {
21
- extractImportStringsFromCondition(condition, importStrings);
22
- }
23
- }
24
- // Build pattern for this staticReplaceEntry
25
- const ruleParts = [];
26
- // For function imports: if array, use OR; otherwise use AND
27
- if (functionImportStrings.size > 0) {
28
- const functionPatterns = [];
29
- for (const importStr of functionImportStrings) {
30
- const parts = parseImportString(importStr);
31
- if (parts) {
32
- // Each function import should match both importPath and export name
33
- functionPatterns.push(`(?=.*${escapeRegex(parts.importPath)})(?=.*${escapeRegex(parts.exportName)})`);
34
- }
35
- }
36
- // If multiple functions, they are alternatives (OR)
37
- if (functionPatterns.length > 0) {
38
- if (functionPatterns.length === 1) {
39
- ruleParts.push(functionPatterns[0]);
40
- }
41
- else {
42
- // Multiple function patterns: file must match at least one
43
- ruleParts.push(`(?:${functionPatterns.join('|')})`);
44
- }
45
- }
46
- }
47
- // For arg imports: all must be present (AND)
48
- for (const importStr of importStrings) {
49
- const parts = parseImportString(importStr);
50
- if (parts) {
51
- // Each arg import should match both importPath and export name
52
- ruleParts.push(`(?=.*${escapeRegex(parts.importPath)})(?=.*${escapeRegex(parts.exportName)})`);
53
- }
54
- }
55
- // Combine all parts for this rule with AND logic
56
- if (ruleParts.length > 0) {
57
- // All parts must match for this rule
58
- rulePatterns.push(ruleParts.join(''));
59
- }
60
- }
61
- if (rulePatterns.length === 0)
62
- return null;
63
- // Create a regex that matches if any rule pattern matches (OR between staticReplace entries)
64
- return new RegExp(rulePatterns.join('|'), 's');
65
- }
66
- /**
67
- * Extract import strings from function patterns
68
- */
69
- function extractImportStrings(functions, result) {
70
- const arr = Array.isArray(functions) ? functions : [functions];
71
- for (const fn of arr) {
72
- if (parseImportString(fn)) {
73
- result.add(fn);
74
- }
75
- }
76
- }
77
- /**
78
- * Extract import strings from argument conditions
79
- */
80
- function extractImportStringsFromCondition(condition, result) {
81
- if (typeof condition === 'string') {
82
- if (parseImportString(condition)) {
83
- result.add(condition);
84
- }
85
- }
86
- else if (condition && typeof condition === 'object') {
87
- // Handle call condition
88
- if ('call' in condition && typeof condition.call === 'string') {
89
- if (parseImportString(condition.call)) {
90
- result.add(condition.call);
91
- }
92
- // Recursively check nested args
93
- if (condition.args) {
94
- for (const nestedCondition of Object.values(condition.args)) {
95
- extractImportStringsFromCondition(nestedCondition, result);
96
- }
97
- }
98
- }
99
- }
100
- }