Skip to content

Commit

Permalink
Merge pull request #23 from isinstock/puppeteer
Browse files Browse the repository at this point in the history
Puppeteer
  • Loading branch information
dewski authored Nov 22, 2023
2 parents 60bd335 + 05079c2 commit f08ebc3
Show file tree
Hide file tree
Showing 29 changed files with 1,826 additions and 985 deletions.
44 changes: 44 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Use the specific version of the image that suits your needs
FROM mcr.microsoft.com/devcontainers/typescript-node

# Install Puppeteer dependencies
RUN apt-get update && apt-get install -y \
ca-certificates \
chromium \
fonts-liberation \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libc6 \
libcairo2 \
libcups2 \
libdbus-1-3 \
libexpat1 \
libfontconfig1 \
libgbm1 \
libgcc1 \
libglib2.0-0 \
libgtk-3-0 \
libnspr4 \
libnss3 \
libpango-1.0-0 \
libpangocairo-1.0-0 \
libstdc++6 \
libx11-6 \
libx11-xcb1 \
libxcb1 \
libxcomposite1 \
libxcursor1 \
libxdamage1 \
libxext6 \
libxfixes3 \
libxi6 \
libxrandr2 \
libxrender1 \
libxss1 \
libxtst6 \
lsb-release \
wget \
xdg-utils \
xvfb \
&& rm -rf /var/lib/apt/lists/*
24 changes: 21 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
{
"image": "mcr.microsoft.com/devcontainers/typescript-node",
"build": {
"dockerfile": "Dockerfile",
"args": {
"--platform": "linux/amd64"
}
},
"capAdd": ["SYS_ADMIN"],
"containerEnv": {
"DISPLAY": ":99",
"PUPPETEER_EXECUTABLE_PATH": "/usr/bin/chromium",
"PUPPETEER_SKIP_CHROMIUM_DOWNLOAD": "true"
},
"postStartCommand": "Xvfb :99 -ac -screen 0 1280x720x16 -nolisten tcp -nolisten unix &",
"customizations": {
"vscode": {
"extensions": ["dbaeumer.vscode-eslint"]
"extensions": [
"bradlc.vscode-tailwindcss",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"GitHub.copilot",
"Orta.vscode-jest"
]
}
},
"forwardPorts": [8888]
"forwardPorts": [21222]
}
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"project": ["./tsconfig.json"]
},
"env": {
"es6": true
"es6": true,
"jest": true
},
"plugins": [
"@typescript-eslint",
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- run: npm ci
- run: npm run build -- --production
- run: npm run test
8 changes: 7 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "bradlc.vscode-tailwindcss", "GitHub.copilot"]
"recommendations": [
"bradlc.vscode-tailwindcss",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"GitHub.copilot",
"Orta.vscode-jest",
]
}
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
},
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.code-search": true,
"**/dist": true
},
"eslint.validate": ["javascript", "javascriptreact", "html", "typescript", "typescriptreact"],
Expand Down
18 changes: 17 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
{
"label": "install",
"type": "shell",
"group": "build",
"command": "npm",
"args": ["install"]
},
{
"label": "update",
"type": "shell",
"group": "build",
"command": "npm",
"args": ["update"]
},
{
"label": "test",
"label": "run tests",
"type": "shell",
"group": "build",
"command": "npm",
"args": ["run", "test"]
},
{
"label": "run tests on host",
"type": "process",
"group": "build",
"command": "CHROME_DEVTOOLS_ID=${input:chromeDevtoolsID} npm run test",
},
{
"label": "development - build",
"type": "shell",
Expand Down Expand Up @@ -50,5 +59,12 @@
"command": "npm",
"args": ["run", "watch", "--", "--production"]
}
],
"inputs": [
{
"id": "chromeDevtoolsID",
"description": "Chrome Devtools ID: e3c7f5e3-c1e5-4b81-bda1-4633c5c5f7f7",
"type": "promptString"
},
]
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ npm run watch -- --production
- ✅ JSON-LD
- ✅ RDFa
- ✅ Microdata

## Testing

### Google Chrome

- Run `bin/launch-test-chrome` to run the test suite in Google Chrome.
- Open the profile which has the installed extension.
- If you do not have the extension installed:
- run `npm run build -- --production` to build the extension
- Load it into Google Chrome by visiting `chrome://extensions`
- You should see output similar to `DevTools listening on ws://127.0.0.1:21222/devtools/browser/e3c7f5e3-c1e5-4b81-bda1-4633c5c5f7f7`, copy the UUID portion of the URL which would be `e3c7f5e3-c1e5-4b81-bda1-4633c5c5f7f7` in this example.
- Run `bin/integration-test "e3c7f5e3-c1e5-4b81-bda1-4633c5c5f7f7"`
2 changes: 2 additions & 0 deletions bin/integration-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#/usr/bin sh
CHROME_DEVTOOLS_ID="$1" npm run test
2 changes: 2 additions & 0 deletions bin/launch-test-chrome
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#/usr/bin sh
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=21222
70 changes: 30 additions & 40 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const build = require('esbuild').build
const copy = require('esbuild-plugin-copy').copy
const postCssPlugin = require('esbuild-style-plugin')
const postcss = require('postcss')
const pkg = require('./package.json')
const fs = require('fs')
const tailwindcss = require('tailwindcss')
const autoprefixer = require('autoprefixer')

const isProduction = process.argv.includes('--production')
const watch = process.argv.includes('--watch')
Expand Down Expand Up @@ -41,15 +43,24 @@ const copyFirefoxManifestPlugin = {
},
}

// Chrome
build({
const cssAsStringPlugin = {
name: 'css-as-string',
setup(build) {
build.onLoad({filter: /\.css$/}, async args => {
const from = args.path
const cssContent = await fs.promises.readFile(from, 'utf8')
const result = await postcss([tailwindcss, autoprefixer]).process(cssContent, {from})
return {
contents: `export default ${JSON.stringify(result.css)}`,
loader: 'js',
}
})
},
}

const config = {
logLevel: 'info',
entryPoints: [
'./src/background.ts',
'./src/content_scripts/content_script.tsx',
'./src/elements/isinstock-button/style.css',
],
outdir: 'dist/chrome',
entryPoints: ['./src/background.ts', './src/content_scripts/content_script.tsx'],
bundle: true,
sourcemap: !isProduction,
watch,
Expand All @@ -65,12 +76,15 @@ build({
'.png': 'dataurl',
'.svg': 'dataurl',
},
plugins: [cssAsStringPlugin],
}

// Chrome
build({
...config,
outdir: 'dist/chrome',
plugins: [
postCssPlugin({
postcss: {
plugins: [require('tailwindcss'), require('autoprefixer')],
},
}),
...config.plugins,
copy({
resolveFrom: 'cwd',
assets: {
Expand All @@ -84,34 +98,10 @@ build({

// Firefox
build({
logLevel: 'info',
entryPoints: [
'./src/background.ts',
'./src/content_scripts/content_script.tsx',
'./src/elements/isinstock-button/style.css',
],
...config,
outdir: 'dist/firefox',
bundle: true,
sourcemap: !isProduction,
watch,
minify: isProduction,
target: ['ios15', 'chrome100', 'edge100', 'firefox100', 'safari15'],
define: {
VERSION: `"${pkg.version}"`,
ISINSTOCK_URL: isProduction ? '"https://isinstock.com"' : '"http://localhost:3000"',
CHROME_EXTENSION_ID: '"bnglflgcpflggbpbcbpgeaknekceeojd"',
},
drop: isProduction ? ['console'] : [],
loader: {
'.png': 'dataurl',
'.svg': 'dataurl',
},
plugins: [
postCssPlugin({
postcss: {
plugins: [require('tailwindcss'), require('autoprefixer')],
},
}),
...config.plugins,
copy({
resolveFrom: 'cwd',
assets: {
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testEnvironment: 'jsdom',
roots: ['src'],
testTimeout: 30000,
transform: {
'^.+\\.ts$': ['ts-jest', {tsconfig: 'tsconfig.test.json'}],
},
Expand Down
Loading

0 comments on commit f08ebc3

Please sign in to comment.