Skip to content

Commit

Permalink
chore: update build script to allow exclusion of module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pilar6195 committed Jul 13, 2024
1 parent 0ffbb5a commit d7a9bd6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
77 changes: 66 additions & 11 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import fsAsync from 'node:fs/promises';
import path from 'node:path';
import chokidar from 'chokidar';
import archiver from 'archiver';
import { compile as compileSass } from 'sass';
Expand All @@ -11,7 +12,7 @@ const watchFlag = process.argv.includes('--watch');
const serveFlag = process.argv.includes('--serve');
const includeSourceMaps = process.argv.includes('--include-sourcemap');

const header = `
const header = (env: 'extension' | 'userscript' = 'userscript') => `
// ==UserScript==
// @name AniList Extras (Unofficial)
// @namespace https://github.com/pilar6195
Expand All @@ -30,9 +31,19 @@ const header = `
// ==/UserScript==
const ALEXTRAS_VERSION = '${packageJson.version}';
const ALEXTRAS_DEV = ${watchFlag};
const ALEXTRAS_ENV = '${env}';
`;

async function build() {
async function bundle(env: 'extension' | 'userscript' = 'userscript') {
const modulesToExclude: string[] = [];

if (env === 'extension') {
// This module will not be included in the browser extension build.
// It requires an external YouTube API script to work which is not
// allowed in MV3 extensions. Will look into a workaround for this.
modulesToExclude.push('anilist/setYTDefaultVolume.ts');
}

const result = await Bun.build({
entrypoints: ['./src/anilist-extras.user.ts'],
target: 'browser',
Expand All @@ -58,12 +69,37 @@ async function build() {
});
},
},
{
name: 'remove-polyfill',
setup(build) {
// Don't include the polyfill in the userscript build.
if (env === 'extension') return;
build.onLoad({ filter: /Polyfill\.ts$/ }, () => ({
contents: '',
loader: 'ts',
}));
},
},
{
name: 'exclude-modules',
setup(build) {
if (!modulesToExclude.length) return;
build.onLoad({
filter: new RegExp(modulesToExclude.map(module => {
const modulePath = path.normalize(`modules/${module}`).replaceAll(/[.\\]/g, '\\$&');
return `(${modulePath}$)`;
}).join('|')),
}, () => ({
contents: '',
loader: 'ts',
}));
},
},
],
});

if (!result.success) {
console.error('Build failed with errors:');
console.log('');
console.error(`\n[${env}] Build failed with errors:\n`);
for (const log of result.logs) {
console.error(log);
}
Expand All @@ -78,22 +114,36 @@ async function build() {
/* Prepend userscript header to output */

const content = await result.outputs[0].text();
let output = header + content;
let output = header(env) + content;

/* Fix sourcemap offset */
// We have to do this if we want the sourcemap to work correctly due to the header we added.
if (result.outputs[0].sourcemap) {
const sourceMap = await result.outputs[0].sourcemap?.json();
const offset = header.split('\n').length - 1;
const offset = header(env).split('\n').length - 1;
const updatedSourceMap = offsetLines(sourceMap, offset);
const b64encoded = Buffer.from(JSON.stringify(updatedSourceMap)).toString('base64');
output += `//# sourceMappingURL=data:application/json;base64,${b64encoded}`;
}

return output;
}

async function buildUserscript() {
const output = await bundle('userscript');

if (!output) return;

/* Write userscript to dist directory */

await Bun.write('./dist/anilist-extras.user.js', output);
console.log(`[${new Date().toISOString()}]`, 'Userscript built. Output written to "./dist/anilist-extras.user.js"');
}

async function buildExtension() {
const output = await bundle('extension');

if (!output) return;

/* Write extension to dist directory */

Expand Down Expand Up @@ -131,16 +181,21 @@ async function build() {
}
}

async function buildAll() {
await buildUserscript();
await buildExtension();
}

// Watch for changes and rebuild
if (watchFlag) {
chokidar.watch('src/**/*', {
ignoreInitial: true,
})
.on('add', () => void build())
.on('change', () => void build())
.on('unlink', () => void build());
.on('add', () => void buildAll())
.on('change', () => void buildAll())
.on('unlink', () => void buildAll());

await build();
await buildAll();

console.log('Watching for changes...');

Expand All @@ -163,5 +218,5 @@ if (watchFlag) {
}
// Build once and exit
} else {
void build();
void buildAll();
}
2 changes: 1 addition & 1 deletion src/utils/Polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable prefer-promise-reject-errors */
if (window.GM === undefined) {
if (ALEXTRAS_ENV === 'extension') {
// @ts-expect-error - Basic GM API Polyfill
// No where near 1:1, but should be enough.
window.GM = {
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const ALEXTRAS_VERSION: string;
declare const ALEXTRAS_DEV: boolean;
declare const ALEXTRAS_ENV: 'extension' | 'userscript';

declare module '*.css' {
const content: string;
Expand Down

0 comments on commit d7a9bd6

Please sign in to comment.