Skip to content

Commit 1853752

Browse files
committed
restructure code
1 parent 008bf6e commit 1853752

25 files changed

+135
-24
lines changed
File renamed without changes.
File renamed without changes.

build/build.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const archiver = require("archiver");
2+
const { execSync } = require("child_process");
3+
const { readFileSync, writeFileSync, createWriteStream } = require("fs");
4+
const { copy, ensureDir, move, remove } = require("fs-extra");
5+
const { join } = require("path");
6+
7+
// These files are copied along with Webpack-bundled files
8+
// to produce the final web extension
9+
const STATIC_FILES = ["icons", "popups", "main.html", "panel.html"];
10+
11+
const preProcess = async (destinationPath, tempPath) => {
12+
await remove(destinationPath); // Clean up from previously completed builds
13+
await remove(tempPath); // Clean up from any previously failed builds
14+
await ensureDir(tempPath); // Create temp dir for this new build
15+
};
16+
17+
const build = async (tempPath, manifestPath) => {
18+
const binPath = join(tempPath, "bin");
19+
const zipPath = join(tempPath, "zip");
20+
21+
const webpackPath = join(
22+
__dirname,
23+
"..",
24+
"..",
25+
"node_modules",
26+
".bin",
27+
"webpack"
28+
);
29+
execSync(
30+
`${webpackPath} --config webpack.config.js --output-path ${binPath}`,
31+
{
32+
cwd: __dirname,
33+
env: process.env,
34+
stdio: "inherit",
35+
}
36+
);
37+
execSync(
38+
`${webpackPath} --config webpack.backend.js --output-path ${binPath}`,
39+
{
40+
cwd: __dirname,
41+
env: process.env,
42+
stdio: "inherit",
43+
}
44+
);
45+
46+
// Make temp dir
47+
await ensureDir(zipPath);
48+
49+
const copiedManifestPath = join(zipPath, "manifest.json");
50+
51+
// Copy unbuilt source files to zip dir to be packaged:
52+
await copy(binPath, join(zipPath, "build"));
53+
await copy(manifestPath, copiedManifestPath);
54+
await Promise.all(
55+
STATIC_FILES.map((file) => copy(join(__dirname, file), join(zipPath, file)))
56+
);
57+
58+
const commit = getGitCommit();
59+
const dateString = new Date().toLocaleDateString();
60+
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
61+
const versionDateString = `${manifest.version} (${dateString})`;
62+
if (manifest.version_name) {
63+
manifest.version_name = versionDateString;
64+
}
65+
manifest.description += `\n\nCreated from revision ${commit} on ${dateString}.`;
66+
67+
writeFileSync(copiedManifestPath, JSON.stringify(manifest, null, 2));
68+
69+
// Pack the extension
70+
const archive = archiver("zip", { zlib: { level: 9 } });
71+
const zipStream = createWriteStream(join(tempPath, "ReactDevTools.zip"));
72+
await new Promise((resolve, reject) => {
73+
archive
74+
.directory(zipPath, false)
75+
.on("error", (err) => reject(err))
76+
.pipe(zipStream);
77+
archive.finalize();
78+
zipStream.on("close", () => resolve());
79+
});
80+
};
81+
82+
const postProcess = async (tempPath, destinationPath) => {
83+
const unpackedSourcePath = join(tempPath, "zip");
84+
const packedSourcePath = join(tempPath, "ReactDevTools.zip");
85+
const packedDestPath = join(destinationPath, "ReactDevTools.zip");
86+
const unpackedDestPath = join(destinationPath, "unpacked");
87+
88+
await move(unpackedSourcePath, unpackedDestPath); // Copy built files to destination
89+
await move(packedSourcePath, packedDestPath); // Copy built files to destination
90+
await remove(tempPath); // Clean up temp directory and files
91+
};
92+
93+
const main = async (buildId) => {
94+
const root = join(__dirname, buildId);
95+
const manifestPath = join(root, "manifest.json");
96+
const destinationPath = join(root, "build");
97+
98+
try {
99+
const tempPath = join(__dirname, "build", buildId);
100+
await preProcess(destinationPath, tempPath);
101+
await build(tempPath, manifestPath);
102+
103+
const builtUnpackedPath = join(destinationPath, "unpacked");
104+
await postProcess(tempPath, destinationPath);
105+
106+
return builtUnpackedPath;
107+
} catch (error) {
108+
console.error(error);
109+
process.exit(1);
110+
}
111+
112+
return null;
113+
};
114+
115+
module.exports = main;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

+20-24
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
"name": "react-context-devtool",
33
"version": "1.0.1",
44
"description": "Devtool for React Context",
5-
"main": "dist/plugin/cjs/index.js",
6-
"module": "dist/plugin/esm/index.js",
5+
"main": "dist/module/cjs/index.js",
6+
"module": "dist/module/esm/index.js",
77
"files": [
8-
"dist/plugin"
8+
"dist/module"
99
],
1010
"scripts": {
11-
"build-extension:dev": "npm run clean-extensions && webpack --config build/webpack.dev.js",
12-
"build-extension:prod": "webpack --config build/webpack.prod.js",
13-
"compile": "babel index.js --config-file ./babel.config.js --extensions '.js'",
14-
"build:esm": "BABEL_ENV=esm npm run compile -- --out-dir dist/plugin/esm",
15-
"build:cjs": "BABEL_ENV=cjs npm run compile -- --out-dir dist/plugin/cjs",
16-
"build": "npm run clean-extensions && npm run clean-plugin && npm run build:esm && npm run build:cjs && npm run build-extension:prod",
17-
"clean-extensions": "rimraf dist/extensions",
18-
"clean-plugin": "rimraf dist/plugin",
19-
"start": "npm run build-extension:dev",
20-
"prepublishOnly": "npm run build"
11+
"compile:module": "babel module/index.js --config-file ./babel.config.js --extensions '.js'",
12+
"clean:module": "rimraf dist/module",
13+
"build:module:esm": "BABEL_ENV=esm npm run compile:module -- --out-dir dist/module/esm",
14+
"build:module:cjs": "BABEL_ENV=cjs npm run compile:module -- --out-dir dist/module/cjs",
15+
"build:module": "npm run clean:module && npm run build:module:esm && npm run build:module:cjs",
16+
"prepublishOnly": "npm run build:module"
2117
},
2218
"repository": {
2319
"type": "git",
@@ -29,6 +25,17 @@
2925
"url": "https://github.com/deeppatel234/react-context-devtool/issues"
3026
},
3127
"homepage": "https://github.com/deeppatel234/react-context-devtool#readme",
28+
"dependencies": {
29+
"codemirror": "^5.49.2",
30+
"error-stack-parser": "^2.0.6",
31+
"object-assign": "^4.1.1",
32+
"prismjs": "^1.17.1",
33+
"react": "^16.12.0",
34+
"react-diff-viewer": "^2.0.6",
35+
"react-dom": "^16.12.0",
36+
"react-json-view": "^1.19.1",
37+
"styled-components": "^4.4.1"
38+
},
3239
"devDependencies": {
3340
"@babel/cli": "^7.7.5",
3441
"@babel/core": "^7.7.5",
@@ -45,16 +52,5 @@
4552
"webpack": "^4.41.2",
4653
"webpack-cli": "^3.3.10",
4754
"webpack-merge": "^4.2.2"
48-
},
49-
"dependencies": {
50-
"codemirror": "^5.49.2",
51-
"error-stack-parser": "^2.0.6",
52-
"object-assign": "^4.1.1",
53-
"prismjs": "^1.17.1",
54-
"react": "^16.12.0",
55-
"react-diff-viewer": "^2.0.6",
56-
"react-dom": "^16.12.0",
57-
"react-json-view": "^1.19.1",
58-
"styled-components": "^4.4.1"
5955
}
6056
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)