Skip to content

Commit

Permalink
Update build scripts, set project type to 'module'
Browse files Browse the repository at this point in the history
  • Loading branch information
larsjohnsen committed Jan 13, 2024
1 parent 5140221 commit 9b2dffd
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"plugins": [
"@babel/plugin-transform-modules-commonjs",
"@babel/plugin-transform-flow-strip-types"
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-syntax-import-attributes"
],
"sourceMaps": "inline"
}
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ To load the extension into your browser, see [Loading RES into your browser](#lo

#### Build commands

**`yarn start [--browsers=<browsers>]`** will clean `dist/`, then build RES (dev mode), and start a watch task that will rebuild RES when you make changes. Only changed files will be rebuilt.
**`yarn start [--browsers <browsers>]`** will clean `dist/`, then build RES (dev mode), and start a watch task that will rebuild RES when you make changes. Only changed files will be rebuilt.

**`yarn once [--browsers=<browsers>]`** will clean `dist/`, then build RES (dev mode) a single time.
**`yarn once [--browsers <browsers>]`** will clean `dist/`, then build RES (dev mode) a single time.

**`yarn build [--browsers=<browsers>]`** will clean `dist/`, then build RES (release mode). Each build output will be compressed to a .zip file in `dist/zip/`.
**`yarn build [--browsers <browsers>]`** will clean `dist/`, then build RES (release mode). Each build output will be compressed to a .zip file in `dist/zip/`.

`<browsers>` is a comma-separated list of browsers to target, e.g. `chrome,firefox`. `all` will build all targets. By default, `chrome` will be targeted.

Expand Down Expand Up @@ -95,7 +95,7 @@ The default host and port (`localhost` and `4444`) should work for most local in
- `locales`: RES i18n translations
- `tests/`: integration tests
- `package.json`: package info, dependencies
- `build.mjs`: build script
- `build.js`: build script

## Adding new files

Expand Down
56 changes: 27 additions & 29 deletions build.mjs → build.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* @flow */
/* eslint import/no-nodejs-modules: 0 */
/* @noflow */
/* eslint import/no-nodejs-modules: 0, import/extensions: 0 */

// $FlowIssue
import fs from 'node:fs';
// $FlowIssue
import path from 'node:path';
import * as commander from 'commander';
import * as esbuild from 'esbuild';
import * as semver from 'semver';
import JSZip from 'jszip';
import * as esbuild from 'esbuild';
import { copy } from 'esbuild-plugin-copy';
import flow from 'esbuild-plugin-flow';
import { copy } from 'esbuild-plugin-copy';
import { sassPlugin } from 'esbuild-sass-plugin';
import isBetaVersion from './build/isBetaVersion.js';
import packageInfo from './package.json' with { type: 'json' };

const targets = {
chrome: {
Expand All @@ -25,12 +25,12 @@ const targets = {
manifest: './chrome/beta/manifest.json',
},
edge: {
browserName: 'chrome',
browserName: 'edge',
browserMinVersion: '114.0',
manifest: './chrome/manifest.json',
},
opera: {
browserName: 'chrome',
browserName: 'opera',
browserMinVersion: '114.0',
manifest: './chrome/manifest.json',
noSourcemap: true,
Expand All @@ -53,29 +53,27 @@ const options = commander.program

const isProduction = options.mode === 'production';
const devBuildToken = `${Math.random()}`.slice(2);
const announcementsSubreddit /*: string */ = 'RESAnnouncements';
const name /*: string */ = packageInfo.title;
const author /*: string */ = packageInfo.author;
const description /*: string */ = packageInfo.description;
const version /*: string */ = packageInfo.version;
const isBeta /*: boolean */ = isBetaVersion(version);
const isPatch /*: boolean */ = semver.patch(version) !== 0;
const isMinor /*: boolean */ = !isPatch && semver.minor(version) !== 0;
const isMajor /*: boolean */ = !isPatch && !isMinor && semver.major(version) !== 0;
const updatedURL /*: string */ = isBeta ?
// link to the release listing page instead of a specific release page
// so if someone goes from the previous version to a hotfix (e.g. 5.10.3 -> 5.12.1)
// they see the big release notes for the minor release in addition to the changes in the hotfix
`https://redditenhancementsuite.com/releases/beta/#v${version}` :
`https://redditenhancementsuite.com/releases/#v${version}`;
const homepageURL /*: string */ = packageInfo.homepage;
// used for invalidating caches on each build (executed at build time)
// production builds uses version number to keep the build reproducible
const buildToken = isProduction ? version : devBuildToken;

async function buildForBrowser(targetName, { manifest, noSourceMap, browserName, browserMinVersion }) {
const packageInfo = await fs.promises.readFile('./package.json').then(JSON.parse);
const announcementsSubreddit /*: string */ = 'RESAnnouncements';
const name /*: string */ = packageInfo.title;
const author /*: string */ = packageInfo.author;
const description /*: string */ = packageInfo.description;
const version /*: string */ = packageInfo.version;
const isBeta /*: boolean */ = (semver.minor(version) % 2) === 1;
const isPatch /*: boolean */ = semver.patch(version) !== 0;
const isMinor /*: boolean */ = !isPatch && semver.minor(version) !== 0;
const isMajor /*: boolean */ = !isPatch && !isMinor && semver.major(version) !== 0;
const updatedURL /*: string */ = isBeta ?
// link to the release listing page instead of a specific release page
// so if someone goes from the previous version to a hotfix (e.g. 5.10.3 -> 5.12.1)
// they see the big release notes for the minor release in addition to the changes in the hotfix
`https://redditenhancementsuite.com/releases/beta/#v${version}` :
`https://redditenhancementsuite.com/releases/#v${version}`;
const homepageURL /*: string */ = packageInfo.homepage;
// used for invalidating caches on each build (executed at build time)
// production builds uses version number to keep the build reproducible
const buildToken = isProduction ? version : devBuildToken;

const context = {
entryPoints: {
'foreground.entry': './lib/foreground.entry.js',
Expand Down
24 changes: 13 additions & 11 deletions build/deploy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* @noflow */

/* eslint-disable import/no-commonjs, import/no-nodejs-modules */
/* eslint import/no-nodejs-modules: 0, import/extensions: 0 */

const fs = require('fs'); // eslint-disable-line import/no-extraneous-dependencies
const path = require('path'); // eslint-disable-line import/no-extraneous-dependencies
const chromeDeploy = require('chrome-extension-deploy');
const firefoxDeploy = require('firefox-extension-deploy');
const { version } = require('../package.json');
const isBetaVersion = require('./isBetaVersion');
import fs from 'node:fs';
import path from 'node:path';
import chromeDeploy from 'chrome-extension-deploy';
import firefoxDeploy from 'firefox-extension-deploy';
import packageInfo from '../package.json' with { type: 'json' };
import isBetaVersion from './isBetaVersion.js';

const version = packageInfo.version;

if (isBetaVersion(version)) {
console.log(`Deploying ${version} beta release...`);
Expand All @@ -29,7 +31,7 @@ function deployChromeBeta() {
clientSecret: process.env.CHROME_CLIENT_SECRET,
refreshToken: process.env.CHROME_REFRESH_TOKEN,
id: 'flhpapomijliefifkkeepedibpmibbpo',
zip: fs.readFileSync(path.join(__dirname, '../dist/zip/chrome-beta.zip')),
zip: fs.readFileSync(path.join(import.meta.dirname, '../dist/zip/chromebeta.zip')),
to: chromeDeploy.TRUSTED_TESTERS,
}).then(() => {
console.log('Chrome beta deployment complete!');
Expand All @@ -47,7 +49,7 @@ function deployChromeStable() {
clientSecret: process.env.CHROME_CLIENT_SECRET,
refreshToken: process.env.CHROME_REFRESH_TOKEN,
id: 'kbmfpngjjgdllneeigpgjifpgocmfgmb',
zip: fs.readFileSync(path.join(__dirname, '../dist/zip/chrome.zip')),
zip: fs.readFileSync(path.join(import.meta.dirname, '../dist/zip/chrome.zip')),
}).then(() => {
console.log('Chrome stable deployment complete!');
}, err => {
Expand All @@ -63,8 +65,8 @@ function deployFirefoxStable() {
issuer: process.env.FIREFOX_ISSUER,
secret: process.env.FIREFOX_SECRET,
id: 'jid1-xUfzOsOFlzSOXg@jetpack',
version: require('../dist/firefox/manifest.json').version, // eslint-disable-line global-require
src: fs.createReadStream(path.join(__dirname, '../dist/zip/firefox.zip')),
version,
src: fs.createReadStream(path.join(import.meta.dirname, '../dist/zip/firefox.zip')),
}).then(() => {
console.log('Firefox stable deployment complete!');
}, err => {
Expand Down
24 changes: 12 additions & 12 deletions build/deployChangelog.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* @noflow */
/* eslint import/no-nodejs-modules: 0, import/extensions: 0 */

/* eslint-disable import/no-commonjs, import/no-nodejs-modules */
import { execSync } from 'child_process';
import fs from 'node:fs';
import path from 'node:path';
import { rimrafSync } from 'rimraf';
import packageInfo from '../package.json' with { type: 'json' };
import { changelogPathFromVersion } from './utils/changelog.js';
import isBetaVersion from './isBetaVersion.js';

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path'); // eslint-disable-line import/no-extraneous-dependencies
const rimraf = require('rimraf');
const { version } = require('../package.json');
const { changelogPathFromVersion } = require('./utils/changelog');
const isBetaVersion = require('./isBetaVersion');
const version = packageInfo.version;

const tempDir = path.join(__dirname, '..', 'dist', 'temp');
const tempDir = path.join(import.meta.dirname, '..', 'dist', 'temp');

rimraf.sync(tempDir);
rimrafSync(tempDir);
execSync(`git clone --depth=1 https://github.com/Reddit-Enhancement-Suite/Reddit-Enhancement-Suite.github.io.git ${tempDir}`);

const releaseTimestamp = (/^tagger.+?\s(\d+)\s/m).exec(execSync(`git cat-file tag v${version}`, { encoding: 'utf8' }))[1];
const releaseDate = new Date(releaseTimestamp * 1000);
const releaseDate = new Date(execSync(`git log -1 --format=%ai v${version}`, { encoding: 'utf8' }));

const newChangelogFile = path.join(tempDir, '_posts', `${releaseDate.toISOString().slice(0, 10)}-${version.replace(/\./g, '')}.md`);

Expand Down
10 changes: 5 additions & 5 deletions build/i18nLint.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* @noflow */

/* eslint-disable import/no-commonjs, import/no-nodejs-modules */
/* eslint-disable import/no-nodejs-modules */

// Checks that all keys listed in locales/locales/en.json are used somewhere in lib/**/*.js.
// Keys must be used verbatim, that is, literally 'thisKey', and not 'this' + 'Key'.
// This is required not only for this script, but also so that developers can always grep for usages of an i18n key.

const { readFileSync, readdirSync, statSync } = require('fs');
const { join } = require('path'); // eslint-disable-line import/no-extraneous-dependencies
const i18n = require('../locales/locales/en.json');
import { readFileSync, readdirSync, statSync } from 'fs';
import { join } from 'path';
import i18n from '../locales/locales/en.json' with { type: 'json' };

function checkUnused() {
let allFiles = '';
Expand All @@ -23,7 +23,7 @@ function checkUnused() {
allFiles += readFileSync(filePath);
}
}
})(join(__dirname, '..', 'lib'));
})(join(import.meta.dirname, '..', 'lib'));

const allKeys = Object.keys(i18n);
const unusedKeys = allKeys.filter(key => !allFiles.includes(`'${key}'`));
Expand Down
6 changes: 3 additions & 3 deletions build/i18nTransformer.js → build/i18nTransformer.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import fs from 'fs';
import path from 'path'; // eslint-disable-line import/no-extraneous-dependencies
import { mapValues, startCase } from 'lodash-es';
import _ from 'lodash';


export default function i18nTransformer(file, api) {
Expand Down Expand Up @@ -40,7 +40,7 @@ export default function i18nTransformer(file, api) {
const optName = optionPath.node.key.name;

const titleKey = `${modName}${upCase(optName)}Title`;
newI18nKeys[titleKey] = startCase(optName);
newI18nKeys[titleKey] = _.startCase(optName);
optionPath.node.value.properties.unshift(j.property('init', j.identifier('title'), j.literal(titleKey)));

j(optionPath)
Expand All @@ -60,7 +60,7 @@ export default function i18nTransformer(file, api) {
const enJson = fs.readFileSync(enJsonLocation, { encoding: 'utf8' });

const enJsonObj = JSON.parse(enJson);
Object.assign(enJsonObj, mapValues(newI18nKeys, message => ({ message })));
Object.assign(enJsonObj, _.mapValues(newI18nKeys, message => ({ message })));
const newEnJson = JSON.stringify(enJsonObj, null, '\t');

fs.writeFileSync(enJsonLocation, newEnJson);
Expand Down
14 changes: 5 additions & 9 deletions build/utils/changelog.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/* @noflow */
/* eslint import/no-nodejs-modules: 0 */

/* eslint-disable import/no-commonjs, import/no-nodejs-modules */

const path = require('path'); // eslint-disable-line import/no-extraneous-dependencies
import path from 'node:path';

const dir = 'changelog';

function changelogPath(filename) {
return path.resolve(__dirname, path.join('..', '..', dir, filename));
export function changelogPath(filename) {
return path.join(import.meta.dirname, '..', '..', dir, filename);
}

function changelogPathFromVersion(version) {
export function changelogPathFromVersion(version) {
return changelogPath(`v${version}.md`);
}

module.exports.changelogPath = changelogPath;
module.exports.changelogPathFromVersion = changelogPathFromVersion;
14 changes: 7 additions & 7 deletions build/version.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* @flow */
/* @noflow */
/* eslint import/no-nodejs-modules: 0, import/extensions: 0 */

/* eslint-disable import/no-commonjs, import/no-nodejs-modules */

const { execSync } = require('child_process');
const fs = require('fs');
const { version, repository } = require('../package.json');
const { changelogPath, changelogPathFromVersion } = require('./utils/changelog');
import { execSync } from 'child_process';
import fs from 'fs';
import packageData from '../package.json' with { type: 'json' };
import { changelogPath, changelogPathFromVersion } from './utils/changelog.js';

const { version, repository } = packageData;
const unreleasedChangelog = changelogPath('UNRELEASED.md');
const templateChangelog = changelogPath('_EXAMPLE.md');
const newChangelog = changelogPathFromVersion(version);
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "Reddit Enhancement Suite contributors",
"license": "GPL-3.0",
"private": true,
"type": "module",
"homepage": "https://redditenhancementsuite.com",
"repository": {
"type": "git",
Expand All @@ -16,12 +17,12 @@
"scripts": {
"preinstall": "node -v",
"prestart": "rimraf dist",
"start": "node build.mjs --watch --mode development",
"start": "node build.js --watch --mode development",
"preonce": "rimraf dist",
"once": "node build.mjs --mode development",
"once": "node build.js --mode development",
"prebuild": "rimraf dist",
"build": "node build.mjs --mode production --zip",
"autoi18n": "jscodeshift --parser flow --transform build/i18nTransformer.js",
"build": "node build.js --mode production --zip",
"autoi18n": "jscodeshift --parser flow --transform build/i18nTransformer.cjs",
"postautoi18n": "eslint --fix lib/modules/*.js",
"eslint": "eslint .",
"stylelint": "stylelint lib/**/*.scss",
Expand All @@ -31,7 +32,7 @@
"flow": "flow",
"test": "cross-env NODE_ENV=test ava",
"integration-only": "nightwatch --env",
"preintegration": "yarn build --browsers=chrome,firefox",
"preintegration": "yarn build --browsers chrome,firefox",
"integration": "yarn integration-only",
"deploy": "node build/deploy.js",
"deploy-changelog": "node build/deployChangelog.js",
Expand All @@ -58,6 +59,7 @@
"@babel/core": "7.23.7",
"@babel/eslint-parser": "7.23.3",
"@babel/plugin-syntax-flow": "7.23.3",
"@babel/plugin-syntax-import-attributes": "7.23.3",
"@babel/plugin-transform-flow-strip-types": "7.23.3",
"@babel/plugin-transform-modules-commonjs": "7.23.3",
"@babel/register": "7.23.7",
Expand All @@ -80,6 +82,7 @@
"eslint-plugin-you-dont-need-lodash-underscore": "6.13.0",
"firefox-extension-deploy": "1.1.2",
"flow-bin": "0.84.0",
"jscodeshift": "0.15.1",
"jszip": "3.10.1",
"nightwatch": "1.7.13",
"rimraf": "5.0.5",
Expand Down
Loading

0 comments on commit 9b2dffd

Please sign in to comment.