Skip to content

Commit

Permalink
feat: introduce @previewjs/config-helper-nextjs to simplify Next.js c…
Browse files Browse the repository at this point in the history
…onfiguration (#1156)
  • Loading branch information
fwouts committed Oct 31, 2022
1 parent 774cfb5 commit 439f435
Show file tree
Hide file tree
Showing 52 changed files with 424 additions and 954 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/release-components.yaml
Expand Up @@ -57,6 +57,14 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
- if: steps.checkConfigVersion.outputs.changed == 'true'
run: cd config && pnpm publish --no-git-checks --access public
- id: checkConfigHelperNextjsVersion
uses: EndBug/version-check@v2.1.1
with:
diff-search: true
file-name: ./config-helpers/nextjs/package.json
token: ${{ secrets.GITHUB_TOKEN }}
- if: steps.checkConfigHelperNextjsVersion.outputs.changed == 'true'
run: cd config-helpers/nextjs && pnpm publish --no-git-checks --access public
- id: checkCoreVersion
uses: EndBug/version-check@v2.1.1
with:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test-app-e2e.yaml
Expand Up @@ -68,9 +68,11 @@ jobs:
- uses: pnpm/action-setup@v2.2.4
with:
run_install: true
- run: pnpm turbo run build --scope="@previewjs/e2e-test-runner" --include-dependencies
- run: pnpm turbo run build
env:
SKIP_PREFLIGHT_CHECK: "true"
- run: pnpm install # necessary to link e2e-test-runner
- run: pnpm turbo run e2e-test --scope="@previewjs/app" --include-dependencies
- run: pnpm turbo run e2e-test
env:
GROUP_INDEX: ${{ matrix.group }}
GROUP_COUNT: 3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@ pro
*.vsix
.idea
.DS_Store
.svelte-kit
.turbo
.pnpm-debug.log
yarn-debug.log
Expand Down
Binary file modified app/tests/__screenshots__/linux/nextjs-11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/tests/__screenshots__/linux/nextjs-12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion app/tests/smoke-tests.spec.ts
Expand Up @@ -76,7 +76,7 @@ export const smokeTests = fs
await controller.props.editor.isReady();
}
await controller.takeScreenshot(
"#ready",
iframe,
path.join(
__dirname,
"..",
Expand All @@ -86,6 +86,7 @@ export const smokeTests = fs
`${appName}.png`
)
);
console.error("E");
}
);
},
Expand Down
21 changes: 21 additions & 0 deletions config-helpers/nextjs/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Zenc Labs Pty Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions config-helpers/nextjs/build.config.ts
@@ -0,0 +1,8 @@
export default {
entries: ["./src/index"],
rollup: {
emitCJS: true,
},
declaration: true,
clean: true,
};
41 changes: 41 additions & 0 deletions config-helpers/nextjs/package.json
@@ -0,0 +1,41 @@
{
"name": "@previewjs/config-helper-nextjs",
"version": "0.0.1",
"license": "MIT",
"author": {
"name": "François Wouts",
"email": "f@zenc.io"
},
"repository": {
"type": "git",
"url": "https://github.com/fwouts/previewjs"
},
"bugs": {
"url": "https://github.com/fwouts/previewjs/issues"
},
"homepage": "https://previewjs.com",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"scripts": {
"prepublish": "cd .. && pnpm turbo run build --scope=@previewjs/config-helper-nextjs --no-deps --include-dependencies",
"build": "tsc && unbuild"
},
"devDependencies": {
"@types/react": "18.0.24",
"next": "13.0.0",
"react": "18.2.0",
"unbuild": "0.9.1"
},
"peerDependencies": {
"next": "*",
"react": "*"
}
}
55 changes: 55 additions & 0 deletions config-helpers/nextjs/src/index.tsx
@@ -0,0 +1,55 @@
import * as NextImage from "next/image";
import * as NextRouter from "next/router";
import React from "react";

const OriginalNextImage = NextImage.default;

// Patch Image to disable optimisations within Preview.js.
Object.defineProperty(NextImage, "default", {
configurable: true,
value: (props: NextImage.ImageProps) => (
<OriginalNextImage {...props} unoptimized />
),
});

// Patch useRouter() to fake the router within Preview.js.
Object.defineProperty(NextRouter, "useRouter", {
configurable: true,
value: () => ({
locale: "en-US",
route: "/",
pathname: "/",
query: {},
asPath: "/",
push() {
return Promise.resolve(true);
},
replace() {
return Promise.resolve(true);
},
reload() {
// Do nothing.
},
back() {
// Do nothing.
},
prefetch() {
return Promise.resolve();
},
beforePopState() {
// Do nothing.
},
events: {
on() {
// Do nothing.
},
off() {
// Do nothing.
},
emit() {
// Do nothing.
},
},
isFallback: false,
}),
});
12 changes: 12 additions & 0 deletions config-helpers/nextjs/tsconfig.json
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "ES2020",
"moduleResolution": "node",
"rootDir": "src",
"outDir": "dist",
"esModuleInterop": true,
"jsx": "react"
},
"include": ["src"]
}
20 changes: 13 additions & 7 deletions e2e-test-runner/src/helpers/app-controller.ts
Expand Up @@ -127,9 +127,7 @@ export class AppController {
}
}

async takeScreenshot(waitForSelector: string, destinationPath: string) {
const preview = await this.previewIframe();
await preview.waitForSelector(waitForSelector);
async takeScreenshot(preview: playwright.Frame, destinationPath: string) {
preview.addStyleTag({
content: `
*,
Expand All @@ -154,10 +152,18 @@ export class AppController {
if (img.complete) {
return;
}
return new Promise((resolve) => {
img.addEventListener("load", resolve);
// If an image fails to load, ignore it.
img.addEventListener("error", resolve);
return new Promise<unknown>((resolve) => {
const observer = new IntersectionObserver((entries) => {
if (entries[0]?.isIntersecting) {
img.addEventListener("load", resolve);
// If an image fails to load, ignore it.
img.addEventListener("error", resolve);
} else {
resolve(null);
}
observer.unobserve(img);
});
observer.observe(img);
});
})
);
Expand Down

0 comments on commit 439f435

Please sign in to comment.