@naiv/react-bundler 0.0.1
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/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +175 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface FileSource {
|
|
2
|
+
path: string;
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Generates and minifies Tailwind CSS for the given source strings using Tailwind v4 API.
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateCSS(source: string, other_sources: FileSource[]): Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Bundles React TSX code into a single minified JS string using esbuild.
|
|
11
|
+
* Supports modularity through a virtual file system plugin.
|
|
12
|
+
*/
|
|
13
|
+
export declare function bundleJS(source: string, other_sources: FileSource[]): Promise<string>;
|
|
14
|
+
export declare function bundleHTML(source: string, other_sources: FileSource[]): Promise<string>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAKA,UAAU,UAAU;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,wBAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CA+B9F;AAgDD;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAoB3F;AAED,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAkB7F"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.generateCSS = generateCSS;
|
|
7
|
+
exports.bundleJS = bundleJS;
|
|
8
|
+
exports.bundleHTML = bundleHTML;
|
|
9
|
+
const tailwindcss_1 = require("tailwindcss");
|
|
10
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
11
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
+
const esbuild_1 = require("esbuild");
|
|
13
|
+
/**
|
|
14
|
+
* Generates and minifies Tailwind CSS for the given source strings using Tailwind v4 API.
|
|
15
|
+
*/
|
|
16
|
+
async function generateCSS(source, other_sources) {
|
|
17
|
+
// Find the Tailwind base CSS file
|
|
18
|
+
const tailwindCssPath = node_path_1.default.resolve(process.cwd(), 'node_modules/tailwindcss/index.css');
|
|
19
|
+
const baseCss = node_fs_1.default.readFileSync(tailwindCssPath, 'utf8');
|
|
20
|
+
// Load the design system with the base CSS
|
|
21
|
+
const compiler = await (0, tailwindcss_1.compile)(baseCss, {
|
|
22
|
+
base: node_path_1.default.dirname(tailwindCssPath),
|
|
23
|
+
loadStylesheet: async (id, base) => {
|
|
24
|
+
const resolvedPath = node_path_1.default.resolve(base, id);
|
|
25
|
+
return {
|
|
26
|
+
content: node_fs_1.default.readFileSync(resolvedPath, 'utf8'),
|
|
27
|
+
base: node_path_1.default.dirname(resolvedPath),
|
|
28
|
+
path: resolvedPath
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
// Extract candidates from all sources
|
|
33
|
+
const allContents = [source, ...other_sources.map(s => s.content)];
|
|
34
|
+
const candidates = allContents
|
|
35
|
+
.flatMap(content => Array.from(content.matchAll(/(?:className|class)\s*=\s*["']([^"']+)["']/g)))
|
|
36
|
+
.flatMap(match => (match[1] || '').split(/\s+/))
|
|
37
|
+
.filter(Boolean);
|
|
38
|
+
// Build the CSS
|
|
39
|
+
const css = compiler.build(candidates);
|
|
40
|
+
// Minify CSS using esbuild
|
|
41
|
+
const minified = await (0, esbuild_1.transform)(css, { loader: 'css', minify: true });
|
|
42
|
+
return minified.code;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* esbuild plugin to resolve virtual files from other_sources.
|
|
46
|
+
*/
|
|
47
|
+
function virtualFsPlugin(other_sources) {
|
|
48
|
+
return {
|
|
49
|
+
name: 'virtual-fs',
|
|
50
|
+
setup(build) {
|
|
51
|
+
// Resolve IDs for virtual files
|
|
52
|
+
build.onResolve({ filter: /^\.\.?\// }, args => {
|
|
53
|
+
// Normalize the path relative to the current directory
|
|
54
|
+
// In this simple example, we assume everything is relative to CWD
|
|
55
|
+
let relativePath = node_path_1.default.join(args.resolveDir, args.path.replace(/^\.\//, ''));
|
|
56
|
+
relativePath = node_path_1.default.relative(process.cwd(), relativePath);
|
|
57
|
+
// Try to match with extensions
|
|
58
|
+
const possiblePaths = [
|
|
59
|
+
relativePath,
|
|
60
|
+
relativePath + '.tsx',
|
|
61
|
+
relativePath + '.ts',
|
|
62
|
+
relativePath + '.jsx',
|
|
63
|
+
relativePath + '.js',
|
|
64
|
+
];
|
|
65
|
+
const match = other_sources.find(s => possiblePaths.includes(s.path));
|
|
66
|
+
if (match) {
|
|
67
|
+
return { path: match.path, namespace: 'virtual' };
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
});
|
|
71
|
+
// Load the content for virtual files
|
|
72
|
+
build.onLoad({ filter: /.*/, namespace: 'virtual' }, args => {
|
|
73
|
+
const match = other_sources.find(s => s.path === args.path);
|
|
74
|
+
if (match) {
|
|
75
|
+
return {
|
|
76
|
+
contents: match.content,
|
|
77
|
+
loader: args.path.endsWith('.tsx') ? 'tsx' : args.path.endsWith('.ts') ? 'ts' : 'js',
|
|
78
|
+
resolveDir: process.cwd(), // crucial for resolving node_modules
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Bundles React TSX code into a single minified JS string using esbuild.
|
|
88
|
+
* Supports modularity through a virtual file system plugin.
|
|
89
|
+
*/
|
|
90
|
+
async function bundleJS(source, other_sources) {
|
|
91
|
+
const result = await (0, esbuild_1.build)({
|
|
92
|
+
stdin: {
|
|
93
|
+
contents: source,
|
|
94
|
+
loader: 'tsx',
|
|
95
|
+
resolveDir: process.cwd(),
|
|
96
|
+
sourcefile: 'index.tsx',
|
|
97
|
+
},
|
|
98
|
+
bundle: true,
|
|
99
|
+
minify: true,
|
|
100
|
+
write: false,
|
|
101
|
+
format: 'iife',
|
|
102
|
+
platform: 'browser',
|
|
103
|
+
plugins: [virtualFsPlugin(other_sources)],
|
|
104
|
+
define: {
|
|
105
|
+
'process.env.NODE_ENV': '"production"',
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
return result.outputFiles ? result.outputFiles[0]?.text || '' : '';
|
|
109
|
+
}
|
|
110
|
+
async function bundleHTML(source, other_sources) {
|
|
111
|
+
const css = await generateCSS(source, other_sources);
|
|
112
|
+
const js = await bundleJS(source, other_sources);
|
|
113
|
+
const html = `<!DOCTYPE html>
|
|
114
|
+
<html lang="en">
|
|
115
|
+
<head>
|
|
116
|
+
<meta charset="UTF-8">
|
|
117
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
118
|
+
<title>Naiv React App</title>
|
|
119
|
+
<style>${css}</style>
|
|
120
|
+
</head>
|
|
121
|
+
<body>
|
|
122
|
+
<div id="root"></div>
|
|
123
|
+
<script>${js}</script>
|
|
124
|
+
</body>
|
|
125
|
+
</html>`;
|
|
126
|
+
return html;
|
|
127
|
+
}
|
|
128
|
+
// async function main() {
|
|
129
|
+
// const logoComponent: FileSource = {
|
|
130
|
+
// path: 'components/Logo.tsx',
|
|
131
|
+
// content: `
|
|
132
|
+
// import React from 'react';
|
|
133
|
+
// export function Logo() {
|
|
134
|
+
// return (
|
|
135
|
+
// <div className="flex items-center space-x-2 bg-white p-3 rounded-full shadow-sm border border-teal-100 mb-6">
|
|
136
|
+
// <div className="w-8 h-8 bg-teal-500 rounded-full flex items-center justify-center text-white font-bold">N</div>
|
|
137
|
+
// <div className="font-bold text-teal-800">Naiv Modularity</div>
|
|
138
|
+
// </div>
|
|
139
|
+
// );
|
|
140
|
+
// }
|
|
141
|
+
// `
|
|
142
|
+
// };
|
|
143
|
+
// const entryPointCode = `
|
|
144
|
+
// import { createRoot } from 'react-dom/client';
|
|
145
|
+
// import React from 'react';
|
|
146
|
+
// import { Logo } from './components/Logo';
|
|
147
|
+
// function App() {
|
|
148
|
+
// return <div className='flex flex-col items-center text-teal-600 justify-center min-h-screen bg-teal-50'>
|
|
149
|
+
// <Logo />
|
|
150
|
+
// <div className="text-5xl font-extrabold mb-6 tracking-tight text-teal-900">Hello Standalone World</div>
|
|
151
|
+
// <div className="p-8 bg-white/80 backdrop-blur-md rounded-2xl shadow-2xl border border-white/20 max-w-md text-center">
|
|
152
|
+
// <p className="text-lg text-teal-700 leading-relaxed">
|
|
153
|
+
// Everything you see here is bundled programmatically into a <span className="font-bold text-teal-600">single HTML file</span>.
|
|
154
|
+
// </p>
|
|
155
|
+
// <div className="mt-6 flex justify-center space-x-3">
|
|
156
|
+
// <span className="px-3 py-1 bg-teal-100 text-teal-700 rounded-md text-sm font-medium">Tailwind v4</span>
|
|
157
|
+
// <span className="px-3 py-1 bg-blue-100 text-blue-700 rounded-md text-sm font-medium">esbuild</span>
|
|
158
|
+
// <span className="px-3 py-1 bg-purple-100 text-purple-700 rounded-md text-sm font-medium">React</span>
|
|
159
|
+
// </div>
|
|
160
|
+
// </div>
|
|
161
|
+
// </div>;
|
|
162
|
+
// }
|
|
163
|
+
// const doc = document.getElementById('root');
|
|
164
|
+
// if (doc) {
|
|
165
|
+
// const root = createRoot(doc);
|
|
166
|
+
// root.render(<App />);
|
|
167
|
+
// }
|
|
168
|
+
// `;
|
|
169
|
+
// const html = await bundleHTML(entryPointCode, [logoComponent]);
|
|
170
|
+
// const outputPath = path.resolve(process.cwd(), 'index.html');
|
|
171
|
+
// fs.writeFileSync(outputPath, html);
|
|
172
|
+
// console.log(`Successfully generated standalone app: ${outputPath}`);
|
|
173
|
+
// }
|
|
174
|
+
// main().catch(console.error).finally(() => process.exit(0));
|
|
175
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;AAaA,kCA+BC;AAoDD,4BAoBC;AAED,gCAkBC;AAxID,6CAAsC;AACtC,sDAAyB;AACzB,0DAA6B;AAC7B,qCAAmD;AAOnD;;GAEG;AACI,KAAK,UAAU,WAAW,CAAC,MAAc,EAAE,aAA2B;IAC3E,kCAAkC;IAClC,MAAM,eAAe,GAAG,mBAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,oCAAoC,CAAC,CAAC;IAC1F,MAAM,OAAO,GAAG,iBAAE,CAAC,YAAY,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;IAEzD,2CAA2C;IAC3C,MAAM,QAAQ,GAAG,MAAM,IAAA,qBAAO,EAAC,OAAO,EAAE;QACtC,IAAI,EAAE,mBAAI,CAAC,OAAO,CAAC,eAAe,CAAC;QACnC,cAAc,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE;YACjC,MAAM,YAAY,GAAG,mBAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC5C,OAAO;gBACL,OAAO,EAAE,iBAAE,CAAC,YAAY,CAAC,YAAY,EAAE,MAAM,CAAC;gBAC9C,IAAI,EAAE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CAA4C;AAE5C,mBAAmB;AACnB,6GAA6G;AAC7G,eAAe;AACf,8GAA8G;AAC9G,4HAA4H;AAC5H,8DAA8D;AAC9D,wIAAwI;AACxI,aAAa;AACb,6DAA6D;AAC7D,kHAAkH;AAClH,8GAA8G;AAC9G,gHAAgH;AAChH,eAAe;AACf,aAAa;AACb,YAAY;AACZ,IAAI;AAEJ,+CAA+C;AAC/C,aAAa;AACb,kCAAkC;AAClC,0BAA0B;AAC1B,IAAI;AACJ,KAAK;AAEL,oEAAoE;AAEpE,kEAAkE;AAClE,wCAAwC;AACxC,yEAAyE;AACzE,IAAI;AAEJ,8DAA8D"}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@naiv/react-bundler",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Bundle react files into single html output",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "rm -rf dist && tsc"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"package.json"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/node": "^25.3.0",
|
|
21
|
+
"esbuild": "^0.27.3",
|
|
22
|
+
"typescript": "^5.9.3",
|
|
23
|
+
"autoprefixer": "^10.4.24",
|
|
24
|
+
"postcss": "^8.5.6",
|
|
25
|
+
"react": "^19.2.4",
|
|
26
|
+
"react-dom": "^19.2.4",
|
|
27
|
+
"tailwindcss": "^4.2.0"
|
|
28
|
+
}
|
|
29
|
+
}
|