Skip to content

Commit 7708728

Browse files
authored
chore: add build scripts (#139)
1 parent 3fc11b5 commit 7708728

File tree

10 files changed

+328
-1721
lines changed

10 files changed

+328
-1721
lines changed

package-lock.json

Lines changed: 146 additions & 1653 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
{
22
"name": "testing-playground",
33
"version": "1.5.0",
4-
"description": "",
4+
"description": "Simple and complete DOM testing playground that encourage good testing practices.",
55
"scripts": {
66
"start": "run-s start:client",
7-
"start:client": "parcel src/index.html --open",
8-
"start:embed": "parcel src/embed.html --open",
9-
"build": "cross-env NODE_ENV=production run-s clean build:client build:server build:sw",
10-
"build:client": "parcel build src/index.html src/embed.js --dist-dir dist/client",
11-
"postbuild:client": "mkdir -p dist/client/public && cp _redirects dist/client/_redirects && cp public/favicon.ico dist/client/favicon.ico && cp public/icon.png dist/client/public/icon.png && cp -r .well-known dist/client/.well-known",
12-
"build:server": "cp -r src/lambda/ dist/server",
13-
"postbuild:server": "mkdir -p dist/server/server && mv dist/client/index.html dist/server/server/",
14-
"build:sw": "workbox generateSW",
15-
"lint": "eslint . --quiet --fix",
16-
"clean": "rimraf ./dist ./.cache ./.parcel-cache",
7+
"start:client": "node ./scripts/build-client",
8+
"build": "run-s clean build:*",
9+
"build:client": "cross-env NODE_ENV=production node ./scripts/build-client",
10+
"build:server": "cross-env NODE_ENV=production node ./scripts/build-lambda",
11+
"lint": "run-s lint:*",
12+
"lint:eslint": "eslint . --quiet --fix",
13+
"lint:prettier": "prettier . --write",
14+
"clean": "run-p clean:*",
15+
"clean:cache": "rimraf ./dist ./.cache ./.parcel-cache",
16+
"clean:dist": "rimraf ./dist",
1717
"ci:lint": "eslint .",
1818
"ci:test": "jest --ci",
1919
"ci:changelog": "node scripts/changelog.js",
2020
"test": "jest",
2121
"test:watch": "jest --watch",
22-
"prettier": "prettier . --write",
2322
"bump:patch": "npm version patch -m 'release: cut the %s release'",
2423
"bump:minor": "npm version minor -m 'release: cut the %s release'",
2524
"bump:major": "npm version major -m 'release: cut the %s release'",
@@ -50,6 +49,7 @@
5049
"@testing-library/jest-dom": "^5.9.0",
5150
"@testing-library/react": "^10.2.0",
5251
"@testing-library/user-event": "^11.2.0",
52+
"@types/fs-extra": "^9.0.1",
5353
"babel-eslint": "^10.1.0",
5454
"conventional-changelog": "^3.1.21",
5555
"conventional-changelog-config-spec": "^2.1.0",
@@ -58,6 +58,8 @@
5858
"eslint-config-prettier": "^6.11.0",
5959
"eslint-plugin-prettier": "^3.1.3",
6060
"eslint-plugin-react": "^7.20.0",
61+
"fs-extra": "^9.0.1",
62+
"get-port": "^5.1.1",
6163
"git-semver-tags": "^4.0.0",
6264
"husky": "^4.2.5",
6365
"jest": "^26.0.1",
@@ -71,7 +73,7 @@
7173
"prettier": "^2.0.5",
7274
"rimraf": "^3.0.2",
7375
"tailwindcss": "^1.4.6",
74-
"workbox-cli": "^5.1.3"
76+
"workbox-build": "^5.1.3"
7577
},
7678
"jest": {
7779
"verbose": true,

public/site.webmanifest

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,37 @@
99
"start_url": "/",
1010
"icons": [
1111
{
12-
"src": "../public/36-production.png",
12+
"src": "36-production.png",
1313
"sizes": "36x36",
1414
"type": "image/png",
1515
"density": "0.75"
1616
},
1717
{
18-
"src": "../public/48-production.png",
18+
"src": "48-production.png",
1919
"sizes": "48x48",
2020
"type": "image/png",
2121
"density": "1.0"
2222
},
2323
{
24-
"src": "../public/72-production.png",
24+
"src": "72-production.png",
2525
"sizes": "72x72",
2626
"type": "image/png",
2727
"density": "1.5"
2828
},
2929
{
30-
"src": "../public/96-production.png",
30+
"src": "96-production.png",
3131
"sizes": "96x96",
3232
"type": "image/png",
3333
"density": "2.0"
3434
},
3535
{
36-
"src": "../public/144-production.png",
36+
"src": "144-production.png",
3737
"sizes": "144x144",
3838
"type": "image/png",
3939
"density": "3.0"
4040
},
4141
{
42-
"src": "../public/192-production.png",
42+
"src": "192-production.png",
4343
"sizes": "192x192",
4444
"type": "image/png",
4545
"density": "4.0"

scripts/build-client.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const { copy, remove, readFile, writeFile, readdir } = require('fs-extra');
2+
const { resolve, join } = require('path');
3+
const { build } = require('./build');
4+
const { openInBrowser } = require('@parcel/utils');
5+
const workbox = require('workbox-build');
6+
7+
const hashRegExp = /\.[0-9a-fA-F]{8}\./;
8+
9+
const removeRevisionManifestTransform = async (manifestEntries) => ({
10+
manifest: manifestEntries.map((entry) =>
11+
hashRegExp.test(entry.url) ? { ...entry, revision: null } : entry,
12+
),
13+
});
14+
15+
const workboxConfig = {
16+
globDirectory: 'dist/client',
17+
globPatterns: ['**/*.{html,js,css,png,svg,jpg,gif,json,ico}'],
18+
swDest: 'dist/client/sw.js',
19+
clientsClaim: true,
20+
skipWaiting: true,
21+
manifestTransforms: [removeRevisionManifestTransform],
22+
ignoreURLParametersMatching: [/.*/],
23+
};
24+
25+
async function fixWebManifest({ dest }) {
26+
// browsers can't detect service-worker updates if the manifest changes name
27+
const htmlContent = await readFile(join(dest, 'index.html'), 'utf8');
28+
const [, manifestFilename] = htmlContent.match(
29+
/<link rel="manifest" href="\/?(site\.[0-9a-fA-F]{8}\.webmanifest)">/,
30+
);
31+
32+
// replace site.e5465fc8.webmanifest with manifest.json
33+
const replacer = new RegExp(manifestFilename, 'g');
34+
const newContent = htmlContent.replace(replacer, 'manifest.json', 'utf8');
35+
36+
// fix image paths in manifest.json
37+
const iconSrcHashTable = (await readdir(dest))
38+
.filter((file) => /\.png/.test(file))
39+
.reduce((index, file) => {
40+
index[file.replace(hashRegExp, '.')] = file;
41+
return index;
42+
}, {});
43+
44+
const manifest = JSON.parse(
45+
await readFile(join(dest, manifestFilename), 'utf8'),
46+
);
47+
48+
manifest.icons.forEach((icon) => {
49+
icon.src = iconSrcHashTable[icon.src];
50+
});
51+
52+
await Promise.all([
53+
// rename manifest file from site.e5465fc8.webmanifest to manifest.json
54+
remove(join(dest, manifestFilename)),
55+
// write updated html content referring to the renamed manifest
56+
writeFile(join(dest, 'index.html'), newContent),
57+
// write the fixed manifest json
58+
writeFile(
59+
join(dest, 'manifest.json'),
60+
JSON.stringify(manifest, '', ' '),
61+
'utf8',
62+
),
63+
]);
64+
}
65+
66+
async function main() {
67+
const dest = resolve('dist/client');
68+
await remove(dest);
69+
70+
const entries = ['src/index.html', 'src/embed.js'];
71+
72+
if (process.env.NODE_ENV === 'development') {
73+
entries.push('src/embed.html');
74+
}
75+
76+
const parcel = await build({
77+
entries,
78+
dest: 'dist/client',
79+
port: 1234,
80+
});
81+
82+
await fixWebManifest({ dest });
83+
84+
await workbox.generateSW(workboxConfig);
85+
86+
await Promise.all([
87+
copy('_redirects', join(dest, '_redirects')),
88+
copy('public/favicon.ico', join(dest, 'favicon.ico')),
89+
copy('public/icon.png', join(dest, 'icon.png')),
90+
copy('.well-known', join(dest, '.well-known')),
91+
]);
92+
93+
if (parcel.watching) {
94+
openInBrowser(`http://localhost:${parcel.config.serve.port}`);
95+
}
96+
}
97+
98+
main();

scripts/build-lambda.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { copy, remove } = require('fs-extra');
2+
const { resolve } = require('path');
3+
4+
async function main() {
5+
const dest = resolve('dist/server');
6+
await remove(dest);
7+
8+
await copy('src/lambda', dest);
9+
await copy('dist/client/index.html', 'dist/server/server/index.html');
10+
}
11+
12+
main();

scripts/build.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { ensureDir } = require('fs-extra');
2+
const Parcel = require('@parcel/core').default;
3+
const getPort = require('get-port');
4+
5+
async function build({
6+
entries,
7+
dest,
8+
port,
9+
serve = process.env.NODE_ENV !== 'production',
10+
}) {
11+
await ensureDir(dest);
12+
13+
const config = {
14+
entries,
15+
distDir: dest,
16+
};
17+
18+
if (serve !== false && port) {
19+
config.serve = { port: await getPort({ port }) };
20+
} else {
21+
config.mode = 'production';
22+
}
23+
24+
const parcel = new Parcel(config);
25+
26+
return new Promise((resolve, reject) => {
27+
const callback = (err, build) => {
28+
if (err) {
29+
return reject(err);
30+
}
31+
32+
resolve({ build, config, watching: serve });
33+
};
34+
35+
if (serve) {
36+
parcel.watch(callback);
37+
} else {
38+
parcel
39+
.run()
40+
.then((build) => resolve({ build, config, watching: false }))
41+
.catch(reject);
42+
}
43+
});
44+
}
45+
46+
module.exports = {
47+
build,
48+
};

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<link rel="icon" type="image/png" sizes="192x192" href="../public/192-production.png" />
1919
<link rel="icon" type="image/png" sizes="96x96" href="../public/96-production.png" />
20+
<link rel="icon" type="image/png" sizes="32x32" href="../public/48-production.png" />
2021
<link rel="icon" type="image/png" sizes="32x32" href="../public/32-production.png" />
2122
<link rel="icon" type="image/png" sizes="16x16" href="../public/16-production.png" />
2223

src/lambda/oembed/oembed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function handler(event, context, callback) {
5454
width: maxwidth,
5555
height: maxheight,
5656

57-
thumbnail_url: `${host}/public/icon.png`,
57+
thumbnail_url: `${host}/icon.png`,
5858
thumbnail_width: 512,
5959
thumbnail_height: 512,
6060

src/lib/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const isLoggingEnabled =
6464
process.env.NODE_ENV !== 'production' || !!localStorage.getItem('debug');
6565

6666
export function withLogging(reducerFn) {
67-
if (isLoggingEnabled) {
67+
if (!isLoggingEnabled) {
6868
return reducerFn;
6969
}
7070

workbox-config.js

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

0 commit comments

Comments
 (0)