@navita/core 0.0.8 → 0.0.9
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/createRenderer.d.ts +4 -1
- package/createRenderer.js +24 -31
- package/createRenderer.mjs +24 -31
- package/evaluateAndProcess.d.ts +10 -3
- package/evaluateAndProcess.js +9 -14
- package/evaluateAndProcess.mjs +9 -14
- package/package.json +5 -4
package/createRenderer.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as magic_string from 'magic-string';
|
|
1
2
|
import { Options as Options$1, Engine, UsedIdCache } from '@navita/engine';
|
|
2
3
|
export { Engine, Options as EngineOptions, UsedIdCache } from '@navita/engine';
|
|
3
4
|
import { ImportMap } from '@navita/types';
|
|
@@ -8,8 +9,9 @@ interface Options {
|
|
|
8
9
|
readFile: (filepath: string) => Promise<string>;
|
|
9
10
|
importMap: ImportMap;
|
|
10
11
|
engineOptions?: Options$1;
|
|
12
|
+
context?: string;
|
|
11
13
|
}
|
|
12
|
-
declare function createRenderer({ resolver, readFile, importMap, engineOptions, }: Options): {
|
|
14
|
+
declare function createRenderer({ resolver, readFile, importMap, engineOptions, context, }: Options): {
|
|
13
15
|
engine: Engine;
|
|
14
16
|
transformAndProcess({ content, filePath, }: {
|
|
15
17
|
content: string;
|
|
@@ -18,6 +20,7 @@ declare function createRenderer({ resolver, readFile, importMap, engineOptions,
|
|
|
18
20
|
result: string;
|
|
19
21
|
dependencies: string[];
|
|
20
22
|
usedIds: UsedIdCache;
|
|
23
|
+
sourceMap: magic_string.SourceMap;
|
|
21
24
|
}>;
|
|
22
25
|
};
|
|
23
26
|
type Renderer = ReturnType<typeof createRenderer>;
|
package/createRenderer.js
CHANGED
|
@@ -1,52 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var engine = require('@navita/engine');
|
|
4
|
-
var
|
|
4
|
+
var MagicString = require('magic-string');
|
|
5
5
|
var evaluateAndProcess = require('./evaluateAndProcess.js');
|
|
6
6
|
require('node:path');
|
|
7
|
+
require('@navita/swc');
|
|
7
8
|
require('node:vm');
|
|
8
9
|
require('node:fs');
|
|
9
10
|
require('enhanced-resolve');
|
|
10
11
|
require('@navita/adapter');
|
|
11
12
|
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const engine$1 = new engine.Engine(engineOptions);
|
|
13
|
+
function createRenderer({ resolver , readFile , importMap =[] , engineOptions , context }) {
|
|
14
|
+
const engine$1 = new engine.Engine({
|
|
15
|
+
context,
|
|
16
|
+
...engineOptions || {}
|
|
17
|
+
});
|
|
18
18
|
return {
|
|
19
19
|
engine: engine$1,
|
|
20
20
|
async transformAndProcess ({ content , filePath }) {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
]);
|
|
36
|
-
const finalContent = [];
|
|
37
|
-
// Check if content starts with "use client".
|
|
38
|
-
if (startsWithRSCDirective(content)) {
|
|
39
|
-
const [directive, ...restContent] = newContent.trim().split('\n');
|
|
40
|
-
finalContent.push(directive, result, ...restContent);
|
|
41
|
-
} else {
|
|
42
|
-
finalContent.push(result, newContent.trim());
|
|
21
|
+
const { result , dependencies } = await evaluateAndProcess.evaluateAndProcess({
|
|
22
|
+
type: 'entryPoint',
|
|
23
|
+
source: content,
|
|
24
|
+
filePath,
|
|
25
|
+
resolver,
|
|
26
|
+
readFile,
|
|
27
|
+
importMap,
|
|
28
|
+
engine: engine$1
|
|
29
|
+
});
|
|
30
|
+
const newSource = new MagicString(content, {
|
|
31
|
+
filename: filePath
|
|
32
|
+
});
|
|
33
|
+
for (const { start , end , value } of result.reverse()){
|
|
34
|
+
newSource.update(start, end, value);
|
|
43
35
|
}
|
|
44
36
|
return {
|
|
45
|
-
result:
|
|
37
|
+
result: newSource.toString(),
|
|
46
38
|
dependencies,
|
|
47
39
|
usedIds: engine$1.getUsedCacheIds([
|
|
48
40
|
filePath
|
|
49
|
-
])
|
|
41
|
+
]),
|
|
42
|
+
sourceMap: newSource.generateMap()
|
|
50
43
|
};
|
|
51
44
|
}
|
|
52
45
|
};
|
package/createRenderer.mjs
CHANGED
|
@@ -1,50 +1,43 @@
|
|
|
1
1
|
import { Engine } from '@navita/engine';
|
|
2
|
-
import
|
|
2
|
+
import MagicString from 'magic-string';
|
|
3
3
|
import { evaluateAndProcess } from './evaluateAndProcess.mjs';
|
|
4
4
|
import 'node:path';
|
|
5
|
+
import '@navita/swc';
|
|
5
6
|
import 'node:vm';
|
|
6
7
|
import 'node:fs';
|
|
7
8
|
import 'enhanced-resolve';
|
|
8
9
|
import '@navita/adapter';
|
|
9
10
|
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const engine = new Engine(engineOptions);
|
|
11
|
+
function createRenderer({ resolver , readFile , importMap =[] , engineOptions , context }) {
|
|
12
|
+
const engine = new Engine({
|
|
13
|
+
context,
|
|
14
|
+
...engineOptions || {}
|
|
15
|
+
});
|
|
16
16
|
return {
|
|
17
17
|
engine,
|
|
18
18
|
async transformAndProcess ({ content , filePath }) {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
]);
|
|
34
|
-
const finalContent = [];
|
|
35
|
-
// Check if content starts with "use client".
|
|
36
|
-
if (startsWithRSCDirective(content)) {
|
|
37
|
-
const [directive, ...restContent] = newContent.trim().split('\n');
|
|
38
|
-
finalContent.push(directive, result, ...restContent);
|
|
39
|
-
} else {
|
|
40
|
-
finalContent.push(result, newContent.trim());
|
|
19
|
+
const { result , dependencies } = await evaluateAndProcess({
|
|
20
|
+
type: 'entryPoint',
|
|
21
|
+
source: content,
|
|
22
|
+
filePath,
|
|
23
|
+
resolver,
|
|
24
|
+
readFile,
|
|
25
|
+
importMap,
|
|
26
|
+
engine
|
|
27
|
+
});
|
|
28
|
+
const newSource = new MagicString(content, {
|
|
29
|
+
filename: filePath
|
|
30
|
+
});
|
|
31
|
+
for (const { start , end , value } of result.reverse()){
|
|
32
|
+
newSource.update(start, end, value);
|
|
41
33
|
}
|
|
42
34
|
return {
|
|
43
|
-
result:
|
|
35
|
+
result: newSource.toString(),
|
|
44
36
|
dependencies,
|
|
45
37
|
usedIds: engine.getUsedCacheIds([
|
|
46
38
|
filePath
|
|
47
|
-
])
|
|
39
|
+
]),
|
|
40
|
+
sourceMap: newSource.generateMap()
|
|
48
41
|
};
|
|
49
42
|
}
|
|
50
43
|
};
|
package/evaluateAndProcess.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Engine } from '@navita/engine';
|
|
2
2
|
import { ImportMap } from '@navita/types';
|
|
3
3
|
|
|
4
|
+
type FilePath$1 = string;
|
|
5
|
+
type ResolverCache = Record<FilePath$1, unknown>;
|
|
6
|
+
type NodeModuleCache = Record<FilePath$1, unknown>;
|
|
7
|
+
|
|
4
8
|
type FilePath = string;
|
|
5
|
-
type
|
|
6
|
-
|
|
9
|
+
type CollectedResults = Record<FilePath, {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
value: string;
|
|
13
|
+
}[]>;
|
|
7
14
|
|
|
8
15
|
type FilePathWithType = string;
|
|
9
16
|
type ModuleCache = Map<FilePathWithType, {
|
|
@@ -20,7 +27,7 @@ interface Caches {
|
|
|
20
27
|
}
|
|
21
28
|
type Types = 'entryPoint' | 'dependency';
|
|
22
29
|
interface Output<Type extends Types> {
|
|
23
|
-
result: Type extends 'entryPoint' ?
|
|
30
|
+
result: Type extends 'entryPoint' ? CollectedResults[number] : Record<string, unknown>;
|
|
24
31
|
dependencies: string[];
|
|
25
32
|
}
|
|
26
33
|
declare function evaluateAndProcess<Type extends 'entryPoint' | 'dependency'>({ type, filePath, source, engine, resolver, readFile, importMap, nodeModuleCache, resolverCache, moduleCache, }: {
|
package/evaluateAndProcess.js
CHANGED
|
@@ -5,8 +5,8 @@ var swc = require('@navita/swc');
|
|
|
5
5
|
var vm = require('node:vm');
|
|
6
6
|
var fs = require('node:fs');
|
|
7
7
|
var enhancedResolve = require('enhanced-resolve');
|
|
8
|
-
var engine = require('@navita/engine');
|
|
9
8
|
var adapter = require('@navita/adapter');
|
|
9
|
+
var engine = require('@navita/engine');
|
|
10
10
|
|
|
11
11
|
function createMagicProxy() {
|
|
12
12
|
// Todo: at some point, the magic proxy should notify the user
|
|
@@ -146,16 +146,6 @@ function createDefineFunction({ filePath , resolver , isExternal , resolverCache
|
|
|
146
146
|
};
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
function formatResults(results = []) {
|
|
150
|
-
const result = results.map((item)=>{
|
|
151
|
-
if (item instanceof engine.Static) {
|
|
152
|
-
return '';
|
|
153
|
-
}
|
|
154
|
-
return JSON.stringify(item);
|
|
155
|
-
});
|
|
156
|
-
return `const $$evaluatedValues = [${result.join(',')}];`;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
149
|
function setAdapter({ engine: engine$1 , collectResults , }) {
|
|
160
150
|
adapter.setAdapter({
|
|
161
151
|
generateIdentifier: (value)=>engine$1.generateIdentifier(value),
|
|
@@ -171,7 +161,7 @@ function setAdapter({ engine: engine$1 , collectResults , }) {
|
|
|
171
161
|
}
|
|
172
162
|
return engine$1.addFontFace(fontFace);
|
|
173
163
|
},
|
|
174
|
-
collectResult ({ index , filePath , identifier , result: resultFactory , line , column , }) {
|
|
164
|
+
collectResult ({ index , filePath , identifier , result: resultFactory , sourceMap: { line , column } , position , }) {
|
|
175
165
|
if (index === 0) {
|
|
176
166
|
if (collectResults) {
|
|
177
167
|
collectResults[filePath] = [];
|
|
@@ -198,7 +188,12 @@ function setAdapter({ engine: engine$1 , collectResults , }) {
|
|
|
198
188
|
}
|
|
199
189
|
}
|
|
200
190
|
if (collectResults) {
|
|
201
|
-
|
|
191
|
+
const [start, end] = position;
|
|
192
|
+
collectResults[filePath][index] = {
|
|
193
|
+
start,
|
|
194
|
+
end,
|
|
195
|
+
value: result === undefined ? "undefined" : JSON.stringify(result)
|
|
196
|
+
};
|
|
202
197
|
}
|
|
203
198
|
return result;
|
|
204
199
|
}
|
|
@@ -257,7 +252,7 @@ async function evaluateAndProcess({ type , filePath , source , engine , resolver
|
|
|
257
252
|
return compiledFn().then(({ dependencies , exports })=>{
|
|
258
253
|
if (type === 'entryPoint') {
|
|
259
254
|
return {
|
|
260
|
-
result:
|
|
255
|
+
result: collectedResults[filePath],
|
|
261
256
|
dependencies
|
|
262
257
|
};
|
|
263
258
|
}
|
package/evaluateAndProcess.mjs
CHANGED
|
@@ -7,8 +7,8 @@ import { extraction } from '@navita/swc';
|
|
|
7
7
|
import vm from 'node:vm';
|
|
8
8
|
import fs from 'node:fs';
|
|
9
9
|
import enhancedResolve from 'enhanced-resolve';
|
|
10
|
-
import { Static, ClassList } from '@navita/engine';
|
|
11
10
|
import { setAdapter as setAdapter$1 } from '@navita/adapter';
|
|
11
|
+
import { ClassList } from '@navita/engine';
|
|
12
12
|
|
|
13
13
|
function createMagicProxy() {
|
|
14
14
|
// Todo: at some point, the magic proxy should notify the user
|
|
@@ -148,16 +148,6 @@ function createDefineFunction({ filePath , resolver , isExternal , resolverCache
|
|
|
148
148
|
};
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
function formatResults(results = []) {
|
|
152
|
-
const result = results.map((item)=>{
|
|
153
|
-
if (item instanceof Static) {
|
|
154
|
-
return '';
|
|
155
|
-
}
|
|
156
|
-
return JSON.stringify(item);
|
|
157
|
-
});
|
|
158
|
-
return `const $$evaluatedValues = [${result.join(',')}];`;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
151
|
function setAdapter({ engine , collectResults , }) {
|
|
162
152
|
setAdapter$1({
|
|
163
153
|
generateIdentifier: (value)=>engine.generateIdentifier(value),
|
|
@@ -173,7 +163,7 @@ function setAdapter({ engine , collectResults , }) {
|
|
|
173
163
|
}
|
|
174
164
|
return engine.addFontFace(fontFace);
|
|
175
165
|
},
|
|
176
|
-
collectResult ({ index , filePath , identifier , result: resultFactory , line , column , }) {
|
|
166
|
+
collectResult ({ index , filePath , identifier , result: resultFactory , sourceMap: { line , column } , position , }) {
|
|
177
167
|
if (index === 0) {
|
|
178
168
|
if (collectResults) {
|
|
179
169
|
collectResults[filePath] = [];
|
|
@@ -200,7 +190,12 @@ function setAdapter({ engine , collectResults , }) {
|
|
|
200
190
|
}
|
|
201
191
|
}
|
|
202
192
|
if (collectResults) {
|
|
203
|
-
|
|
193
|
+
const [start, end] = position;
|
|
194
|
+
collectResults[filePath][index] = {
|
|
195
|
+
start,
|
|
196
|
+
end,
|
|
197
|
+
value: result === undefined ? "undefined" : JSON.stringify(result)
|
|
198
|
+
};
|
|
204
199
|
}
|
|
205
200
|
return result;
|
|
206
201
|
}
|
|
@@ -259,7 +254,7 @@ async function evaluateAndProcess({ type , filePath , source , engine , resolver
|
|
|
259
254
|
return compiledFn().then(({ dependencies , exports })=>{
|
|
260
255
|
if (type === 'entryPoint') {
|
|
261
256
|
return {
|
|
262
|
-
result:
|
|
257
|
+
result: collectedResults[filePath],
|
|
263
258
|
dependencies
|
|
264
259
|
};
|
|
265
260
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navita/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "Core package for Navita. Used for creating integrations.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"core",
|
|
@@ -32,10 +32,11 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"enhanced-resolve": "^5.15.0",
|
|
34
34
|
"outdent": "^0.8.0",
|
|
35
|
+
"magic-string": "^0.30.3",
|
|
35
36
|
"@navita/types": "0.0.8",
|
|
36
|
-
"@navita/adapter": "0.0.
|
|
37
|
-
"@navita/swc": "0.0.
|
|
38
|
-
"@navita/engine": "0.0.
|
|
37
|
+
"@navita/adapter": "0.0.9",
|
|
38
|
+
"@navita/swc": "0.0.9",
|
|
39
|
+
"@navita/engine": "0.0.9"
|
|
39
40
|
},
|
|
40
41
|
"license": "MIT",
|
|
41
42
|
"author": "Eagerpatch",
|