diff --git a/package.json b/package.json index 369e287..387ab6a 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "test:typecheck": "vitest typecheck --config ./vitest.type.config.ts --run", "test:coverage": "npm test -- --reporter verbose --coverage", "test:e2e": "run-s test:e2e:*", + "test:e2e:browser": "cd playground/browser && node --test", "test:e2e:node": "cd playground/node && node --test", "test:e2e:deno": "cd playground/deno && deno task test", "test:e2e:bun": "cd playground/bun && npm run test", diff --git a/playground/browser/browser.test.mjs b/playground/browser/browser.test.mjs new file mode 100644 index 0000000..5e73495 --- /dev/null +++ b/playground/browser/browser.test.mjs @@ -0,0 +1,39 @@ +import assert from 'node:assert' +import test from 'node:test' +import { createServer } from 'node:http' +import playwright from 'playwright' +import handler from 'serve-handler' + +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) + +async function getText(page, selector, options) { + return page.locator(selector, options).innerText() +} + +test('browser integration test', async () => { + const server = createServer((request, response) => { + return handler(request, response) + }) + server.listen(3000, () => { + console.log('Running at http://localhost:3000') + }) + + const cleanup = () => { + server.close() + } + process.on('SIGINT', cleanup) + process.on('SIGTERM', cleanup) + process.on('SIGQUIT', cleanup) + process.on('uncaughtException', cleanup) + await sleep(2000) + + const browser = await playwright['chromium'].launch({ headless: true }) + const page = await browser.newPage({}) + await page.goto('http://localhost:3000') + const data = await getText(page, '#app') + + assert(data === `isLocale('en-US'): true`) + + browser.close() + server.close() +}) diff --git a/playground/browser/package.json b/playground/browser/package.json index 761f133..84aa8ac 100644 --- a/playground/browser/package.json +++ b/playground/browser/package.json @@ -4,9 +4,14 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "npx --no-install serve -l 3000" + "dev": "npx --no-install serve -l 3000", + "test": "node --test" }, "devDependencies": { - "serve": "^14.2.1" + "serve": "^14.2.1", + "serve-handler": "^6.1.5" + }, + "dependencies": { + "@intlify/utils": "npm:@intlify/utils-edge@latest" } } diff --git a/playground/node/package.json b/playground/node/package.json index f6c03c3..ada1d92 100644 --- a/playground/node/package.json +++ b/playground/node/package.json @@ -7,7 +7,7 @@ "test": "node --test" }, "dependencies": { - "@intlify/utils": "npm:@intlify/utils-edge@latest" + "@intlify/utils": "file:/Users/kazuya.kawaguchi/Projects/oss/intlify/utils/intlify-utils-0.10.0.tgz" }, "peerDependencies": { "typescript": "^5.2.2" diff --git a/scripts/replaceDeps.ts b/scripts/replaceDeps.ts index e538add..9442d1e 100644 --- a/scripts/replaceDeps.ts +++ b/scripts/replaceDeps.ts @@ -8,7 +8,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url)) type Platform = 'browser' | 'node' | 'deno' | 'bun' -async function replaceNodePlatform(platform: 'node' | 'bun', playgroundPath: string) { +async function replaceNodePlatform(platform: 'browser' | 'node' | 'bun', playgroundPath: string) { const utilsPath = resolve(__dirname, '..') const utilsPkg = await readPackageJSON(resolve(utilsPath, 'package.json')) const utilsTgzPath = resolve(utilsPath, `intlify-utils-${utilsPkg.version}.tgz`) @@ -22,6 +22,20 @@ async function replaceNodePlatform(platform: 'node' | 'bun', playgroundPath: str return true } +async function replaceBrowserPlatform(platform: 'browser', playgroundPath: string) { + if (!replaceNodePlatform(platform, playgroundPath)) { + return false + } + const codePath = resolve(playgroundPath, platform, 'index.js') + const code = await fs.readFile(codePath, 'utf-8') + const replacedCode = code.replace( + 'https://esm.sh/@intlify/utils', + './node_modules/@intlify/utils/dist/index.mjs', + ) + await fs.writeFile(codePath, replacedCode, 'utf-8') + return true +} + async function replaceDenoPlatform(playgroundPath: string) { const denoConfigPath = resolve(playgroundPath, 'deno', 'deno.jsonc') const denoConfig = JSON.parse(await fs.readFile(denoConfigPath, 'utf-8')) as { @@ -44,8 +58,12 @@ async function main() { if (!await replaceDenoPlatform(playgroundPath)) { console.error(`cannot replace '@intlify/utils' dependency on ${platform}`) } - } else { // for browser - // TODO: + } else if (platform === 'browser') { + if (!await replaceBrowserPlatform(platform, playgroundPath)) { + console.error(`cannot replace '@intlify/utils' dependency on ${platform}`) + } + } else { + throw new Error(`no support '${platform}' platform`) } } }