Files
ArtPlayer/scripts/dev.js
T
Harvey Zack e8a7e050c4 refactor: update version handling and remove placeholder injection
- Simplified version display in context menu and template by directly importing version from package.json.
- Removed static version and environment methods from Artplayer class, replacing them with dynamic imports.
- Eliminated placeholder injection logic from build and dev scripts, streamlining the build process.
- Cleaned up build and dev scripts for better readability and maintainability.
2025-06-20 11:04:29 +08:00

93 lines
2.5 KiB
JavaScript

import path from 'path';
import servor from 'servor';
import prompts from 'prompts';
import { Parcel } from '@parcel/core';
import { fileURLToPath } from 'url';
import { getProjects } from './utils.js';
import openBrowser from 'servor/utils/openBrowser.js';
const projects = getProjects();
async function develop(name) {
const projectPath = projects[name];
const uncompiledPath = path.resolve(`docs/uncompiled/${name}`);
const entryFile = path.join(projectPath, 'src/index.js');
let isOpenBrowser = false;
const { url } = await servor({
root: 'docs',
fallback: 'index.html',
reload: true,
port: 8082,
});
process.chdir(projectPath);
const bundler = new Parcel({
entries: entryFile,
defaultConfig: '@parcel/config-default',
mode: 'development',
targets: {
main: {
distDir: uncompiledPath,
outputFormat: 'global',
isLibrary: true,
engines: {
browsers: ['last 1 Chrome version'],
},
},
},
env: {
NODE_ENV: 'development',
},
additionalReporters: [
{
packageName: '@parcel/reporter-cli',
resolveFrom: fileURLToPath(import.meta.url),
},
],
});
bundler.watch((error, event) => {
if (error) {
console.error(`[${name}] ❌ Build error:`, error);
return;
}
if (event.type === 'buildSuccess') {
const bundles = event.bundleGraph.getBundles();
console.log(`[${name}] ✅ Built ${bundles.length} bundles in ${event.buildTime}ms`);
if (!isOpenBrowser) {
isOpenBrowser = true;
openBrowser(url);
}
}
if (event.type === 'buildFailure') {
console.error(`[${name}] ❌ Build failure:`, event.diagnostics);
}
});
}
(async () => {
try {
const response = await prompts({
type: 'select',
name: 'value',
message: 'Which project do you want to develop?',
choices: Object.keys(projects).map((name) => ({
title: name,
value: name,
})),
initial: 0,
});
if (response.value) {
await develop(response.value);
}
} catch (error) {
console.error(`❌ Prompt error: ${error.message}`);
}
})();