Skip to content

Commit

Permalink
refactor: ♻️ 将 eh 的代码拆分开来
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jul 7, 2024
1 parent d505117 commit 1e4b748
Show file tree
Hide file tree
Showing 6 changed files with 727 additions and 676 deletions.
26 changes: 17 additions & 9 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import { resolve, dirname } from 'node:path';
import { resolve, dirname, basename, extname } from 'node:path';
import { fileURLToPath } from 'node:url';

import shell from 'shelljs';
Expand Down Expand Up @@ -63,16 +63,21 @@ const { meta, createMetaHeader } = getMetaData(isDevMode);
const generateScopedName = '[local]';

export const buildOptions = (
fileName: string,
path: string,
watchFiles?: string[],
fn?: (options: RollupOptions) => RollupOptions,
): RollupOptions => {
const isUserScript = ['dev', 'index'].includes(fileName);
const isUserScript = ['dev', 'index'].includes(path);

const dir = isUserScript ? 'dist' : 'dist/cache';
const fileName = path.endsWith('index.tsx')
? path.split('/')[1]
: basename(path, extname(path));

const options: RollupOptions = {
treeshake: true,
external: [...Object.keys(meta.resource ?? {}), 'main', 'dmzjDecrypt'],
input: resolve(__dirname, 'src', fileName),
input: resolve(__dirname, 'src', path),
// 忽略使用 eval 的警告
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning);
Expand Down Expand Up @@ -116,8 +121,7 @@ export const buildOptions = (
watchFiles && isDevMode && watchExternal({ entries: watchFiles }),
],
output: {
// dev 和 index 外的文件都放到 cache 文件夹下
dir: resolve(__dirname, isUserScript ? 'dist' : 'dist/cache'),
file: `${dir}/${fileName}.js`,
format: 'cjs',
strict: false,
generatedCode: 'es2015',
Expand All @@ -129,7 +133,7 @@ export const buildOptions = (
renderChunk(rawCode) {
let code = rawCode;

switch (fileName) {
switch (path) {
case 'index':
updateReadme();
if (isDevMode)
Expand Down Expand Up @@ -226,8 +230,12 @@ const optionList: RollupOptions[] = [
buildOptions('main'),

...fs
.readdirSync('src/site')
.map((fileName) => buildOptions(`site/${fileName}`)),
.readdirSync('src/site', { withFileTypes: true })
.map((item) =>
item.isFile()
? buildOptions(`site/${item.name}`)
: buildOptions(`site/${item.name}/index.tsx`),
),

buildOptions('helper/import', ['dist/cache/main.js']),

Expand Down
Loading

0 comments on commit 1e4b748

Please sign in to comment.