astro-i18next引起的无法编译的问题

Astro 101

这个问题困扰了我两天,根本原因是i18next插件没有同步删除生成出来的language code命名的路由文件夹导致的错误,在dev模式下都是正常的,但是build无论是SSR还是SSG都不行。

具体过程是,原本是一个article目录下存放了[slug].astro文件,后来觉得用动态路由做SEO更友好,于是把article重命名为[category],但是在用npx astro-i18next generate命令生成出语言相对应子目录的时候它直接生成出[category]目录,而原有的article目录依旧存在,于是就有了下面的错误,翻遍全网都没解决,最后逐一排查才发现在这个地方翻车了。

另外关于astro-18next的问题很多,几个吐槽的地方是:

1. 每次使用npx astro-i18next generate它会整段删除注释/**/中的内容,这个务必要注意

2. 如果你的i18next引入方式是两行,类似于这样

import { t, changeLanguage } from "i18next";
import i18next from "i18next";

在使用了generate命令后它会变成

import { t, changeLanguage } from "i18next";
import i18next, {changeLanguage} from "i18next";

这样一来就出现了重复引入的提示,所以还得这样写:

import i18next, { t, changeLanguage } from "i18next";

3. ssg渲染在dev模式下无法直接预览效果

build的内容是没问题的,但是在yarn dev模式下切换语言会出现语言没有变化的情况。

最后是出错的LOG:

X [ERROR] Could not resolve "url"

    dist/$server_build/chunks/pages/_slug_.astro.775b480a.mjs:2:22:
      2 │ import * as url2 from 'url';
        ╵                       ~~~~~

  The package "url" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "module"

    dist/$server_build/chunks/pages/_slug_.astro.775b480a.mjs:7:20:
      7 │ import module2 from 'module';
        ╵                     ~~~~~~~~

  The package "module" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    dist/$server_build/chunks/pages/_slug_.astro.775b480a.mjs:8:18:
      8 │ import path2 from 'path';
        ╵                   ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/tsm/require.js:1:41:
      1 │ "use strict";const{readFileSync}=require("fs"),{extname}=require("path"),tsm=require("./utils"),loadJS=require.extensions[".js"];let esbuild,env=tsm.$defaults("cjs"),uconf=env.file&&require(env.file),config=tsm.$finaliz... 
        ╵                                          ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/@proload/core/lib/esm/index.mjs:2:48:
      2 │ import { join, dirname, extname, resolve } from "path";
        ╵                                                 ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/tsm/require.js:1:65:
      1 │ "use strict";const{readFileSync}=require("fs"),{extname}=require("path"),tsm=require("./utils"),loadJS=require.extensions[".js"];let esbuild,env=tsm.$defaults("cjs"),uconf=env.file&&require(env.file),config=tsm.$finaliz... 
        ╵                                                                  ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/i18next-fs-backend/esm/fs.cjs:2:18:
      2 │   var f = require('fs')
        │                   ~~~~
        ╵                   "./fs"

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/@proload/core/lib/esm/index.mjs:5:52:
      5 │ import { existsSync, readdir, readFile, stat } from "fs";
        ╵                                                     ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/tsm/utils.js:1:36:
      1 │ "use strict";const{resolve}=require("path"),{existsSync}=require("fs");exports.$defaults=function(l){let{FORCE_COLOR:e,NO_COLOR:s,NODE_DISABLE_COLORS:o,TERM:t}=process.env,i=process.argv.slice(2),n=new Set(i),f=n.has("-... 
        ╵                                     ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "util"

    node_modules/@proload/core/lib/esm/index.mjs:6:26:
      6 │ import { promisify } from "util";
        ╵                           ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/i18next-fs-backend/esm/path.cjs:2:21:
      2 │   var path = require('path')
        │                      ~~~~~~
        ╵                      "./path"

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/tsm/utils.js:1:65:
      1 │ "use strict";const{resolve}=require("path"),{existsSync}=require("fs");exports.$defaults=function(l){let{FORCE_COLOR:e,NO_COLOR:s,NODE_DISABLE_COLORS:o,TERM:t}=process.env,i=process.argv.slice(2),n=new Set(i),f=n.has("-... 
        ╵                                                                  ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1672:17:
      1672 │ var fs = require("fs");
           ╵                  ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "module"

    node_modules/@proload/core/lib/esm/index.mjs:7:30:
      7 │ import { createRequire } from "module";
        ╵                               ~~~~~~~~

  The package "module" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "os"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1673:17:
      1673 │ var os = require("os");
           ╵                  ~~~~

  The package "os" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/escalade/dist/index.mjs:1:33:
      1 │ import { dirname, resolve } from 'path';
        ╵                                  ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "module"

    node_modules/@proload/core/lib/esm/requireOrImport.mjs:2:30:
      2 │ import { createRequire } from 'module';
        ╵                               ~~~~~~~~

  The package "module" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1674:19:
      1674 │ var path = require("path");
           ╵                    ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/escalade/dist/index.mjs:2:30:
      2 │ import { readdir, stat } from 'fs';
        ╵                               ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "url"

    node_modules/@proload/core/lib/esm/requireOrImport.mjs:3:30:
      3 │ import { pathToFileURL } from 'url';
        ╵                               ~~~~~

  The package "url" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "util"

    node_modules/escalade/dist/index.mjs:3:26:
      3 │ import { promisify } from 'util';
        ╵                           ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "child_process"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1856:28:
      1856 │ var child_process = require("child_process");
           ╵                             ~~~~~~~~~~~~~~~

  The package "child_process" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "crypto"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1857:21:
      1857 │ var crypto = require("crypto");
           ╵                      ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "tty"

    node_modules/tsm/node_modules/esbuild/lib/main.js:1861:18:
      1861 │ var tty = require("tty");
           ╵                   ~~~~~

  The package "tty" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

 error   Could not resolve "url"
  File:
    dist/$server_build/chunks/pages/_slug_.astro.775b480a.mjs:2:22
  Code:
    1 | import i18next, { t, changeLanguage } from 'i18next';
    > 2 | import * as url2 from 'url';
        |                      ^
      3 | import '@proload/core';
      4 | import '@proload/plugin-tsm';
      5 | import { c as createAstro, a as createComponent, r as renderTemplate, m as maybeRenderHead, b as renderSlot, d as renderComponent, u as unescapeHTML, F as Fragment, s as spreadAttributes, e as addAttribute, f as renderHead } from '../astro.fe20a6e7.mjs';
error Command failed with exit code 1.

Post Comment