@navita/vite-plugin 0.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/index.d.ts +9 -0
- package/index.js +158 -0
- package/index.mjs +140 -0
- package/package.json +18 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('node:fs');
|
|
4
|
+
var path = require('node:path');
|
|
5
|
+
var compiler = require('@navita/compiler');
|
|
6
|
+
var createRenderer = require('@navita/core/createRenderer');
|
|
7
|
+
var css = require('@navita/css');
|
|
8
|
+
var swc = require('@navita/swc');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
28
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
29
|
+
|
|
30
|
+
const isExternal = (id)=>id.includes('node_modules') || id.startsWith(path__namespace.resolve(__dirname, '../../'));
|
|
31
|
+
const ENTRY_EXTENSIONS = /\.[tj]sx?$/;
|
|
32
|
+
const VIRTUAL_MODULE_NAME = 'virtual:navita';
|
|
33
|
+
const VIRTUAL_CSS_NAME = 'virtual:navita.css';
|
|
34
|
+
async function navitaCompiler(entryPoint, resolver, renderer, onDisposePromise) {
|
|
35
|
+
const compiler$1 = await compiler.createCompiler({
|
|
36
|
+
entryPoint,
|
|
37
|
+
renderer,
|
|
38
|
+
async resolver (args) {
|
|
39
|
+
const result = await resolver(args.path, args.importer);
|
|
40
|
+
if (!result) {
|
|
41
|
+
throw new Error(`Failed to resolve ${args.path}`);
|
|
42
|
+
}
|
|
43
|
+
const external = isExternal(result.id);
|
|
44
|
+
if (external) {
|
|
45
|
+
return {
|
|
46
|
+
path: args.path,
|
|
47
|
+
sideEffects: false,
|
|
48
|
+
external: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
path: result.id,
|
|
53
|
+
sideEffects: undefined,
|
|
54
|
+
external: false
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
readFile: fs__namespace.promises.readFile
|
|
58
|
+
}, onDisposePromise);
|
|
59
|
+
return compiler$1.rebuild();
|
|
60
|
+
}
|
|
61
|
+
function navita(options) {
|
|
62
|
+
const renderer = createRenderer.createRenderer();
|
|
63
|
+
const importMap = [
|
|
64
|
+
...css.importMap,
|
|
65
|
+
...options?.importMap || []
|
|
66
|
+
];
|
|
67
|
+
let server;
|
|
68
|
+
let lastCssContent;
|
|
69
|
+
const dependencyGraph = new Map();
|
|
70
|
+
const addToGraph = (importer, source)=>{
|
|
71
|
+
if (!importer) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const dependencies = dependencyGraph.get(importer) || new Set();
|
|
75
|
+
dependencies.add(source);
|
|
76
|
+
dependencyGraph.set(importer, dependencies);
|
|
77
|
+
};
|
|
78
|
+
const findEntryPoints = (id)=>{
|
|
79
|
+
const result = [];
|
|
80
|
+
for (const [importer, dependencies] of dependencyGraph.entries()){
|
|
81
|
+
if (dependencies.has(id)) {
|
|
82
|
+
result.push(...findEntryPoints(importer), importer);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
let dispose;
|
|
88
|
+
const buildEndPromise = new Promise((resolve)=>{
|
|
89
|
+
dispose = resolve;
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
name: "navita",
|
|
93
|
+
enforce: "pre",
|
|
94
|
+
configureServer (_server) {
|
|
95
|
+
lastCssContent = undefined;
|
|
96
|
+
server = _server;
|
|
97
|
+
},
|
|
98
|
+
async buildEnd () {
|
|
99
|
+
await dispose();
|
|
100
|
+
},
|
|
101
|
+
async resolveId (source, importer, options) {
|
|
102
|
+
const resolved = await this.resolve(source, importer, {
|
|
103
|
+
...options,
|
|
104
|
+
skipSelf: true
|
|
105
|
+
});
|
|
106
|
+
if (resolved) {
|
|
107
|
+
const { id } = resolved;
|
|
108
|
+
if (!isExternal(id) && importer) {
|
|
109
|
+
addToGraph(importer, id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (source === VIRTUAL_MODULE_NAME) {
|
|
113
|
+
return VIRTUAL_CSS_NAME;
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
},
|
|
117
|
+
async load (id) {
|
|
118
|
+
if (id === VIRTUAL_CSS_NAME) {
|
|
119
|
+
return lastCssContent;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
},
|
|
123
|
+
async transform (code, id, options) {
|
|
124
|
+
// Bail as early as we can
|
|
125
|
+
if (!importMap.map((x)=>x.source).some((value)=>code.indexOf(value) !== -1)) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const entryPoint = findEntryPoints(id).find((entryPoint)=>ENTRY_EXTENSIONS.test(entryPoint)) || id;
|
|
129
|
+
const [result] = await Promise.all([
|
|
130
|
+
swc.transformer(code, {
|
|
131
|
+
filename: id,
|
|
132
|
+
importMap: []
|
|
133
|
+
}),
|
|
134
|
+
navitaCompiler(entryPoint, this.resolve.bind(this), renderer, buildEndPromise)
|
|
135
|
+
]);
|
|
136
|
+
const { result: evaluatedResult } = renderer.getResult(id, options?.ssr);
|
|
137
|
+
const newCssContent = renderer.renderToString();
|
|
138
|
+
if (lastCssContent !== newCssContent) {
|
|
139
|
+
if (server) {
|
|
140
|
+
const { moduleGraph } = server;
|
|
141
|
+
const virtualModule = moduleGraph.getModuleById(VIRTUAL_CSS_NAME);
|
|
142
|
+
if (virtualModule) {
|
|
143
|
+
moduleGraph.invalidateModule(virtualModule);
|
|
144
|
+
virtualModule.lastHMRTimestamp = Date.now();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
lastCssContent = newCssContent;
|
|
148
|
+
}
|
|
149
|
+
return `
|
|
150
|
+
${evaluatedResult}
|
|
151
|
+
${result}
|
|
152
|
+
import "${VIRTUAL_MODULE_NAME}";
|
|
153
|
+
`;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
exports.navita = navita;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { fileURLToPath as fileURLToPath$1 } from 'url';
|
|
2
|
+
const __filename = fileURLToPath$1(import.meta.url);
|
|
3
|
+
import { dirname as dirname$1 } from 'path';
|
|
4
|
+
const __dirname = dirname$1(__filename);
|
|
5
|
+
import * as fs from 'node:fs';
|
|
6
|
+
import * as path from 'node:path';
|
|
7
|
+
import { createCompiler } from '@navita/compiler';
|
|
8
|
+
import { createRenderer } from '@navita/core/createRenderer';
|
|
9
|
+
import { importMap } from '@navita/css';
|
|
10
|
+
import { transformer } from '@navita/swc';
|
|
11
|
+
|
|
12
|
+
const isExternal = (id)=>id.includes('node_modules') || id.startsWith(path.resolve(__dirname, '../../'));
|
|
13
|
+
const ENTRY_EXTENSIONS = /\.[tj]sx?$/;
|
|
14
|
+
const VIRTUAL_MODULE_NAME = 'virtual:navita';
|
|
15
|
+
const VIRTUAL_CSS_NAME = 'virtual:navita.css';
|
|
16
|
+
async function navitaCompiler(entryPoint, resolver, renderer, onDisposePromise) {
|
|
17
|
+
const compiler = await createCompiler({
|
|
18
|
+
entryPoint,
|
|
19
|
+
renderer,
|
|
20
|
+
async resolver (args) {
|
|
21
|
+
const result = await resolver(args.path, args.importer);
|
|
22
|
+
if (!result) {
|
|
23
|
+
throw new Error(`Failed to resolve ${args.path}`);
|
|
24
|
+
}
|
|
25
|
+
const external = isExternal(result.id);
|
|
26
|
+
if (external) {
|
|
27
|
+
return {
|
|
28
|
+
path: args.path,
|
|
29
|
+
sideEffects: false,
|
|
30
|
+
external: true
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
path: result.id,
|
|
35
|
+
sideEffects: undefined,
|
|
36
|
+
external: false
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
readFile: fs.promises.readFile
|
|
40
|
+
}, onDisposePromise);
|
|
41
|
+
return compiler.rebuild();
|
|
42
|
+
}
|
|
43
|
+
function navita(options) {
|
|
44
|
+
const renderer = createRenderer();
|
|
45
|
+
const importMap$1 = [
|
|
46
|
+
...importMap,
|
|
47
|
+
...options?.importMap || []
|
|
48
|
+
];
|
|
49
|
+
let server;
|
|
50
|
+
let lastCssContent;
|
|
51
|
+
const dependencyGraph = new Map();
|
|
52
|
+
const addToGraph = (importer, source)=>{
|
|
53
|
+
if (!importer) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const dependencies = dependencyGraph.get(importer) || new Set();
|
|
57
|
+
dependencies.add(source);
|
|
58
|
+
dependencyGraph.set(importer, dependencies);
|
|
59
|
+
};
|
|
60
|
+
const findEntryPoints = (id)=>{
|
|
61
|
+
const result = [];
|
|
62
|
+
for (const [importer, dependencies] of dependencyGraph.entries()){
|
|
63
|
+
if (dependencies.has(id)) {
|
|
64
|
+
result.push(...findEntryPoints(importer), importer);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
let dispose;
|
|
70
|
+
const buildEndPromise = new Promise((resolve)=>{
|
|
71
|
+
dispose = resolve;
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
name: "navita",
|
|
75
|
+
enforce: "pre",
|
|
76
|
+
configureServer (_server) {
|
|
77
|
+
lastCssContent = undefined;
|
|
78
|
+
server = _server;
|
|
79
|
+
},
|
|
80
|
+
async buildEnd () {
|
|
81
|
+
await dispose();
|
|
82
|
+
},
|
|
83
|
+
async resolveId (source, importer, options) {
|
|
84
|
+
const resolved = await this.resolve(source, importer, {
|
|
85
|
+
...options,
|
|
86
|
+
skipSelf: true
|
|
87
|
+
});
|
|
88
|
+
if (resolved) {
|
|
89
|
+
const { id } = resolved;
|
|
90
|
+
if (!isExternal(id) && importer) {
|
|
91
|
+
addToGraph(importer, id);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (source === VIRTUAL_MODULE_NAME) {
|
|
95
|
+
return VIRTUAL_CSS_NAME;
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
},
|
|
99
|
+
async load (id) {
|
|
100
|
+
if (id === VIRTUAL_CSS_NAME) {
|
|
101
|
+
return lastCssContent;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
},
|
|
105
|
+
async transform (code, id, options) {
|
|
106
|
+
// Bail as early as we can
|
|
107
|
+
if (!importMap$1.map((x)=>x.source).some((value)=>code.indexOf(value) !== -1)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const entryPoint = findEntryPoints(id).find((entryPoint)=>ENTRY_EXTENSIONS.test(entryPoint)) || id;
|
|
111
|
+
const [result] = await Promise.all([
|
|
112
|
+
transformer(code, {
|
|
113
|
+
filename: id,
|
|
114
|
+
importMap: []
|
|
115
|
+
}),
|
|
116
|
+
navitaCompiler(entryPoint, this.resolve.bind(this), renderer, buildEndPromise)
|
|
117
|
+
]);
|
|
118
|
+
const { result: evaluatedResult } = renderer.getResult(id, options?.ssr);
|
|
119
|
+
const newCssContent = renderer.renderToString();
|
|
120
|
+
if (lastCssContent !== newCssContent) {
|
|
121
|
+
if (server) {
|
|
122
|
+
const { moduleGraph } = server;
|
|
123
|
+
const virtualModule = moduleGraph.getModuleById(VIRTUAL_CSS_NAME);
|
|
124
|
+
if (virtualModule) {
|
|
125
|
+
moduleGraph.invalidateModule(virtualModule);
|
|
126
|
+
virtualModule.lastHMRTimestamp = Date.now();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
lastCssContent = newCssContent;
|
|
130
|
+
}
|
|
131
|
+
return `
|
|
132
|
+
${evaluatedResult}
|
|
133
|
+
${result}
|
|
134
|
+
import "${VIRTUAL_MODULE_NAME}";
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { navita };
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@navita/vite-plugin",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": "./index.mjs",
|
|
8
|
+
"require": "./index.js",
|
|
9
|
+
"types": "./index.d.ts"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@navita/core": "0.0.0",
|
|
14
|
+
"@navita/css": "0.0.0",
|
|
15
|
+
"@navita/swc": "0.0.0",
|
|
16
|
+
"@navita/compiler": "0.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|