@navita/vite-plugin 0.0.1 → 0.0.4
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/LICENSE.md +21 -0
- package/index.d.ts +2 -1
- package/index.js +37 -81
- package/index.mjs +37 -84
- package/package.json +3 -5
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Alexander Liljengård
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ImportMap } from '@navita/
|
|
1
|
+
import { ImportMap, EngineOptions } from '@navita/core/createRenderer';
|
|
2
2
|
import { Plugin } from 'vite';
|
|
3
3
|
|
|
4
4
|
interface Options {
|
|
5
5
|
importMap?: ImportMap;
|
|
6
|
+
engineOptions?: EngineOptions;
|
|
6
7
|
}
|
|
7
8
|
declare function navita(options?: Options): Plugin;
|
|
8
9
|
|
package/index.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var fs = require('node:fs');
|
|
4
|
-
var path = require('node:path');
|
|
5
|
-
var compiler = require('@navita/compiler');
|
|
6
4
|
var createRenderer = require('@navita/core/createRenderer');
|
|
7
5
|
var css = require('@navita/css');
|
|
8
|
-
var swc = require('@navita/swc');
|
|
9
6
|
|
|
10
7
|
function _interopNamespaceDefault(e) {
|
|
11
8
|
var n = Object.create(null);
|
|
@@ -25,69 +22,17 @@ function _interopNamespaceDefault(e) {
|
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
28
|
-
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
29
25
|
|
|
30
|
-
const isExternal = (id)=>id.includes('node_modules') || id.startsWith(path__namespace.resolve(__dirname, '../../'));
|
|
31
|
-
const ENTRY_EXTENSIONS = /\.[tj]sx?$/;
|
|
32
26
|
const VIRTUAL_MODULE_NAME = 'virtual:navita';
|
|
33
27
|
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
28
|
function navita(options) {
|
|
62
|
-
|
|
29
|
+
let renderer;
|
|
63
30
|
const importMap = [
|
|
64
31
|
...css.importMap,
|
|
65
32
|
...options?.importMap || []
|
|
66
33
|
];
|
|
67
34
|
let server;
|
|
68
35
|
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
36
|
return {
|
|
92
37
|
name: "navita",
|
|
93
38
|
enforce: "pre",
|
|
@@ -95,20 +40,25 @@ function navita(options) {
|
|
|
95
40
|
lastCssContent = undefined;
|
|
96
41
|
server = _server;
|
|
97
42
|
},
|
|
98
|
-
async
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
43
|
+
async buildStart () {
|
|
44
|
+
const defaultEngineOptions = {
|
|
45
|
+
enableSourceMaps: !!server,
|
|
46
|
+
enableDebugIdentifiers: !!server,
|
|
47
|
+
...options?.engineOptions || {}
|
|
48
|
+
};
|
|
49
|
+
renderer = createRenderer.createRenderer({
|
|
50
|
+
importMap,
|
|
51
|
+
engineOptions: defaultEngineOptions,
|
|
52
|
+
resolver: async (filepath, request)=>{
|
|
53
|
+
const resolved = await this.resolve(request, filepath);
|
|
54
|
+
return resolved?.id || null;
|
|
55
|
+
},
|
|
56
|
+
readFile: (path)=>{
|
|
57
|
+
return fs__namespace.promises.readFile(path, 'utf-8');
|
|
110
58
|
}
|
|
111
|
-
}
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
async resolveId (source) {
|
|
112
62
|
if (source === VIRTUAL_MODULE_NAME) {
|
|
113
63
|
return VIRTUAL_CSS_NAME;
|
|
114
64
|
}
|
|
@@ -120,21 +70,16 @@ function navita(options) {
|
|
|
120
70
|
}
|
|
121
71
|
return null;
|
|
122
72
|
},
|
|
123
|
-
async transform (code, id
|
|
73
|
+
async transform (code, id) {
|
|
124
74
|
// Bail as early as we can
|
|
125
75
|
if (!importMap.map((x)=>x.source).some((value)=>code.indexOf(value) !== -1)) {
|
|
126
76
|
return null;
|
|
127
77
|
}
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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();
|
|
78
|
+
const { result } = await renderer.transformAndProcess({
|
|
79
|
+
content: code,
|
|
80
|
+
filePath: id
|
|
81
|
+
});
|
|
82
|
+
const newCssContent = renderer.engine.renderCssToString();
|
|
138
83
|
if (lastCssContent !== newCssContent) {
|
|
139
84
|
if (server) {
|
|
140
85
|
const { moduleGraph } = server;
|
|
@@ -147,10 +92,21 @@ function navita(options) {
|
|
|
147
92
|
lastCssContent = newCssContent;
|
|
148
93
|
}
|
|
149
94
|
return `
|
|
150
|
-
${evaluatedResult}
|
|
151
95
|
${result}
|
|
152
96
|
import "${VIRTUAL_MODULE_NAME}";
|
|
153
97
|
`;
|
|
98
|
+
},
|
|
99
|
+
renderChunk (_, chunk) {
|
|
100
|
+
for (const id of Object.keys(chunk.modules)){
|
|
101
|
+
if (id.startsWith(VIRTUAL_CSS_NAME)) {
|
|
102
|
+
delete chunk.modules[id];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
chunk.viteMetadata.importedCss.add(this.getFileName(this.emitFile({
|
|
106
|
+
name: "navita.css",
|
|
107
|
+
type: "asset",
|
|
108
|
+
source: renderer.engine.renderCssToString()
|
|
109
|
+
})));
|
|
154
110
|
}
|
|
155
111
|
};
|
|
156
112
|
}
|
package/index.mjs
CHANGED
|
@@ -1,75 +1,17 @@
|
|
|
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
1
|
import * as fs from 'node:fs';
|
|
6
|
-
import * as path from 'node:path';
|
|
7
|
-
import { createCompiler } from '@navita/compiler';
|
|
8
2
|
import { createRenderer } from '@navita/core/createRenderer';
|
|
9
3
|
import { importMap } from '@navita/css';
|
|
10
|
-
import { transformer } from '@navita/swc';
|
|
11
4
|
|
|
12
|
-
const isExternal = (id)=>id.includes('node_modules') || id.startsWith(path.resolve(__dirname, '../../'));
|
|
13
|
-
const ENTRY_EXTENSIONS = /\.[tj]sx?$/;
|
|
14
5
|
const VIRTUAL_MODULE_NAME = 'virtual:navita';
|
|
15
6
|
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
7
|
function navita(options) {
|
|
44
|
-
|
|
8
|
+
let renderer;
|
|
45
9
|
const importMap$1 = [
|
|
46
10
|
...importMap,
|
|
47
11
|
...options?.importMap || []
|
|
48
12
|
];
|
|
49
13
|
let server;
|
|
50
14
|
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
15
|
return {
|
|
74
16
|
name: "navita",
|
|
75
17
|
enforce: "pre",
|
|
@@ -77,20 +19,25 @@ function navita(options) {
|
|
|
77
19
|
lastCssContent = undefined;
|
|
78
20
|
server = _server;
|
|
79
21
|
},
|
|
80
|
-
async
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
22
|
+
async buildStart () {
|
|
23
|
+
const defaultEngineOptions = {
|
|
24
|
+
enableSourceMaps: !!server,
|
|
25
|
+
enableDebugIdentifiers: !!server,
|
|
26
|
+
...options?.engineOptions || {}
|
|
27
|
+
};
|
|
28
|
+
renderer = createRenderer({
|
|
29
|
+
importMap: importMap$1,
|
|
30
|
+
engineOptions: defaultEngineOptions,
|
|
31
|
+
resolver: async (filepath, request)=>{
|
|
32
|
+
const resolved = await this.resolve(request, filepath);
|
|
33
|
+
return resolved?.id || null;
|
|
34
|
+
},
|
|
35
|
+
readFile: (path)=>{
|
|
36
|
+
return fs.promises.readFile(path, 'utf-8');
|
|
92
37
|
}
|
|
93
|
-
}
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
async resolveId (source) {
|
|
94
41
|
if (source === VIRTUAL_MODULE_NAME) {
|
|
95
42
|
return VIRTUAL_CSS_NAME;
|
|
96
43
|
}
|
|
@@ -102,21 +49,16 @@ function navita(options) {
|
|
|
102
49
|
}
|
|
103
50
|
return null;
|
|
104
51
|
},
|
|
105
|
-
async transform (code, id
|
|
52
|
+
async transform (code, id) {
|
|
106
53
|
// Bail as early as we can
|
|
107
54
|
if (!importMap$1.map((x)=>x.source).some((value)=>code.indexOf(value) !== -1)) {
|
|
108
55
|
return null;
|
|
109
56
|
}
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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();
|
|
57
|
+
const { result } = await renderer.transformAndProcess({
|
|
58
|
+
content: code,
|
|
59
|
+
filePath: id
|
|
60
|
+
});
|
|
61
|
+
const newCssContent = renderer.engine.renderCssToString();
|
|
120
62
|
if (lastCssContent !== newCssContent) {
|
|
121
63
|
if (server) {
|
|
122
64
|
const { moduleGraph } = server;
|
|
@@ -129,10 +71,21 @@ function navita(options) {
|
|
|
129
71
|
lastCssContent = newCssContent;
|
|
130
72
|
}
|
|
131
73
|
return `
|
|
132
|
-
${evaluatedResult}
|
|
133
74
|
${result}
|
|
134
75
|
import "${VIRTUAL_MODULE_NAME}";
|
|
135
76
|
`;
|
|
77
|
+
},
|
|
78
|
+
renderChunk (_, chunk) {
|
|
79
|
+
for (const id of Object.keys(chunk.modules)){
|
|
80
|
+
if (id.startsWith(VIRTUAL_CSS_NAME)) {
|
|
81
|
+
delete chunk.modules[id];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
chunk.viteMetadata.importedCss.add(this.getFileName(this.emitFile({
|
|
85
|
+
name: "navita.css",
|
|
86
|
+
type: "asset",
|
|
87
|
+
source: renderer.engine.renderCssToString()
|
|
88
|
+
})));
|
|
136
89
|
}
|
|
137
90
|
};
|
|
138
91
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navita/vite-plugin",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"private": false,
|
|
6
6
|
"sideEffects": false,
|
|
@@ -12,9 +12,7 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@navita/core": "0.0.
|
|
16
|
-
"@navita/
|
|
17
|
-
"@navita/css": "0.0.0",
|
|
18
|
-
"@navita/compiler": "0.0.1"
|
|
15
|
+
"@navita/core": "0.0.4",
|
|
16
|
+
"@navita/css": "0.0.4"
|
|
19
17
|
}
|
|
20
18
|
}
|