next-translate-plugin 2.0.0 → 2.0.1

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/lib/cjs/index.js CHANGED
@@ -48,9 +48,9 @@ var possiblePageDirs = __spreadArray([
48
48
  function nextTranslate(nextConfig) {
49
49
  if (nextConfig === void 0) { nextConfig = {}; }
50
50
  var basePath = pkgDir();
51
- var translationDir = path_1.default.resolve(path_1.default.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
51
+ var dir = path_1.default.resolve(path_1.default.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
52
52
  var nextConfigI18n = nextConfig.i18n || {};
53
- var _a = require(path_1.default.join(translationDir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
53
+ var _a = require(path_1.default.join(dir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
54
54
  var nextConfigWithI18n = __assign(__assign({}, nextConfig), { i18n: {
55
55
  locales: locales,
56
56
  defaultLocale: defaultLocale,
@@ -62,20 +62,20 @@ function nextTranslate(nextConfig) {
62
62
  if (!pagesInDir) {
63
63
  for (var _i = 0, possiblePageDirs_1 = possiblePageDirs; _i < possiblePageDirs_1.length; _i++) {
64
64
  var possiblePageDir = possiblePageDirs_1[_i];
65
- if (fs_1.default.existsSync(path_1.default.join(basePath, possiblePageDir))) {
65
+ if (fs_1.default.existsSync(path_1.default.join(dir, possiblePageDir))) {
66
66
  pagesInDir = possiblePageDir;
67
67
  isAppDirNext13 = appDirNext13.includes(possiblePageDir);
68
68
  break;
69
69
  }
70
70
  }
71
71
  }
72
- if (!pagesInDir || !fs_1.default.existsSync(path_1.default.join(basePath, pagesInDir))) {
72
+ if (!pagesInDir || !fs_1.default.existsSync(path_1.default.join(dir, pagesInDir))) {
73
73
  return nextConfigWithI18n;
74
74
  }
75
- var pagesPath = path_1.default.join(basePath, pagesInDir);
75
+ var pagesPath = path_1.default.join(dir, pagesInDir);
76
76
  var app = fs_1.default.readdirSync(pagesPath).find(function (page) { return page.startsWith('_app.'); });
77
77
  if (app) {
78
- var appPkg = (0, utils_1.parseFile)(basePath, path_1.default.join(pagesPath, app));
78
+ var appPkg = (0, utils_1.parseFile)(dir, path_1.default.join(pagesPath, app));
79
79
  var defaultExport = (0, utils_1.getDefaultExport)(appPkg);
80
80
  if (defaultExport) {
81
81
  var isGetInitialProps = (0, utils_1.hasStaticName)(appPkg, defaultExport, 'getInitialProps');
@@ -92,7 +92,7 @@ function nextTranslate(nextConfig) {
92
92
  config.module = {};
93
93
  if (!config.module.rules)
94
94
  config.module.rules = [];
95
- config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path_1.default.resolve(translationDir) });
95
+ config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path_1.default.resolve(dir) });
96
96
  if (!loader)
97
97
  return config;
98
98
  config.module.rules.push({
@@ -6,7 +6,8 @@ var defaultDynamicExport = "export const dynamic = 'force-dynamic';";
6
6
  function templateAppDir(pagePkg, _a) {
7
7
  var _b = _a === void 0 ? {} : _a, _c = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _c === void 0 ? false : _c, _d = _b.pageNoExt, pageNoExt = _d === void 0 ? '/' : _d, _e = _b.normalizedResourcePath, normalizedResourcePath = _e === void 0 ? '' : _e, _f = _b.normalizedPagesPath, normalizedPagesPath = _f === void 0 ? '' : _f;
8
8
  var code = pagePkg.getCode();
9
- var isClientCode = clientLine.some(function (line) { return code.startsWith(line); });
9
+ var codeWithoutComments = (0, utils_1.removeCommentsFromCode)(code).trim();
10
+ var isClientCode = clientLine.some(function (line) { return codeWithoutComments.startsWith(line); });
10
11
  var isPage = pageNoExt.endsWith('/page') && normalizedResourcePath.startsWith(normalizedPagesPath);
11
12
  if (!isPage && !isClientCode)
12
13
  return code;
package/lib/cjs/utils.js CHANGED
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.interceptExport = exports.isNotExportModifier = exports.hasHOC = exports.isPageToIgnore = exports.hasStaticName = exports.getStaticName = exports.hasExportName = exports.getDefaultExport = exports.getNamedExport = exports.resolveIdentifier = exports.resolveParenthesis = exports.getImportedNames = exports.getSymbol = exports.parseCode = exports.parseFile = exports.getFilePkg = exports.getTsCompilerOptions = exports.overwriteLoadLocales = exports.getDefaultAppJs = exports.defaultLoader = void 0;
26
+ exports.removeCommentsFromCode = exports.interceptExport = exports.isNotExportModifier = exports.hasHOC = exports.isPageToIgnore = exports.hasStaticName = exports.getStaticName = exports.hasExportName = exports.getDefaultExport = exports.getNamedExport = exports.resolveIdentifier = exports.resolveParenthesis = exports.getImportedNames = exports.getSymbol = exports.parseCode = exports.parseFile = exports.getFilePkg = exports.getTsCompilerOptions = exports.overwriteLoadLocales = exports.getDefaultAppJs = exports.defaultLoader = void 0;
27
27
  var typescript_1 = __importDefault(require("typescript"));
28
28
  var specFileOrFolderRgx = /(__mocks__|__tests__)|(\.(spec|test)\.(tsx|ts|js|jsx)$)/;
29
29
  exports.defaultLoader = '(l, n) => import(`@next-translate-root/locales/${l}/${n}`).then(m => m.default)';
@@ -350,3 +350,7 @@ function interceptExport(filePkg, exportName, defaultLocalName) {
350
350
  return finalLocalName;
351
351
  }
352
352
  exports.interceptExport = interceptExport;
353
+ function removeCommentsFromCode(code) {
354
+ return code.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '');
355
+ }
356
+ exports.removeCommentsFromCode = removeCommentsFromCode;
package/lib/esm/index.js CHANGED
@@ -43,9 +43,9 @@ var possiblePageDirs = __spreadArray([
43
43
  function nextTranslate(nextConfig) {
44
44
  if (nextConfig === void 0) { nextConfig = {}; }
45
45
  var basePath = pkgDir();
46
- var translationDir = path.resolve(path.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
46
+ var dir = path.resolve(path.relative(basePath, process.env.NEXT_TRANSLATE_PATH || '.'));
47
47
  var nextConfigI18n = nextConfig.i18n || {};
48
- var _a = require(path.join(translationDir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
48
+ var _a = require(path.join(dir, 'i18n')), _b = _a.locales, locales = _b === void 0 ? nextConfigI18n.locales || [] : _b, _c = _a.defaultLocale, defaultLocale = _c === void 0 ? nextConfigI18n.defaultLocale || 'en' : _c, _d = _a.domains, domains = _d === void 0 ? nextConfigI18n.domains : _d, _e = _a.localeDetection, localeDetection = _e === void 0 ? nextConfigI18n.localeDetection : _e, _f = _a.loader, loader = _f === void 0 ? true : _f, pagesInDir = _a.pagesInDir, restI18n = __rest(_a, ["locales", "defaultLocale", "domains", "localeDetection", "loader", "pagesInDir"]);
49
49
  var nextConfigWithI18n = __assign(__assign({}, nextConfig), { i18n: {
50
50
  locales: locales,
51
51
  defaultLocale: defaultLocale,
@@ -57,20 +57,20 @@ function nextTranslate(nextConfig) {
57
57
  if (!pagesInDir) {
58
58
  for (var _i = 0, possiblePageDirs_1 = possiblePageDirs; _i < possiblePageDirs_1.length; _i++) {
59
59
  var possiblePageDir = possiblePageDirs_1[_i];
60
- if (fs.existsSync(path.join(basePath, possiblePageDir))) {
60
+ if (fs.existsSync(path.join(dir, possiblePageDir))) {
61
61
  pagesInDir = possiblePageDir;
62
62
  isAppDirNext13 = appDirNext13.includes(possiblePageDir);
63
63
  break;
64
64
  }
65
65
  }
66
66
  }
67
- if (!pagesInDir || !fs.existsSync(path.join(basePath, pagesInDir))) {
67
+ if (!pagesInDir || !fs.existsSync(path.join(dir, pagesInDir))) {
68
68
  return nextConfigWithI18n;
69
69
  }
70
- var pagesPath = path.join(basePath, pagesInDir);
70
+ var pagesPath = path.join(dir, pagesInDir);
71
71
  var app = fs.readdirSync(pagesPath).find(function (page) { return page.startsWith('_app.'); });
72
72
  if (app) {
73
- var appPkg = parseFile(basePath, path.join(pagesPath, app));
73
+ var appPkg = parseFile(dir, path.join(pagesPath, app));
74
74
  var defaultExport = getDefaultExport(appPkg);
75
75
  if (defaultExport) {
76
76
  var isGetInitialProps = hasStaticName(appPkg, defaultExport, 'getInitialProps');
@@ -87,7 +87,7 @@ function nextTranslate(nextConfig) {
87
87
  config.module = {};
88
88
  if (!config.module.rules)
89
89
  config.module.rules = [];
90
- config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path.resolve(translationDir) });
90
+ config.resolve.alias = __assign(__assign({}, (config.resolve.alias || {})), { '@next-translate-root': path.resolve(dir) });
91
91
  if (!loader)
92
92
  return config;
93
93
  config.module.rules.push({
@@ -1,10 +1,11 @@
1
- import { interceptExport, overwriteLoadLocales, getNamedExport } from "./utils";
1
+ import { interceptExport, overwriteLoadLocales, getNamedExport, removeCommentsFromCode } from "./utils";
2
2
  var clientLine = ['"use client"', "'use client'"];
3
3
  var defaultDynamicExport = "export const dynamic = 'force-dynamic';";
4
4
  export default function templateAppDir(pagePkg, _a) {
5
5
  var _b = _a === void 0 ? {} : _a, _c = _b.hasLoadLocaleFrom, hasLoadLocaleFrom = _c === void 0 ? false : _c, _d = _b.pageNoExt, pageNoExt = _d === void 0 ? '/' : _d, _e = _b.normalizedResourcePath, normalizedResourcePath = _e === void 0 ? '' : _e, _f = _b.normalizedPagesPath, normalizedPagesPath = _f === void 0 ? '' : _f;
6
6
  var code = pagePkg.getCode();
7
- var isClientCode = clientLine.some(function (line) { return code.startsWith(line); });
7
+ var codeWithoutComments = removeCommentsFromCode(code).trim();
8
+ var isClientCode = clientLine.some(function (line) { return codeWithoutComments.startsWith(line); });
8
9
  var isPage = pageNoExt.endsWith('/page') && normalizedResourcePath.startsWith(normalizedPagesPath);
9
10
  if (!isPage && !isClientCode)
10
11
  return code;
package/lib/esm/utils.js CHANGED
@@ -325,3 +325,6 @@ export function interceptExport(filePkg, exportName, defaultLocalName) {
325
325
  }
326
326
  return finalLocalName;
327
327
  }
328
+ export function removeCommentsFromCode(code) {
329
+ return code.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '');
330
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-translate-plugin",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Tiny and powerful i18n plugin to translate your Next.js pages.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -48,7 +48,8 @@
48
48
  "test": "cross-env NODE_ENV=test jest --env=jsdom",
49
49
  "test:coverage": "cross-env NODE_ENV=test jest --env=jsdom --coverage",
50
50
  "test:watch": "cross-env NODE_ENV=test jest --env=jsdom --watch",
51
- "tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node build-packages.js"
51
+ "tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node build-packages.js",
52
+ "postinstall": "node postinstall-message.js"
52
53
  },
53
54
  "dependencies": {
54
55
  "typescript": "4.5.2"
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "templateappdir",
3
+ "private": true,
4
+ "main": "../lib/cjs/templateAppDir.js",
5
+ "module": "../lib/esm/templateAppDir.js",
6
+ "types": "../templateAppDir.d.ts"
7
+ }
package/utils.d.ts CHANGED
@@ -20,3 +20,4 @@ export declare function isPageToIgnore(pageFilePath: string): boolean;
20
20
  export declare function hasHOC(filePkg: ParsedFilePkg): boolean;
21
21
  export declare function isNotExportModifier(modifier: ts.Modifier): boolean;
22
22
  export declare function interceptExport(filePkg: ParsedFilePkg, exportName: string, defaultLocalName: string): string;
23
+ export declare function removeCommentsFromCode(code: string): string;