Skip to content

Commit a57e3d7

Browse files
authored
refactor: pure es6 import syntax (#384)
* refactor: convert require syntax to import syntax * refactor: downgrade ssestream to avoild "SSEStream is not a constructor" problem See EventSource/node-ssestream#3 (comment) * refactor: module is not defined in pure es6 module package so remove it The removed code is not important since we use absolute path when calling the crx-webpack-plugin. * fix: exsiting way to write process.env.XXX does not work * chore: format code * refactor: convert specific ci scripts to es6 modules * refactor: conventional-changelog-action requires pre-commit.js as a CommonJS module
1 parent 211a287 commit a57e3d7

File tree

15 files changed

+874
-861
lines changed

15 files changed

+874
-861
lines changed

.github/workflows/release-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
with:
2424
github-token: ${{ secrets.GITHUB_TOKEN }}
2525
skip-on-empty: 'false'
26-
pre-commit: scripts/pre-commit.js
26+
pre-commit: scripts/pre-commit.cjs
2727
git-message: 'chore(release): {version}'
2828

2929
- name: Build

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "hypercrx",
33
"version": "1.6.0",
4+
"type": "module",
45
"private": true,
56
"description": "Hypertrons Chromium Extension",
67
"license": "Apache",
78
"scripts": {
8-
"build": "node utils/build.js",
9-
"start": "node utils/server.js",
9+
"build": "NODE_ENV='production' BABEL_ENV='production' node utils/build.js",
10+
"start": "NODE_ENV='development' BABEL_ENV='development' node utils/server.js",
1011
"deploy": "node scripts/deploy.js",
1112
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,json,css,scss}\"",
1213
"prettier:check": "prettier --check \"**/*.{js,jsx,ts,tsx,json,css,scss}\"",
@@ -43,7 +44,7 @@
4344
"@types/react": "^17.0.39",
4445
"@types/react-dom": "^17.0.11",
4546
"babel-loader": "^8.2.3",
46-
"chrome-webstore-upload": "0.5.0",
47+
"chrome-webstore-upload": "^1.0.0",
4748
"clean-webpack-plugin": "^4.0.0",
4849
"copy-webpack-plugin": "^7.0.0",
4950
"crx": "^5.0.1",
@@ -53,14 +54,15 @@
5354
"html-loader": "^3.1.0",
5455
"html-webpack-plugin": "^5.5.0",
5556
"husky": "^8.0.1",
57+
"lodash-es": "^4.17.21",
5658
"mkdirp": "^1.0.4",
5759
"prettier": "^2.7.1",
5860
"pretty-quick": "^3.1.3",
5961
"querystring": "^0.2.1",
6062
"sass": "^1.52.1",
6163
"sass-loader": "^12.4.0",
6264
"source-map-loader": "^3.0.1",
63-
"ssestream": "^1.1.0",
65+
"ssestream": "1.0.1",
6466
"style-loader": "^3.3.1",
6567
"terser-webpack-plugin": "^5.3.1",
6668
"ts-loader": "^9.2.6",

scripts/bump-version.js renamed to scripts/bump-version.cjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
// according to https://github.com/TriPSs/conventional-changelog-action#pre-commit-hook
2+
// this script should be a CommonJS module
3+
14
async function bump({ version, deploy }) {
2-
const { readJson, writeJson, processFile } = require('./utils.js');
5+
const { readJson, writeJson, processFile } = require('./utils.cjs');
36
// update package.json
47
const pkgPath = 'package.json';
58
const pkg = await readJson(pkgPath);
69
pkg.version = version;
7-
await writeJson(pkgPath, pkg);
10+
writeJson(pkgPath, pkg);
811

912
// update update_information.json
1013
const infoPath = 'publish/update_information.json';

scripts/deploy.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
const chromeWebstoreUpload = require('chrome-webstore-upload');
2-
const path = require('path');
3-
const fs = require('fs');
1+
import chromeWebstoreUpload from 'chrome-webstore-upload';
2+
import { fileURLToPath } from 'url';
3+
import path, { dirname } from 'path';
4+
import fs from 'fs';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
48

59
const ZIP_PATH = path.join(__dirname, '..', '/release/hypercrx.zip');
610

scripts/pre-commit.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// according to https://github.com/TriPSs/conventional-changelog-action#pre-commit-hook
2+
// this script should be a CommonJS module
3+
4+
const { bump } = require('./bump-version.cjs');
5+
6+
exports.preCommit = ({ version }) => {
7+
bump({ version: version, deploy: true });
8+
};

scripts/pre-commit.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

scripts/utils.js renamed to scripts/utils.cjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// according to https://github.com/TriPSs/conventional-changelog-action#pre-commit-hook
2+
// this script should be a CommonJS module
3+
14
const fs = require('fs');
25

36
function readJson(filename) {

src/utils/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const ENV = require('../../utils/env');
1+
// @ts-ignore
2+
import ENV from '../../utils/env';
23
import { ErrorCode } from '../constant';
34

45
/**

utils/autoReloadClients/backgroundClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const querystring = require('querystring');
1+
import querystring from 'querystring';
22

33
const logger = (msg) => {
44
console.log(`[BGC] ${msg}`);

utils/build.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
// Do this as the first thing so that any code reading it knows the right env.
2-
process.env.BABEL_ENV = 'production';
3-
process.env.NODE_ENV = 'production';
4-
process.env.ASSET_PATH = '/';
1+
import { fileURLToPath } from 'url';
2+
import path, { dirname } from 'path';
3+
import CrxWebpackPlugin from './crx-webpack-plugin/index.js';
4+
import webpack from 'webpack';
5+
import config from '../webpack.config.js';
56

6-
const path = require('path');
7-
const CrxWebpackPlugin = require('./crx-webpack-plugin/index');
8-
9-
var webpack = require('webpack'),
10-
config = require('../webpack.config');
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = dirname(__filename);
119

1210
delete config.custom;
1311

utils/crx-webpack-plugin/index.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
var join = path.join;
4-
var mkdirp = require('mkdirp');
5-
var ChromeExtension = require('crx');
1+
import fs from 'fs';
2+
import { join } from 'path';
3+
import mkdirp from 'mkdirp';
4+
import ChromeExtension from 'crx';
65

76
function CrxWebpackPlugin(options) {
87
this.options = options || {};
@@ -17,16 +16,9 @@ function CrxWebpackPlugin(options) {
1716
this.options.updateUrl = this.options.updateUrl.replace(/\/$/, '');
1817

1918
// setup paths
20-
this.context = path.dirname(module.parent.filename);
21-
this.keyFile = path.isAbsolute(this.options.keyFile)
22-
? this.options.keyFile
23-
: join(this.context, this.options.keyFile);
24-
this.outputPath = path.isAbsolute(this.options.outputPath)
25-
? this.options.outputPath
26-
: join(this.context, this.options.outputPath);
27-
this.contentPath = path.isAbsolute(this.options.contentPath)
28-
? this.options.contentPath
29-
: join(this.context, this.options.contentPath);
19+
this.keyFile = this.options.keyFile;
20+
this.outputPath = this.options.outputPath;
21+
this.contentPath = this.options.contentPath;
3022

3123
// set output info
3224
this.crxName = this.options.name + '.crx';
@@ -81,4 +73,4 @@ CrxWebpackPlugin.prototype.package = function () {
8173
});
8274
};
8375

84-
module.exports = CrxWebpackPlugin;
76+
export default CrxWebpackPlugin;

utils/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// tiny wrapper with default env vars
2-
module.exports = {
2+
export default {
33
NODE_ENV: process.env.NODE_ENV || 'development',
44
PORT: process.env.PORT || 3000,
55
MOCK: process.env.MOCK || false,

utils/server.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
// Do this as the first thing so that any code reading it knows the right env.
2-
process.env.BABEL_ENV = 'development';
3-
process.env.NODE_ENV = 'development';
4-
process.env.ASSET_PATH = '/';
5-
6-
const WebpackDevServer = require('webpack-dev-server'),
7-
webpack = require('webpack'),
8-
config = require('../webpack.config'),
9-
env = require('./env'),
10-
path = require('path');
11-
const { custom } = require('../webpack.config');
12-
const debounce = require('lodash').debounce;
13-
const SSEStream = require('ssestream').default;
1+
import WebpackDevServer from 'webpack-dev-server';
2+
import webpack from 'webpack';
3+
import config from '../webpack.config.js';
4+
import env from './env.js';
5+
import { fileURLToPath } from 'url';
6+
import path, { dirname } from 'path';
7+
import { debounce } from 'lodash-es';
8+
import SSEStream from 'ssestream';
9+
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = dirname(__filename);
1412

1513
const customOptions = config.custom;
1614

@@ -121,7 +119,7 @@ const server = new WebpackDevServer(
121119
customOptions.enableContentScriptsAutoReload;
122120

123121
if (shouldBackgroundReload) {
124-
sseStream.writeMessage(
122+
sseStream.write(
125123
{
126124
event: 'background-updated',
127125
data: {}, // "data" key should be reserved though it is empty.
@@ -130,7 +128,7 @@ const server = new WebpackDevServer(
130128
);
131129
}
132130
if (shouldContentScriptsReload) {
133-
sseStream.writeMessage(
131+
sseStream.write(
134132
{
135133
event: 'content-scripts-updated',
136134
data: {},

webpack.config.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
const webpack = require('webpack'),
2-
path = require('path'),
3-
fileSystem = require('fs-extra'),
4-
env = require('./utils/env'),
5-
CopyWebpackPlugin = require('copy-webpack-plugin'),
6-
HtmlWebpackPlugin = require('html-webpack-plugin'),
7-
TerserPlugin = require('terser-webpack-plugin');
8-
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1+
import webpack from 'webpack';
2+
import { fileURLToPath } from 'url';
3+
import path, { dirname } from 'path';
4+
import fileSystem from 'fs-extra';
5+
import env from './utils/env.js';
6+
import CopyWebpackPlugin from 'copy-webpack-plugin';
7+
import HtmlWebpackPlugin from 'html-webpack-plugin';
8+
import TerserPlugin from 'terser-webpack-plugin';
9+
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
10+
11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = dirname(__filename);
913

1014
const ASSET_PATH = process.env.ASSET_PATH || '/';
1115

@@ -203,4 +207,4 @@ if (env.NODE_ENV === 'development') {
203207
};
204208
}
205209

206-
module.exports = options;
210+
export default options;

0 commit comments

Comments
 (0)