wovn-nextjs 0.0.21 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/wovn-nextjs CHANGED
@@ -31,19 +31,27 @@ async function main() {
31
31
  if (command === 'build' && context.projectToken && context.hostname) {
32
32
  context.wovnJsData = await httpsRequest(`https://data.wovn.io/js_data/json/1/${context.projectToken}/?u=http%3A%2F%2F${context.hostname}`).then(body => JSON.parse(body.toString()))
33
33
  await overwriteTsxAndJsxFiles(context, undo) // Make sure to collect translatable texts
34
- await overwriteTsxAndJsxFiles(context, transform).then(() => reportToWovn(context))
34
+ await overwriteTsxAndJsxFiles(context, transform)
35
+ await reportToWovn(context)
35
36
  await overwriteInitializationProcess(context)
36
37
  await createMiddlewareFile(context)
37
38
  if (process.env.WOVN_DEBUG) {
38
39
  console.dir(context, {depth: null})
39
40
  }
40
- } else if (command === 'build-config' && context.projectToken && context.hostname) {
41
+ } else if (command === 'enable' && context.projectToken && context.hostname) {
41
42
  context.wovnJsData = await httpsRequest(`https://data.wovn.io/js_data/json/1/${context.projectToken}/?u=http%3A%2F%2F${context.hostname}`).then(body => JSON.parse(body.toString()))
43
+ await walkTsxAndJsx(context, transform)
42
44
  await overwriteInitializationProcess(context)
43
45
  await createMiddlewareFile(context)
44
46
  if (process.env.WOVN_DEBUG) {
45
47
  console.dir(context, {depth: null})
46
48
  }
49
+ } else if (command === 'report' && context.projectToken && context.hostname) {
50
+ await walkTsxAndJsx(context, transform)
51
+ await reportToWovn(context)
52
+ if (process.env.WOVN_DEBUG) {
53
+ console.dir(context, {depth: null})
54
+ }
47
55
  } else if (command === 'undo') {
48
56
  await overwriteTsxAndJsxFiles(context, undo)
49
57
  await undoIndexJavaScriptFiles()
@@ -212,6 +220,10 @@ function transform(context, source) {
212
220
  code = code.replaceAll(/^(import { *useRouter *} from ["'`]next\/navigation.*)/mg, 'import { useAppRouter as useRouter } from "wovn-nextjs" //$1 wovn-nextjs-command-comment')
213
221
  code = code.match(/^['"]use /) ? code.replace(/\n/, '\n' + importer) : importer + code
214
222
  }
223
+ for (const text of getTextsFromTokens(tokens)) {
224
+ context.uvs.set(text, -1)
225
+ component.uvs.add(text)
226
+ }
215
227
  context.components.push(component)
216
228
  return code
217
229
  }
@@ -225,6 +237,14 @@ function undo(context, code) {
225
237
  return code
226
238
  }
227
239
 
240
+ function getTextsFromTokens(tokens) {
241
+ function unquote(s) {
242
+ return s[0] === '"' ? s.slice(1, -1).replace(/\\"/g, '"') :
243
+ s[0] === "'" ? s.slice(1, -1).replace(/\\'/g, "'") : '';
244
+ }
245
+ return tokens.filter((t, i) => i >=2 && t.type === 'String' && (t.value[0] === '"' || t.value[0] === "'") && tokens[i-1].value === '(' && tokens[i-2].value === 't').map(t => unquote(t.value)).filter(s => s.length)
246
+ }
247
+
228
248
  const appendMarker = '\n// wovn-nextjs-command-appended'
229
249
 
230
250
  async function overwriteInitializationProcess(context) {
@@ -261,6 +281,17 @@ async function overwriteJavaScriptFiles(filename, convert) {
261
281
  }
262
282
 
263
283
  async function overwriteTsxAndJsxFiles(context, convert) {
284
+ const promises = []
285
+ await walkTsxAndJsx(async (context, source) => {
286
+ const convertedSource = convert(context, source)
287
+ if (source !== convertedSource) {
288
+ promises.push(fsp.writeFile(context.pathname, convertedSource))
289
+ }
290
+ })
291
+ return Promise.all(promises)
292
+ }
293
+
294
+ async function walkTsxAndJsx(context, callback){
264
295
  async function* targetFiles(dir) {
265
296
  const dirents = await fsp.readdir(dir, { withFileTypes: true });
266
297
  for (const dirent of dirents) {
@@ -274,17 +305,12 @@ async function overwriteTsxAndJsxFiles(context, convert) {
274
305
  }
275
306
  }
276
307
  }
277
- const promises = []
278
308
  const pwd = process.env.PWD
279
309
  for await (const pathname of targetFiles(buildDir)) {
280
310
  context.pathname = pathname.slice(pwd.length + buildDir.length)
281
311
  const source = await fsp.readFile(pathname, 'utf-8')
282
- const convertedSource = convert(context, source)
283
- if (source !== convertedSource) {
284
- promises.push(fsp.writeFile(pathname, convertedSource))
285
- }
312
+ callback(context, source)
286
313
  }
287
- return Promise.all(promises)
288
314
  }
289
315
 
290
316
  async function reportToWovn(context) {
@@ -307,6 +333,9 @@ async function reportToWovn(context) {
307
333
  function httpsRequest(url, method='get', body='') {
308
334
  return new Promise((resolve, reject) => {
309
335
  try {
336
+ if (process.env.WOVN_DEBUG) {
337
+ console.dir({method, url, body}, {depth: null})
338
+ }
310
339
  const req = https.request(url, { method }, res => {
311
340
  if (res.statusCode >= 200 && res.statusCode < 300) {
312
341
  const chunks = []
package/dist/cjs/index.js CHANGED
@@ -97,10 +97,7 @@ function usePageRouter() {
97
97
  case 'replace':
98
98
  return (url, as, options) => target.replace(link(url), as ? link(as) : as, options);
99
99
  case 'prefetch':
100
- return (url, asPath, options) => {
101
- console.log(url, asPath, target);
102
- return target.prefetch(link(url), asPath ? link(asPath) : asPath, options);
103
- };
100
+ return (url, asPath, options) => target.prefetch(link(url), asPath ? link(asPath) : asPath, options);
104
101
  default:
105
102
  return Reflect.get(target, prop, receiver);
106
103
  }
package/dist/esm/index.js CHANGED
@@ -86,10 +86,7 @@ export function usePageRouter() {
86
86
  case 'replace':
87
87
  return (url, as, options) => target.replace(link(url), as ? link(as) : as, options);
88
88
  case 'prefetch':
89
- return (url, asPath, options) => {
90
- console.log(url, asPath, target);
91
- return target.prefetch(link(url), asPath ? link(asPath) : asPath, options);
92
- };
89
+ return (url, asPath, options) => target.prefetch(link(url), asPath ? link(asPath) : asPath, options);
93
90
  default:
94
91
  return Reflect.get(target, prop, receiver);
95
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wovn-nextjs",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Localize the Next.js web application without changing the code",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",