Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tmp/index.html cannot found in monorepo #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/plugma/lib/vite-plugins/vite-plugin-copy-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ function removeDirectory(dirPath) {
fs.rmdirSync(dirPath); // Remove the directory itself
}
}
const defaultRenamePipe = (destPath, { file, destination }) => destPath

function copyDirectory(source, destination) {
function copyDirectory(source, destination, renamePipe = defaultRenamePipe) {
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination);
fs.mkdirSync(destination, { recursive: true });
}

const files = fs.readdirSync(source);
Expand All @@ -37,13 +38,7 @@ function copyDirectory(source, destination) {
copyDirectory(sourcePath, destPath);
} else {
// Check if file is named 'index.html'
if (file === 'index.html') {
// Rename 'index.html' to 'ui.html' during copy
fs.copyFileSync(sourcePath, path.join(destination, 'ui.html'));
} else {
// Copy files other than 'index.html'
fs.copyFileSync(sourcePath, destPath);
}
fs.copyFileSync(sourcePath, renamePipe(destPath, { file, destination }));
}
}

Expand All @@ -58,12 +53,19 @@ function copyDirectory(source, destination) {
}


export default function viteCopyDirectoryPlugin({ sourceDir, targetDir }) {
export default function viteCopyDirectoryPlugin({ sourceDir, targetDir, buildStart = false, renamePipe = defaultRenamePipe }) {
return {
name: 'vite-plugin-copy-dir',
apply: 'build',
writeBundle() {
copyDirectory(sourceDir, targetDir);
if (!buildStart) {
copyDirectory(sourceDir, targetDir, renamePipe);
}
},
buildStart() {
if (buildStart) {
copyDirectory(sourceDir, targetDir, renamePipe);
}
},
};
}
8 changes: 7 additions & 1 deletion packages/plugma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
"name": "plugma",
"version": "1.2.7",
"description": "",
"main": "index.js",
"main": "./bin/cli.js",
"type": "module",
"bin": {
"plugma": "./bin/cli.js"
},
"exports": {
"./index.html": "./tmp/index.html",
"./tmp": "./tmp",
"./package.json": "./package.json",
"./lib": "./lib"
},
"scripts": {
"test": "echo \"Error: no test specified\"",
"build-apps": "cd ../apps && npm run build",
Expand Down
5 changes: 3 additions & 2 deletions packages/plugma/scripts/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { build as viteBuild, createServer, mergeConfig, build } from 'vite';
import { nanoid } from 'nanoid';

import { Log } from '../lib/logger.js';
import { getRandomNumber, readJson, createConfigs, getUserFiles, formatTime, cleanManifestFiles } from './utils.js';
import { getRandomNumber, readJson, createConfigs, getUserFiles, require, formatTime, cleanManifestFiles } from './utils.js';
import { task, run, serial } from '../task-runner/taskrunner.js';
import { suppressLogs } from '../lib/suppress-logs.js';
import { logFileUpdates } from '../lib/vite-plugins/vite-plugin-log-file-updates.js';
Expand Down Expand Up @@ -422,7 +422,8 @@ export async function runScript(command, options) {

task('start-websockets-server', async ({ options }) => {
if (options.websockets) {
exec('node node_modules/plugma/lib/start-web-sockets-server.cjs');
const cjsPath = require.resolve('plugma/lib/start-web-sockets-server.cjs')
exec(`node ${cjsPath}`)
log.text(`Preview: ${chalk.cyan('http://localhost:')}${chalk.bold.cyan(options.port)}${chalk.cyan('/')}`)
}
})
Expand Down
18 changes: 17 additions & 1 deletion packages/plugma/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import vitePluginInsertCustomFunctions from '../lib/vite-plugins/vite-plugin-ins
import viteSupressLogs from '../lib/vite-plugins/vite-plugin-surpress-logs.js';
import { cwd } from 'process';
import rewritePostMessageTargetOrigin from '../lib/vite-plugins/vite-plugin-rewrite-postmessage-origin.js';
import { createRequire } from 'node:module'

export const require = createRequire(import.meta.url)

const CURR_DIR = process.cwd();
const __dirname = dirname(fileURLToPath(import.meta.url));
const __filename = fileURLToPath(import.meta.url);

const plugmaRootDir = require.resolve('plugma/package.json').replace('/package.json', '')

export function createFileWithDirectory(filePath, fileName, fileContent, callback) {

function callback(err, result) {
Expand Down Expand Up @@ -73,11 +77,23 @@ export async function readJson(filePath) {

export function createConfigs(options, userFiles) {
// Common plugins and paths for both configurations
const tmpDir = resolve(plugmaRootDir, 'tmp')
const commonVitePlugins = [
viteSingleFile(),
viteCopyDirectoryPlugin({
sourceDir: path.join(options.output, 'node_modules', 'plugma', 'tmp'),
sourceDir: tmpDir,
targetDir: 'node_modules/plugma/tmp',
buildStart: true,
}),
viteCopyDirectoryPlugin({
sourceDir: tmpDir,
targetDir: path.join(options.output),
renamePipe: (destPath, { file, destination }) => {
if (file === 'index.html') {
return path.join(destination, 'ui.html')
}
return destPath
},
}),
];

Expand Down