@intlify/bundle-utils 5.1.1 → 5.2.0

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.
Files changed (2) hide show
  1. package/lib/js.js +28 -9
  2. package/package.json +1 -3
package/lib/js.js CHANGED
@@ -2,15 +2,11 @@
2
2
  /**
3
3
  * Code generator for i18n js resource
4
4
  */
5
- var __importDefault = (this && this.__importDefault) || function (mod) {
6
- return (mod && mod.__esModule) ? mod : { "default": mod };
7
- };
8
5
  Object.defineProperty(exports, "__esModule", { value: true });
9
6
  exports.generate = void 0;
10
7
  const shared_1 = require("@intlify/shared");
11
8
  const acorn_1 = require("acorn");
12
9
  const estree_walker_1 = require("estree-walker");
13
- const esquery_1 = __importDefault(require("esquery"));
14
10
  const codegen_1 = require("./codegen");
15
11
  /**
16
12
  * @internal
@@ -42,18 +38,18 @@ function generate(targetSource, { type = 'plain', bridge = false, exportESM = fa
42
38
  sourceFile: filename,
43
39
  allowImportExportEverywhere: true
44
40
  });
45
- const astExportDefaultWithObject = (0, esquery_1.default)(ast, 'Program:has(ExportDefaultDeclaration):has(ObjectExpression)');
41
+ const exportResult = scanAst(ast);
46
42
  if (!allowDynamic) {
47
- if (!astExportDefaultWithObject.length) {
43
+ // if (!astExportDefaultWithObject.length) {
44
+ if (!exportResult || exportResult !== 'object') {
48
45
  throw new Error(`You need to define an object as the locale message with 'export default'.`);
49
46
  }
50
47
  }
51
48
  else {
52
- const astExportDefault = (0, esquery_1.default)(ast, 'Program:has(ExportDefaultDeclaration)');
53
- if (!astExportDefault.length) {
49
+ if (!exportResult) {
54
50
  throw new Error(`You need to define 'export default' that will return the locale messages.`);
55
51
  }
56
- if (!astExportDefaultWithObject.length) {
52
+ if (exportResult !== 'object') {
57
53
  /**
58
54
  * NOTE:
59
55
  * If `allowDynamic` is `true`, do not transform the code by this function, return it as is.
@@ -86,6 +82,29 @@ function generate(targetSource, { type = 'plain', bridge = false, exportESM = fa
86
82
  };
87
83
  }
88
84
  exports.generate = generate;
85
+ function scanAst(ast) {
86
+ if (ast.type !== 'Program') {
87
+ throw new Error('Invalid AST: does not have Program node');
88
+ }
89
+ let ret = false;
90
+ for (const node of ast.body) {
91
+ if (node.type === 'ExportDefaultDeclaration') {
92
+ if (node.declaration.type === 'ObjectExpression') {
93
+ ret = 'object';
94
+ break;
95
+ }
96
+ else if (node.declaration.type === 'FunctionDeclaration') {
97
+ ret = 'function';
98
+ break;
99
+ }
100
+ else if (node.declaration.type === 'ArrowFunctionExpression') {
101
+ ret = 'arrow-function';
102
+ break;
103
+ }
104
+ }
105
+ }
106
+ return ret;
107
+ }
89
108
  function generateNode(generator, node, options, injector) {
90
109
  const propsCountStack = [];
91
110
  const pathStack = [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@intlify/bundle-utils",
3
3
  "description": "Bundle utilities for Intlify project",
4
- "version": "5.1.1",
4
+ "version": "5.2.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -21,14 +21,12 @@
21
21
  "@intlify/message-compiler": "next",
22
22
  "@intlify/shared": "next",
23
23
  "acorn": "^8.8.2",
24
- "esquery": "^1.5.0",
25
24
  "estree-walker": "^2.0.2",
26
25
  "jsonc-eslint-parser": "^1.0.1",
27
26
  "source-map": "0.6.1",
28
27
  "yaml-eslint-parser": "^0.3.2"
29
28
  },
30
29
  "devDependencies": {
31
- "@types/esquery": "^1.0.2",
32
30
  "@types/estree": "^1.0.0"
33
31
  },
34
32
  "engines": {