mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-11 13:56:15 -08:00
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.
This commit is contained in:
1 parent
e2e8e83e63
commit
e8a7e050c4
16 files changed
+233
-287
No files matched your search
+30
-40
@@ -3,7 +3,7 @@ import cpy from 'cpy';
|
||||
import path from 'path';
|
||||
import prompts from 'prompts';
|
||||
import { Parcel } from '@parcel/core';
|
||||
import { getProjects, injectPlaceholders } from './utils.js';
|
||||
import { getProjects } from './utils.js';
|
||||
|
||||
const projects = getProjects();
|
||||
const compiledPath = path.resolve('docs/compiled');
|
||||
@@ -62,10 +62,6 @@ async function build(name, targetName) {
|
||||
};
|
||||
|
||||
const entryFile = path.join(projects[name], 'src/index.js');
|
||||
const restorePlaceholders = injectPlaceholders(entryFile, {
|
||||
__APP_VERSION__: `"${version}"`,
|
||||
__NODE_ENV__: `"production"`,
|
||||
});
|
||||
|
||||
const bundler = new Parcel({
|
||||
entries: entryFile,
|
||||
@@ -88,48 +84,42 @@ async function build(name, targetName) {
|
||||
*/
|
||||
`;
|
||||
|
||||
try {
|
||||
const { bundleGraph, buildTime } = await bundler.run();
|
||||
const bundles = bundleGraph.getBundles();
|
||||
const { bundleGraph, buildTime } = await bundler.run();
|
||||
const bundles = bundleGraph.getBundles();
|
||||
|
||||
const filePath = path.join(projects[name], 'dist/index.js');
|
||||
const newFilePath = path.join(projects[name], `dist/${names[targetName]}.js`);
|
||||
const code = fs.readFileSync(filePath, 'utf-8');
|
||||
const filePath = path.join(projects[name], 'dist/index.js');
|
||||
const newFilePath = path.join(projects[name], `dist/${names[targetName]}.js`);
|
||||
const code = fs.readFileSync(filePath, 'utf-8');
|
||||
|
||||
fs.writeFileSync(filePath, banner + compressString(code));
|
||||
fs.renameSync(filePath, newFilePath);
|
||||
const size = fs.statSync(newFilePath).size / 1024;
|
||||
await cpy(newFilePath, compiledPath);
|
||||
fs.writeFileSync(filePath, banner + compressString(code));
|
||||
fs.renameSync(filePath, newFilePath);
|
||||
const size = fs.statSync(newFilePath).size / 1024;
|
||||
await cpy(newFilePath, compiledPath);
|
||||
|
||||
if (targetName === 'esm') {
|
||||
const jsFile = path.join(projects[name], `dist/${names.esm}.js`);
|
||||
const oldMapName = 'index.js.map';
|
||||
const newMapName = `${names.esm}.js.map`;
|
||||
if (targetName === 'esm') {
|
||||
const jsFile = path.join(projects[name], `dist/${names.esm}.js`);
|
||||
const oldMapName = 'index.js.map';
|
||||
const newMapName = `${names.esm}.js.map`;
|
||||
|
||||
const oldMapPath = path.join(projects[name], 'dist', oldMapName);
|
||||
const newMapPath = path.join(projects[name], 'dist', newMapName);
|
||||
if (fs.existsSync(oldMapPath)) {
|
||||
fs.renameSync(oldMapPath, newMapPath);
|
||||
}
|
||||
|
||||
let jsContent = fs.readFileSync(jsFile, 'utf8');
|
||||
jsContent = jsContent.replace(/\/\/# sourceMappingURL=.*\.js\.map/, `\n//# sourceMappingURL=${newMapName}`);
|
||||
fs.writeFileSync(jsFile, jsContent);
|
||||
await cpy(newMapPath, compiledPath);
|
||||
await cpy(newFilePath, compiledPath);
|
||||
const oldMapPath = path.join(projects[name], 'dist', oldMapName);
|
||||
const newMapPath = path.join(projects[name], 'dist', newMapName);
|
||||
if (fs.existsSync(oldMapPath)) {
|
||||
fs.renameSync(oldMapPath, newMapPath);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`✨ Built@${targetName} ${name}@${version}`,
|
||||
`Bundles@${bundles.length}`,
|
||||
`Time@${buildTime}ms`,
|
||||
`Size@${size.toFixed(2)}kb`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`❌ Failed to build ${name}:`, error);
|
||||
} finally {
|
||||
restorePlaceholders();
|
||||
let jsContent = fs.readFileSync(jsFile, 'utf8');
|
||||
jsContent = jsContent.replace(/\/\/# sourceMappingURL=.*\.js\.map/, `\n//# sourceMappingURL=${newMapName}`);
|
||||
fs.writeFileSync(jsFile, jsContent);
|
||||
await cpy(newMapPath, compiledPath);
|
||||
await cpy(newFilePath, compiledPath);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`✨ Built@${targetName} ${name}@${version}`,
|
||||
`Bundles@${bundles.length}`,
|
||||
`Time@${buildTime}ms`,
|
||||
`Size@${size.toFixed(2)}kb`,
|
||||
);
|
||||
}
|
||||
|
||||
async function runBuild() {
|
||||
|
||||
+45
-63
@@ -1,10 +1,9 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import servor from 'servor';
|
||||
import prompts from 'prompts';
|
||||
import { Parcel } from '@parcel/core';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { getProjects, injectPlaceholders } from './utils.js';
|
||||
import { getProjects } from './utils.js';
|
||||
import openBrowser from 'servor/utils/openBrowser.js';
|
||||
|
||||
const projects = getProjects();
|
||||
@@ -13,79 +12,62 @@ async function develop(name) {
|
||||
const projectPath = projects[name];
|
||||
const uncompiledPath = path.resolve(`docs/uncompiled/${name}`);
|
||||
const entryFile = path.join(projectPath, 'src/index.js');
|
||||
const projectPackageJson = path.join(projectPath, 'package.json');
|
||||
const { version } = JSON.parse(fs.readFileSync(projectPackageJson, 'utf-8'));
|
||||
|
||||
const restore = injectPlaceholders(entryFile, {
|
||||
__APP_VERSION__: `"${version}"`,
|
||||
__NODE_ENV__: `"development"`,
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
restore();
|
||||
process.exit();
|
||||
});
|
||||
|
||||
let isOpenBrowser = false;
|
||||
|
||||
try {
|
||||
const { url } = await servor({
|
||||
root: 'docs',
|
||||
fallback: 'index.html',
|
||||
reload: true,
|
||||
port: 8082,
|
||||
});
|
||||
const { url } = await servor({
|
||||
root: 'docs',
|
||||
fallback: 'index.html',
|
||||
reload: true,
|
||||
port: 8082,
|
||||
});
|
||||
|
||||
process.chdir(projectPath);
|
||||
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'],
|
||||
},
|
||||
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',
|
||||
},
|
||||
env: {
|
||||
NODE_ENV: 'development',
|
||||
},
|
||||
additionalReporters: [
|
||||
{
|
||||
packageName: '@parcel/reporter-cli',
|
||||
resolveFrom: fileURLToPath(import.meta.url),
|
||||
},
|
||||
additionalReporters: [
|
||||
{
|
||||
packageName: '@parcel/reporter-cli',
|
||||
resolveFrom: fileURLToPath(import.meta.url),
|
||||
},
|
||||
],
|
||||
});
|
||||
],
|
||||
});
|
||||
|
||||
bundler.watch((error, event) => {
|
||||
if (error) {
|
||||
console.error(`[${name}] ❌ Build error:`, error);
|
||||
return;
|
||||
}
|
||||
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 === '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);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`❌ Failed to start dev server for ${name}: ${error.message}`);
|
||||
restore();
|
||||
}
|
||||
if (event.type === 'buildFailure') {
|
||||
console.error(`[${name}] ❌ Build failure:`, event.diagnostics);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
(async () => {
|
||||
|
||||
@@ -28,16 +28,3 @@ export function removeDir(dir) {
|
||||
fs.rmdirSync(dir);
|
||||
}
|
||||
}
|
||||
|
||||
export function injectPlaceholders(filePath, placeholders) {
|
||||
const original = fs.readFileSync(filePath, 'utf-8');
|
||||
let modified = original;
|
||||
|
||||
for (const [key, value] of Object.entries(placeholders)) {
|
||||
const pattern = new RegExp(key, 'g');
|
||||
modified = modified.replace(pattern, value);
|
||||
}
|
||||
|
||||
fs.writeFileSync(filePath, modified);
|
||||
return () => fs.writeFileSync(filePath, original); // 构建完还原
|
||||
}
|
||||
Reference in new issue
Block a user