Skip to content

Commit

Permalink
Build: add debug and watch mode for API builds (#9783)
Browse files Browse the repository at this point in the history
  • Loading branch information
bershanskiy committed Sep 8, 2022
1 parent d73cd98 commit f98b780
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"debug": "node tasks/build.js --debug",
"debug:watch": "node tasks/build.js --debug --watch",
"lint": "eslint --ignore-pattern '!.eslintplugin.js' -- 'src/**/*.ts' 'src/**/*.tsx' 'tasks/**/*.js' 'tests/**/*.ts' 'tests/[!coverage]**/*.js' 'index.d.ts' '.eslintplugin.js'",
"lint:bundle": "(node ./tasks/check-exists.js ./build/debug/chrome || node tasks/build.js --debug --api) && eslint -- 'build/debug/chrome/**/*.js' 'darkreader.js'",
"lint:bundle": "(node ./tasks/check-exists.js ./build/debug/chrome || node tasks/build.js --debug --api --chrome) && eslint -- 'build/debug/chrome/**/*.js' 'darkreader.js'",
"prepublishOnly": "npm test && npm run api",
"release": "npm test && npm run lint && node tasks/build.js --release",
"test": "npm run test:unit",
Expand Down
16 changes: 10 additions & 6 deletions tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ async function build({platforms, debug, watch, log: logging, test}) {
}
}

async function api() {
async function api(debug, watch) {
log.ok('API');
try {
await runTasks([bundleAPI], {platforms: {}, debug: false, watch: false, log: false, test: false});
await runTasks([bundleAPI], {platforms: {}, debug, watch, log: false, test: false});
if (watch) {
bundleAPI.watch();
log.ok('Watching...');
}
log.ok('MISSION PASSED! RESPECT +');
} catch (err) {
console.log(err);
Expand Down Expand Up @@ -89,7 +93,7 @@ async function run() {
const validArgs = ['--api', '--chrome', '--chrome-mv3', '--firefox', '--thunderbird', '--release', '--debug', '--watch', '--log-info', '--log-warn', '--test'];
args.filter((arg) => !validArgs.includes(arg)).forEach((arg) => log.warn(`Unknown argument ${arg}`));

const allPlatforms = !(args.includes('--chrome') || args.includes('--chrome-mv3') || args.includes('--firefox') || args.includes('--thunderbird'));
const allPlatforms = !(args.includes('--api') || args.includes('--chrome') || args.includes('--chrome-mv3') || args.includes('--firefox') || args.includes('--thunderbird'));
const platforms = {
[PLATFORM.CHROME]: allPlatforms || args.includes('--chrome'),
[PLATFORM.CHROME_MV3]: allPlatforms || args.includes('--chrome-mv3'),
Expand All @@ -103,14 +107,14 @@ async function run() {
const watch = args.includes('--watch');
const logInfo = watch && args.includes('--log-info');
const logWarn = watch && args.includes('--log-warn');
if (release) {
if (release && Object.values(platforms).some(Boolean)) {
await build({platforms, debug: false, watch: false, log: null, test: false});
}
if (debug) {
if (debug && Object.values(platforms).some(Boolean)) {
await build({platforms, debug, watch, log: logWarn ? 'warn' : (logInfo ? 'info' : null), test: args.includes('--test')});
}
if (args.includes('--api')) {
await api();
await api(debug, watch);
}
}

Expand Down
29 changes: 25 additions & 4 deletions tasks/bundle-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ async function getVersion() {
return p.version;
}

async function bundleAPI({debug}) {
let watchFiles = [];

async function bundleAPI({debug, watch}) {
const src = rootPath('src/api/index.ts');
const dest = 'darkreader.js';
const bundle = await rollup.rollup({
Expand All @@ -29,8 +31,11 @@ async function bundleAPI({debug}) {
rootDir,
typescript,
tsconfig: rootPath('src/api/tsconfig.json'),
removeComments: true,
noEmitOnError: true,
noImplicitAny: debug ? false : true,
removeComments: debug ? false : true,
sourceMap: debug ? true : false,
inlineSources: debug ? true : false,
noEmitOnError: watch ? false : true,
cacheDir: debug ? `${fs.realpathSync(os.tmpdir())}/darkreader_api_typescript_cache` : undefined,
}),
rollupPluginReplace({
Expand All @@ -44,19 +49,35 @@ async function bundleAPI({debug}) {
}),
].filter((x) => x)
});
watchFiles = bundle.watchFiles;
await bundle.write({
banner: `/**\n * Dark Reader v${await getVersion()}\n * https://darkreader.org/\n */\n`,
file: dest,
strict: true,
format: 'umd',
name: 'DarkReader',
sourcemap: false,
sourcemap: debug ? 'inline' : false,
});
}

const bundleAPITask = createTask(
'bundle-api',
bundleAPI,
).addWatcher(
() => {
return watchFiles;
},
async (changedFiles, watcher) => {
const oldWatchFiles = watchFiles;
await bundleAPI({debug: true, watch: true});

watcher.unwatch(
oldWatchFiles.filter((oldFile) => !watchFiles.includes(oldFile))
);
watcher.add(
watchFiles.filter((newFile) => oldWatchFiles.includes(newFile))
);
},
);

export default bundleAPITask;

0 comments on commit f98b780

Please sign in to comment.