Skip to content

Commit 9b6240f

Browse files
committed
refactor: extract web-ext-react from race-ext-react
0 parents  commit 9b6240f

File tree

4 files changed

+1077
-0
lines changed

4 files changed

+1077
-0
lines changed

bin/web-ext-react

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
3+
var WebExtReact = require('../index.js');
4+
var webExtReact = new WebExtReact();
5+
var fncName = process.argv[2];
6+
webExtReact[fncName]();

index.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const os = require('os');
4+
const cpy = require('cpy');
5+
const watch = require('@cnakazawa/watch');
6+
const { execSync } = require('child_process');
7+
8+
class WebExtReact {
9+
buildPath = path.join('build', 'static');
10+
assetManifestPath = path.join('build', 'asset-manifest.json');
11+
tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'race-ext-react-'));
12+
13+
contentByExtention(ext) {
14+
const assetManifest = JSON.parse(fs.readFileSync(this.assetManifestPath));
15+
return assetManifest.entrypoints.filter((filepath) => path.extname(filepath) === ext);
16+
}
17+
18+
buildApp() {
19+
execSync('yarn run react-scripts build');
20+
}
21+
22+
buildExt() {
23+
if (fs.existsSync(this.assetManifestPath)) {
24+
const extManifest = JSON.parse(fs.readFileSync('extension/manifest.json'));
25+
extManifest.content_scripts[0].js = this.contentByExtention('.js');
26+
extManifest.content_scripts[0].css = this.contentByExtention('.css');
27+
fs.writeFileSync(path.join(this.tmp, 'manifest.json'), JSON.stringify(extManifest));
28+
cpy('.', path.join(this.tmp, 'static'), { parents: true, cwd: this.buildPath });
29+
return this.tmp;
30+
}
31+
}
32+
33+
build() {
34+
this.buildApp();
35+
const dir = this.buildExt();
36+
process.stdout.write(`${dir}\n`);
37+
}
38+
39+
run() {
40+
this.watchAppSrc();
41+
this.watchAppBuild();
42+
}
43+
44+
watchAppSrc() {
45+
watch.watchTree('src', () => {
46+
this.buildApp();
47+
})
48+
}
49+
50+
watchAppBuild() {
51+
watch.watchTree('build', { filter: (f) => {
52+
return f === this.assetManifestPath;
53+
}}, () => {
54+
const sourceDir = this.buildExt();
55+
process.stdout.write(`${sourceDir}\n`);
56+
})
57+
}
58+
}
59+
60+
module.exports = WebExtReact;

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "web-ext-react",
3+
"version": "1.0.0",
4+
"description": "Command line tool to help build, run and test ReactJS based WebExtensions",
5+
"main": "index.js",
6+
"bin": {
7+
"web-ext-react": "bin/web-ext-react"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"keywords": [
13+
"reactjs",
14+
"javascript",
15+
"web-ext",
16+
"webextensions"
17+
],
18+
"author": "mrloop",
19+
"license": "ISC",
20+
"devDependencies": {
21+
"@cnakazawa/watch": "^1.0.4",
22+
"cpy": "^8.1.0"
23+
}
24+
}

0 commit comments

Comments
 (0)