Skip to content

Commit

Permalink
Add TypeScript support on CLI (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Feb 7, 2020
1 parent 0490387 commit ad8c25e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions cli/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ function isNextInternal(pagePath) {
return pagePath.startsWith(`${currentPagesDir}/_`)
}

function clearPageExt(page) {
const rgx = /(\/index.jsx)|(\/index.js)|(\/index.tsx)|(\/index.ts)|(\.jsx)|(\.js)|(\.tsx)|(\.ts)/gm

return page.replace(rgx, '')
}

/**
* STEP 2: Read each page namespaces
*/
function readPageNamespaces(langs) {
readDirR(currentPagesDir).forEach(page => {
const pageId =
page
.replace(currentPagesDir, '')
.replace(/(\/index.js)|(\/index.jsx)|(\.js)|(\.jsx)/gm, '') || '/'
const pageId = clearPageExt(page.replace(currentPagesDir, '')) || '/'

const namespaces = [...(pages['*'] || []), ...(pages[pageId] || [])]

Expand All @@ -66,11 +69,16 @@ function readPageNamespaces(langs) {
* STEP 3: Build page in each lang path
*/
function getPageTemplate(prefix, page, lang, namespaces) {
const isTypeScript = page.endsWith('.ts') || page.endsWith('.tsx')

return `import I18nProvider from 'next-translate/I18nProvider'
import React from 'react'
import C from '${prefix}/${page}'
import C from '${prefix}/${clearPageExt(page)}'
${namespaces
.map((ns, i) => `import ns${i} from '${prefix}/${localesPath}/${lang}/${ns}'`)
.map(
(ns, i) =>
`import ns${i} from '${prefix}/${localesPath}/${lang}/${ns}.json'`
)
.join('\n')}
const namespaces = { ${namespaces
Expand All @@ -85,7 +93,7 @@ export default function Page(p){
)
}
Page.getInitialProps = C.getInitialProps
Page.getInitialProps = ${isTypeScript ? '(C as any)' : 'C'}.getInitialProps
`
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.5.0",
"version": "0.6.0",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit ad8c25e

Please sign in to comment.