2024-02-05 09:26:37 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2024 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2023-10-12 13:04:29 +00:00
|
|
|
import {mkdir, readFile, readdir, writeFile} from 'fs/promises';
|
2024-02-22 12:42:59 +00:00
|
|
|
import Module from 'node:module';
|
2024-02-22 14:30:58 +00:00
|
|
|
import path from 'path';
|
|
|
|
import posixPath from 'path/posix';
|
2023-10-11 15:54:26 +00:00
|
|
|
|
|
|
|
import esbuild from 'esbuild';
|
|
|
|
import {execa} from 'execa';
|
|
|
|
import {task} from 'hereby';
|
|
|
|
|
2024-02-22 12:42:59 +00:00
|
|
|
const require = Module.createRequire(import.meta.url);
|
|
|
|
|
2023-10-11 15:54:26 +00:00
|
|
|
export const generateVersionTask = task({
|
|
|
|
name: 'generate:version',
|
|
|
|
run: async () => {
|
|
|
|
const {version} = JSON.parse(await readFile('package.json', 'utf8'));
|
|
|
|
await mkdir('src/generated', {recursive: true});
|
|
|
|
await writeFile(
|
|
|
|
'src/generated/version.ts',
|
|
|
|
(await readFile('src/templates/version.ts.tmpl', 'utf8')).replace(
|
|
|
|
'PACKAGE_VERSION',
|
|
|
|
version
|
|
|
|
)
|
|
|
|
);
|
|
|
|
if (process.env['PUBLISH']) {
|
|
|
|
await writeFile(
|
|
|
|
'../../versions.js',
|
|
|
|
(
|
|
|
|
await readFile('../../versions.js', {
|
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
).replace("'NEXT'", `'v${version}'`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const generateInjectedTask = task({
|
|
|
|
name: 'generate:injected',
|
|
|
|
run: async () => {
|
|
|
|
const {
|
|
|
|
outputFiles: [{text}],
|
|
|
|
} = await esbuild.build({
|
|
|
|
entryPoints: ['src/injected/injected.ts'],
|
|
|
|
bundle: true,
|
|
|
|
format: 'cjs',
|
2023-10-12 13:04:29 +00:00
|
|
|
target: ['chrome117', 'firefox118'],
|
2023-10-11 15:54:26 +00:00
|
|
|
minify: true,
|
|
|
|
write: false,
|
|
|
|
});
|
|
|
|
const template = await readFile('src/templates/injected.ts.tmpl', 'utf8');
|
|
|
|
await mkdir('src/generated', {recursive: true});
|
|
|
|
await writeFile(
|
|
|
|
'src/generated/injected.ts',
|
|
|
|
template.replace('SOURCE_CODE', JSON.stringify(text))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-11 15:55:32 +00:00
|
|
|
export const generatePackageJsonTask = task({
|
|
|
|
name: 'generate:package-json',
|
|
|
|
run: async () => {
|
|
|
|
await mkdir('lib/esm', {recursive: true});
|
|
|
|
await writeFile('lib/esm/package.json', JSON.stringify({type: 'module'}));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-11 15:54:26 +00:00
|
|
|
export const generateTask = task({
|
|
|
|
name: 'generate',
|
2023-10-11 15:55:32 +00:00
|
|
|
dependencies: [
|
|
|
|
generateVersionTask,
|
|
|
|
generateInjectedTask,
|
|
|
|
generatePackageJsonTask,
|
|
|
|
],
|
2023-10-11 15:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export const buildTscTask = task({
|
|
|
|
name: 'build:tsc',
|
|
|
|
dependencies: [generateTask],
|
|
|
|
run: async () => {
|
|
|
|
await execa('tsc', ['-b']);
|
2023-10-11 15:55:32 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const buildTask = task({
|
|
|
|
name: 'build',
|
|
|
|
dependencies: [buildTscTask],
|
|
|
|
run: async () => {
|
|
|
|
const formats = ['esm', 'cjs'];
|
2023-10-12 13:04:29 +00:00
|
|
|
const packages = (await readdir('third_party', {withFileTypes: true}))
|
|
|
|
.filter(dirent => {
|
|
|
|
return dirent.isDirectory();
|
|
|
|
})
|
|
|
|
.map(({name}) => {
|
|
|
|
return name;
|
|
|
|
});
|
2023-10-11 15:55:32 +00:00
|
|
|
const builders = [];
|
2023-10-12 13:04:29 +00:00
|
|
|
for (const format of formats) {
|
2024-02-22 14:30:58 +00:00
|
|
|
const folder = posixPath.join('lib', format, 'third_party');
|
2023-10-12 13:04:29 +00:00
|
|
|
for (const name of packages) {
|
2024-02-22 14:30:58 +00:00
|
|
|
const entrypoint = posixPath.join(folder, name, `${name}.js`);
|
2023-10-11 15:55:32 +00:00
|
|
|
builders.push(
|
|
|
|
await esbuild.build({
|
2024-02-22 14:30:58 +00:00
|
|
|
entryPoints: [entrypoint],
|
|
|
|
outfile: entrypoint,
|
2023-10-11 15:55:32 +00:00
|
|
|
bundle: true,
|
|
|
|
allowOverwrite: true,
|
|
|
|
format,
|
|
|
|
target: 'node16',
|
|
|
|
minify: true,
|
2024-02-22 12:42:59 +00:00
|
|
|
legalComments: 'inline',
|
2023-10-11 15:55:32 +00:00
|
|
|
})
|
|
|
|
);
|
2024-02-22 12:42:59 +00:00
|
|
|
let license = '';
|
|
|
|
switch (name) {
|
|
|
|
case 'rxjs':
|
|
|
|
license = await readFile(
|
2024-02-22 14:30:58 +00:00
|
|
|
path.join(
|
|
|
|
path.dirname(require.resolve('rxjs')),
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'LICENSE.txt'
|
|
|
|
),
|
2024-02-22 12:42:59 +00:00
|
|
|
'utf-8'
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'mitt':
|
|
|
|
license = await readFile(
|
2024-02-22 14:30:58 +00:00
|
|
|
path.join(path.dirname(require.resolve('mitt')), '..', 'LICENSE'),
|
2024-02-22 12:42:59 +00:00
|
|
|
'utf-8'
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Add license handling for ${path}`);
|
|
|
|
}
|
2024-02-22 14:30:58 +00:00
|
|
|
const content = await readFile(entrypoint, 'utf-8');
|
2024-02-22 12:42:59 +00:00
|
|
|
await writeFile(
|
2024-02-22 14:30:58 +00:00
|
|
|
entrypoint,
|
2024-02-22 12:42:59 +00:00
|
|
|
`/**
|
|
|
|
${license}
|
|
|
|
*/
|
|
|
|
${content}`,
|
|
|
|
'utf-8'
|
|
|
|
);
|
2023-10-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
await Promise.all(builders);
|
2023-10-11 15:54:26 +00:00
|
|
|
},
|
|
|
|
});
|