Skip to content

Commit 5ad4c85

Browse files
committed
feat: handle other ext contents
1 parent 9b6240f commit 5ad4c85

24 files changed

+23861
-754
lines changed

commitlint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { extends: ["@commitlint/config-conventional"] };

index.js

+114-28
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,119 @@
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');
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");
77

88
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-'));
9+
get buildPath() {
10+
return path.join(process.cwd(), "build", "static");
11+
}
12+
13+
get assetManifestPath() {
14+
return path.join("build", "asset-manifest.json");
15+
}
16+
17+
get tmp() {
18+
this._tmp =
19+
this._tmp || fs.mkdtempSync(path.join(os.tmpdir(), "web-ext-react-"));
20+
return this._tmp;
21+
}
1222

13-
contentByExtention(ext) {
23+
get htmlTemplate() {
24+
return `<!DOCTYPE html>
25+
<html lang="en">
26+
<head>
27+
<meta charset="utf-8">
28+
<!-- INSERT-HEAD -->
29+
</head>
30+
<body>
31+
<!-- INSERT-BODY -->
32+
</body>
33+
</html>`;
34+
}
35+
36+
contentByExtension(ext) {
1437
const assetManifest = JSON.parse(fs.readFileSync(this.assetManifestPath));
15-
return assetManifest.entrypoints.filter((filepath) => path.extname(filepath) === ext);
38+
return assetManifest.entrypoints.filter(
39+
(filepath) => path.extname(filepath) === ext
40+
);
1641
}
1742

1843
buildApp() {
19-
execSync('yarn run react-scripts build');
44+
execSync("yarn run react-scripts build");
2045
}
2146

22-
buildExt() {
47+
get extManifest() {
48+
if (this._extManifest) {
49+
return this._extManifest;
50+
}
2351
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 });
52+
this._extManifest = JSON.parse(
53+
fs.readFileSync("extension/manifest.json")
54+
);
55+
}
56+
return this._extManifest;
57+
}
58+
59+
async buildExt() {
60+
if (this.extManifest) {
61+
this.addContentScript();
62+
await this.bundleExt();
63+
this.addHtml();
2964
return this.tmp;
3065
}
3166
}
3267

33-
build() {
68+
addHtml() {
69+
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Sidebars_popups_options_pages
70+
["background", "sidebar_action", "browser_action", "options_ui"].forEach(
71+
(data) => {
72+
if (this.extManifest[data]) {
73+
data = data.replace("_", "-");
74+
let page =
75+
this.extManifest[data].page ||
76+
this.extManifest[data].default_popup ||
77+
this.extManifest[data].default_panel;
78+
fs.writeFileSync(`${this.tmp}/${page}`, this.html(data));
79+
}
80+
}
81+
);
82+
}
83+
84+
addContentScript() {
85+
this.extManifest.content_scripts[0].js = this.contentByExtension(".js");
86+
this.extManifest.content_scripts[0].css = this.contentByExtension(".css");
87+
}
88+
89+
html(data) {
90+
const scriptJs = this.contentByExtension(".js")
91+
.map((str) => `<script type="module" src="${str}" data-${data}></script>`)
92+
.join("\n");
93+
const linkCss = this.contentByExtension(".css")
94+
.map((str) => `<link rel="stylesheet" href="${str}"/>`)
95+
.join("\n");
96+
return this.htmlTemplate
97+
.replace("<!-- INSERT-HEAD -->", linkCss)
98+
.replace("<!-- INSERT-BODY -->", scriptJs);
99+
}
100+
101+
async bundleExt() {
102+
fs.writeFileSync(
103+
path.join(this.tmp, "manifest.json"),
104+
JSON.stringify(this.extManifest)
105+
);
106+
await cpy(".", path.join(this.tmp, "static"), {
107+
parents: true,
108+
cwd: this.buildPath,
109+
});
110+
}
111+
112+
async build() {
34113
this.buildApp();
35-
const dir = this.buildExt();
114+
const dir = await this.buildExt();
36115
process.stdout.write(`${dir}\n`);
116+
return dir;
37117
}
38118

39119
run() {
@@ -42,18 +122,24 @@ class WebExtReact {
42122
}
43123

44124
watchAppSrc() {
45-
watch.watchTree('src', () => {
125+
watch.watchTree("src", () => {
46126
this.buildApp();
47-
})
127+
});
48128
}
49129

50130
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-
})
131+
watch.watchTree(
132+
"build",
133+
{
134+
filter: (f) => {
135+
return f === this.assetManifestPath;
136+
},
137+
},
138+
async () => {
139+
const sourceDir = await this.buildExt();
140+
process.stdout.write(`${sourceDir}\n`);
141+
}
142+
);
57143
}
58144
}
59145

package.json

+48-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"web-ext-react": "bin/web-ext-react"
88
},
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"lint": "eslint index.js",
11+
"test": "qunit tests/unit/*-test.js"
1112
},
1213
"keywords": [
1314
"reactjs",
@@ -17,8 +18,52 @@
1718
],
1819
"author": "mrloop",
1920
"license": "ISC",
20-
"devDependencies": {
21+
"eslintConfig": {
22+
"env": {
23+
"es6": true,
24+
"node": true
25+
},
26+
"extends": [
27+
"plugin:prettier/recommended"
28+
],
29+
"parserOptions": {
30+
"ecmaVersion": 2018
31+
}
32+
},
33+
"prettier": {
34+
"trailingComma": "es5"
35+
},
36+
"lint-staged": {
37+
"*.+(js|jsx)": [
38+
"eslint --fix"
39+
],
40+
"*.+(json|css|md)": [
41+
"prettier --write"
42+
]
43+
},
44+
"dependencies": {
2145
"@cnakazawa/watch": "^1.0.4",
22-
"cpy": "^8.1.0"
46+
"cpy": "^8.1.0",
47+
"react": "^16.13.1",
48+
"react-dom": "^16.13.1",
49+
"react-scripts": "3.4.1"
50+
},
51+
"devDependencies": {
52+
"@commitlint/cli": "^9.1.1",
53+
"@commitlint/config-conventional": "^9.1.1",
54+
"babel-eslint": "^10.1.0",
55+
"eslint-config-prettier": "^6.11.0",
56+
"eslint-config-react-app": "^5.2.1",
57+
"eslint-plugin-prettier": "^3.1.4",
58+
"husky": "^4.2.5",
59+
"lint-staged": "^10.2.11",
60+
"prettier": "^2.0.5",
61+
"qunit": "^2.11.0"
62+
},
63+
"husky": {
64+
"hooks": {
65+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
66+
"pre-commit": "lint-staged"
67+
}
2368
}
2469
}

tests/dummy/.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

tests/dummy/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `yarn start`
8+
9+
Runs the app in the development mode.<br />
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br />
13+
You will also see any lint errors in the console.
14+
15+
### `yarn test`
16+
17+
Launches the test runner in the interactive watch mode.<br />
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `yarn build`
21+
22+
Builds the app for production to the `build` folder.<br />
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br />
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `yarn eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35+
36+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).
45+
46+
### Code Splitting
47+
48+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49+
50+
### Analyzing the Bundle Size
51+
52+
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53+
54+
### Making a Progressive Web App
55+
56+
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57+
58+
### Advanced Configuration
59+
60+
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61+
62+
### Deployment
63+
64+
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65+
66+
### `yarn build` fails to minify
67+
68+
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

tests/dummy/extension/manifest.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "web-ext-react dummy app",
4+
"version": "0.1",
5+
6+
"description": "dummy app to test web-ext-react",
7+
"homepage_url": "https://github.com/mrloop/web-ext-react",
8+
"icons": {
9+
"48": "icon.svg",
10+
"96": "icon.svg"
11+
},
12+
"content_scripts": [
13+
{
14+
"matches": ["*"],
15+
"js": [],
16+
"css": []
17+
}
18+
],
19+
"background": {
20+
"page": "background-page.html"
21+
}
22+
}

tests/dummy/package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "dummy",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"react": "^16.13.1",
10+
"react-dom": "^16.13.1",
11+
"react-scripts": "3.4.3"
12+
},
13+
"scripts": {
14+
"start": "react-scripts start",
15+
"build": "react-scripts build",
16+
"test": "react-scripts test",
17+
"eject": "react-scripts eject"
18+
},
19+
"eslintConfig": {
20+
"extends": "react-app"
21+
},
22+
"browserslist": {
23+
"production": [
24+
">0.2%",
25+
"not dead",
26+
"not op_mini all"
27+
],
28+
"development": [
29+
"last 1 chrome version",
30+
"last 1 firefox version",
31+
"last 1 safari version"
32+
]
33+
}
34+
}

tests/dummy/public/favicon.ico

3.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)