rails-vite-plugin 0.1.0 → 0.1.2
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.js +27 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,7 +10,8 @@ export const refreshPaths = [
|
|
|
10
10
|
let exitHandlersBound = false;
|
|
11
11
|
export default function rails(options = {}) {
|
|
12
12
|
const sourceDir = options.sourceDir ?? 'app/javascript';
|
|
13
|
-
const
|
|
13
|
+
const entrypointsDir = options.input === undefined ? detectEntrypointsDir(sourceDir) : null;
|
|
14
|
+
const input = options.input ?? (entrypointsDir ? discoverEntrypointInputs(sourceDir, entrypointsDir) : detectEntrypoint(sourceDir));
|
|
14
15
|
const publicDirectory = options.publicDirectory ?? 'public';
|
|
15
16
|
const buildDirectory = options.buildDirectory ?? 'vite';
|
|
16
17
|
const devMetaPath = options.devMetaFile ?? path.join('tmp', 'rails-vite.json');
|
|
@@ -67,7 +68,7 @@ export default function rails(options = {}) {
|
|
|
67
68
|
if (resolvedConfig.build.ssr)
|
|
68
69
|
return;
|
|
69
70
|
const outDir = resolvedConfig.build.outDir;
|
|
70
|
-
fs.writeFileSync(path.join(outDir, 'rails-vite.json'), JSON.stringify({ sourceDir }));
|
|
71
|
+
fs.writeFileSync(path.join(outDir, 'rails-vite.json'), JSON.stringify(entrypointsDir ? { sourceDir, entrypointsDir } : { sourceDir }));
|
|
71
72
|
},
|
|
72
73
|
configureServer(server) {
|
|
73
74
|
server.httpServer?.once('listening', () => {
|
|
@@ -76,6 +77,8 @@ export default function rails(options = {}) {
|
|
|
76
77
|
const devServerUrl = resolveDevServerUrl(address, resolvedConfig);
|
|
77
78
|
resolvedConfig.server.origin = devServerUrl;
|
|
78
79
|
const meta = { url: devServerUrl, sourceDir };
|
|
80
|
+
if (entrypointsDir)
|
|
81
|
+
meta.entrypointsDir = entrypointsDir;
|
|
79
82
|
if (reactRefresh)
|
|
80
83
|
meta.reactRefresh = true;
|
|
81
84
|
fs.writeFileSync(devMetaPath, JSON.stringify(meta));
|
|
@@ -134,9 +137,17 @@ function prefixWithSourceDir(entry, sourceDir) {
|
|
|
134
137
|
}
|
|
135
138
|
return `${sourceDir}/${entry}`;
|
|
136
139
|
}
|
|
137
|
-
const
|
|
140
|
+
const entrypointExtensions = /\.(mjs|js|mts|ts|jsx|tsx|css|scss|sass|less|styl|pcss)$/;
|
|
141
|
+
function detectEntrypointsDir(sourceDir) {
|
|
142
|
+
const entrypointsDir = path.join(sourceDir, 'entrypoints');
|
|
143
|
+
return fs.existsSync(entrypointsDir) ? 'entrypoints' : null;
|
|
144
|
+
}
|
|
145
|
+
function discoverEntrypointInputs(sourceDir, entrypointsDir) {
|
|
146
|
+
const absDir = path.join(sourceDir, entrypointsDir);
|
|
147
|
+
return discoverEntrypoints(absDir).map((entry) => `${entrypointsDir}/${entry}`);
|
|
148
|
+
}
|
|
138
149
|
function detectEntrypoint(sourceDir) {
|
|
139
|
-
for (const ext of
|
|
150
|
+
for (const ext of ['.js', '.mjs', '.ts', '.mts', '.jsx', '.tsx']) {
|
|
140
151
|
const candidate = path.join(sourceDir, `application${ext}`);
|
|
141
152
|
if (fs.existsSync(candidate)) {
|
|
142
153
|
return `application${ext}`;
|
|
@@ -144,6 +155,18 @@ function detectEntrypoint(sourceDir) {
|
|
|
144
155
|
}
|
|
145
156
|
return 'application.js';
|
|
146
157
|
}
|
|
158
|
+
function discoverEntrypoints(dir, base = dir) {
|
|
159
|
+
const entries = [];
|
|
160
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
161
|
+
if (entry.isDirectory()) {
|
|
162
|
+
entries.push(...discoverEntrypoints(path.join(dir, entry.name), base));
|
|
163
|
+
}
|
|
164
|
+
else if (entrypointExtensions.test(entry.name)) {
|
|
165
|
+
entries.push(path.relative(base, path.join(dir, entry.name)));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return entries;
|
|
169
|
+
}
|
|
147
170
|
function resolveRefreshPaths(refresh) {
|
|
148
171
|
if (refresh === false)
|
|
149
172
|
return [];
|