@intlify/bundle-utils
Advanced tools
Comparing version 5.0.1 to 5.1.0
@@ -16,2 +16,3 @@ import { CompileError, ResourceNode } from '@intlify/message-compiler'; | ||
export interface SourceLocationable { | ||
start?: number; | ||
loc?: { | ||
@@ -38,2 +39,3 @@ start: Position; | ||
useClassComponent?: boolean; | ||
allowDynamic?: boolean; | ||
onWarn?: (msg: string) => void; | ||
@@ -71,5 +73,6 @@ onError?: (msg: string, extra?: { | ||
*/ | ||
export interface CodeGenResult<ASTNode> { | ||
export interface CodeGenResult<ASTNode, CodeGenError extends Error = Error> { | ||
code: string; | ||
ast: ASTNode; | ||
errors?: CodeGenError[]; | ||
map?: RawSourceMap; | ||
@@ -76,0 +79,0 @@ } |
@@ -24,4 +24,4 @@ "use strict"; | ||
_context.code += code; | ||
if (_context.map) { | ||
if (node && node.loc && node.loc !== message_compiler_1.LocationStub) { | ||
if (_context.map && node) { | ||
if (node.loc && node.loc !== message_compiler_1.LocationStub) { | ||
addMapping(node.loc.start, name); | ||
@@ -28,0 +28,0 @@ } |
@@ -5,4 +5,3 @@ /** | ||
/// <reference types="node" /> | ||
import { ParseResult } from '@babel/parser'; | ||
import type { File } from '@babel/types'; | ||
import type { Node } from 'estree'; | ||
import type { CodeGenOptions, CodeGenResult } from './codegen'; | ||
@@ -12,2 +11,2 @@ /** | ||
*/ | ||
export declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, useClassComponent }: CodeGenOptions, injector?: () => string): CodeGenResult<ParseResult<File>>; | ||
export declare function generate(targetSource: string | Buffer, { type, bridge, exportESM, filename, inSourceMap, locale, isGlobal, sourceMap, env, forceStringify, onError, useClassComponent, allowDynamic }: CodeGenOptions, injector?: () => string): CodeGenResult<Node>; |
266
lib/js.js
@@ -10,4 +10,6 @@ "use strict"; | ||
exports.generate = void 0; | ||
const parser_1 = require("@babel/parser"); | ||
const traverse_1 = __importDefault(require("@babel/traverse")); | ||
const shared_1 = require("@intlify/shared"); | ||
const acorn_1 = require("acorn"); | ||
const estree_walker_1 = require("estree-walker"); | ||
const esquery_1 = __importDefault(require("esquery")); | ||
const codegen_1 = require("./codegen"); | ||
@@ -17,3 +19,3 @@ /** | ||
*/ | ||
function generate(targetSource, { type = 'plain', bridge = false, exportESM = false, filename = 'vue-i18n-loader.js', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined, useClassComponent = false }, injector) { | ||
function generate(targetSource, { type = 'plain', bridge = false, exportESM = false, filename = 'vue-i18n-loader.js', inSourceMap = undefined, locale = '', isGlobal = false, sourceMap = false, env = 'development', forceStringify = false, onError = undefined, useClassComponent = false, allowDynamic = false }, injector) { | ||
const target = Buffer.isBuffer(targetSource) | ||
@@ -39,10 +41,35 @@ ? targetSource.toString() | ||
const generator = (0, codegen_1.createCodeGenerator)(options); | ||
const ast = (0, parser_1.parse)(value, { | ||
const ast = (0, acorn_1.parse)(value, { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
sourceFilename: filename, | ||
sourceFile: filename, | ||
allowImportExportEverywhere: true | ||
}); | ||
if (ast.errors) { | ||
// TODO: | ||
const astExportDefaultWithObject = (0, esquery_1.default)(ast, 'Program:has(ExportDefaultDeclaration):has(ObjectExpression)'); | ||
console.log('astExportDefaultWithObject', astExportDefaultWithObject); | ||
if (!allowDynamic) { | ||
if (!astExportDefaultWithObject.length) { | ||
throw new Error(`You need to define an object as the locale message with 'export default'.`); | ||
} | ||
} | ||
else { | ||
const astExportDefault = (0, esquery_1.default)(ast, 'Program:has(ExportDefaultDeclaration)'); | ||
if (!astExportDefault.length) { | ||
throw new Error(`You need to define 'export default' that will return the locale messages.`); | ||
} | ||
console.log('astExportDefault', astExportDefault); | ||
if (!astExportDefaultWithObject.length) { | ||
/** | ||
* NOTE: | ||
* If `allowDynamic` is `true`, do not transform the code by this function, return it as is. | ||
* This means that the user **must transform locale messages ownself**. | ||
* Especially at the production, you need to do locale messages pre-compiling. | ||
*/ | ||
return { | ||
ast, | ||
code: value, | ||
map: inSourceMap | ||
}; | ||
} | ||
} | ||
const codeMaps = generateNode(generator, ast, options, injector); | ||
@@ -61,3 +88,3 @@ const { code, map } = generator.context(); | ||
return { | ||
ast: ast, | ||
ast, | ||
code, | ||
@@ -82,5 +109,11 @@ map: newMap != null ? newMap : undefined | ||
: `Component`; | ||
// @ts-ignore TODO: `tranverse` first argument type should be fixed | ||
(0, traverse_1.default)(node, { | ||
enter({ node, parent }) { | ||
(0, estree_walker_1.walk)(node, { | ||
/** | ||
* NOTE: | ||
* force cast to Node of `estree-walker@3.x`, | ||
* because `estree-walker@3.x` is not dual packages, | ||
* so it's support only esm only ... | ||
*/ | ||
// @ts-ignore | ||
enter(node, parent) { | ||
switch (node.type) { | ||
@@ -113,3 +146,3 @@ case 'Program': | ||
propsCountStack.push(node.properties.length); | ||
if (parent.type === 'ArrayExpression') { | ||
if (parent != null && parent.type === 'ArrayExpression') { | ||
const lastIndex = itemsCountStack.length - 1; | ||
@@ -121,52 +154,52 @@ const currentCount = parent.elements.length - itemsCountStack[lastIndex]; | ||
break; | ||
case 'ObjectProperty': | ||
if (isJSONablePrimitiveLiteral(node.value) && | ||
(node.key.type === 'StringLiteral' || | ||
node.key.type === 'Identifier')) { | ||
// prettier-ignore | ||
const name = node.key.type === 'StringLiteral' | ||
? node.key.value | ||
: node.key.name; | ||
if (node.value.type === 'StringLiteral' || | ||
node.value.type === 'TemplateLiteral') { | ||
const value = getValue(node.value); | ||
generator.push(`${JSON.stringify(name)}: `); | ||
pathStack.push(name); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(value, map); | ||
generator.push(`${code}`, node.value, value); | ||
skipStack.push(false); | ||
} | ||
else { | ||
const value = getValue(node.value); | ||
if (forceStringify) { | ||
const strValue = JSON.stringify(value); | ||
case 'Property': | ||
if (node != null) { | ||
if (isJSONablePrimitiveLiteral(node.value) && | ||
(node.key.type === 'Literal' || node.key.type === 'Identifier')) { | ||
// prettier-ignore | ||
const name = node.key.type === 'Literal' | ||
? String(node.key.value) | ||
: node.key.name; | ||
if ((node.value.type === 'Literal' && (0, shared_1.isString)(node.value.value)) || | ||
node.value.type === 'TemplateLiteral') { | ||
const value = getValue(node.value); | ||
generator.push(`${JSON.stringify(name)}: `); | ||
pathStack.push(name); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(strValue, map); | ||
generator.push(`${code}`, node.value, strValue); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(value, map); | ||
generator.push(`${code}`, node.value, value); | ||
skipStack.push(false); | ||
} | ||
else { | ||
generator.push(`${JSON.stringify(name)}: ${JSON.stringify(value)}`); | ||
pathStack.push(name); | ||
const value = getValue(node.value); | ||
if (forceStringify) { | ||
const strValue = JSON.stringify(value); | ||
generator.push(`${JSON.stringify(name)}: `); | ||
pathStack.push(name); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(strValue, map); | ||
generator.push(`${code}`, node.value, strValue); | ||
} | ||
else { | ||
generator.push(`${JSON.stringify(name)}: ${JSON.stringify(value)}`); | ||
pathStack.push(name); | ||
} | ||
skipStack.push(false); | ||
} | ||
skipStack.push(false); | ||
} | ||
else if ((node.value.type === 'ObjectExpression' || | ||
node.value.type === 'ArrayExpression') && | ||
(node.key.type === 'Literal' || node.key.type === 'Identifier')) { | ||
// prettier-ignore | ||
const name = node.key.type === 'Literal' | ||
? String(node.key.value) | ||
: node.key.name; | ||
generator.push(`${JSON.stringify(name)}: `); | ||
pathStack.push(name); | ||
} | ||
else { | ||
// for Regex, function, etc. | ||
skipStack.push(true); | ||
} | ||
} | ||
else if ((node.value.type === 'ObjectExpression' || | ||
node.value.type === 'ArrayExpression') && | ||
(node.key.type === 'StringLiteral' || | ||
node.key.type === 'Identifier')) { | ||
// prettier-ignore | ||
const name = node.key.type === 'StringLiteral' | ||
? node.key.value | ||
: node.key.name; | ||
generator.push(`${JSON.stringify(name)}: `); | ||
pathStack.push(name); | ||
} | ||
else { | ||
// for Regex, function, etc. | ||
skipStack.push(true); | ||
} | ||
const lastIndex = propsCountStack.length - 1; | ||
@@ -178,3 +211,3 @@ propsCountStack[lastIndex] = --propsCountStack[lastIndex]; | ||
generator.indent(); | ||
if (parent.type === 'ArrayExpression') { | ||
if (parent != null && parent.type === 'ArrayExpression') { | ||
const lastIndex = itemsCountStack.length - 1; | ||
@@ -188,3 +221,3 @@ const currentCount = parent.elements.length - itemsCountStack[lastIndex]; | ||
default: | ||
if (isJSONablePrimitiveLiteral(node)) { | ||
if (node != null && parent != null) { | ||
if (parent.type === 'ArrayExpression') { | ||
@@ -194,23 +227,28 @@ const lastIndex = itemsCountStack.length - 1; | ||
pathStack.push(currentCount.toString()); | ||
if (node.type === 'StringLiteral' || | ||
node.type === 'TemplateLiteral') { | ||
const value = getValue(node); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(value, map); | ||
generator.push(`${code}`, node, value); | ||
skipStack.push(false); | ||
} | ||
else { | ||
const value = getValue(node); | ||
if (forceStringify) { | ||
const strValue = JSON.stringify(value); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(strValue, map); | ||
generator.push(`${code}`, node, strValue); | ||
if (isJSONablePrimitiveLiteral(node)) { | ||
if ((node.type === 'Literal' && (0, shared_1.isString)(node.value)) || | ||
node.type === 'TemplateLiteral') { | ||
const value = getValue(node); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(value, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(value, map); | ||
generator.push(`${code}`, node, value); | ||
} | ||
else { | ||
generator.push(`${JSON.stringify(value)}`); | ||
const value = getValue(node); | ||
if (forceStringify) { | ||
const strValue = JSON.stringify(value); | ||
const { code, map } = (0, codegen_1.generateMessageFunction)(strValue, options, pathStack); | ||
sourceMap && map != null && codeMaps.set(strValue, map); | ||
generator.push(`${code}`, node, strValue); | ||
} | ||
else { | ||
generator.push(`${JSON.stringify(value)}`); | ||
} | ||
} | ||
skipStack.push(false); | ||
} | ||
else { | ||
// for Regex, function, etc. | ||
skipStack.push(true); | ||
} | ||
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex]; | ||
@@ -225,3 +263,10 @@ } | ||
}, | ||
exit({ node, parent }) { | ||
/** | ||
* NOTE: | ||
* force cast to Node of `estree-walker@3.x`, | ||
* because `estree-walker@3.x` is not dual packages, | ||
* so it's support only esm only ... | ||
*/ | ||
// @ts-ignore | ||
leave(node, parent) { | ||
switch (node.type) { | ||
@@ -249,3 +294,3 @@ case 'Program': | ||
generator.push(`}`); | ||
if (parent.type === 'ArrayExpression') { | ||
if (parent != null && parent.type === 'ArrayExpression') { | ||
if (itemsCountStack[itemsCountStack.length - 1] !== 0) { | ||
@@ -257,3 +302,3 @@ pathStack.pop(); | ||
break; | ||
case 'ObjectProperty': | ||
case 'Property': | ||
if (propsCountStack[propsCountStack.length - 1] !== 0) { | ||
@@ -273,3 +318,3 @@ pathStack.pop(); | ||
generator.push(`]`); | ||
if (parent.type === 'ArrayExpression') { | ||
if (parent != null && parent.type === 'ArrayExpression') { | ||
if (itemsCountStack[itemsCountStack.length - 1] !== 0) { | ||
@@ -283,22 +328,19 @@ pathStack.pop(); | ||
break; | ||
default: | ||
if (isJSONablePrimitiveLiteral(node)) { | ||
if (parent.type === 'ArrayExpression') { | ||
if (itemsCountStack[itemsCountStack.length - 1] !== 0) { | ||
pathStack.pop(); | ||
if (!skipStack.pop()) { | ||
generator.pushline(`,`); | ||
} | ||
case 'Literal': | ||
if (parent != null && parent.type === 'ArrayExpression') { | ||
if (itemsCountStack[itemsCountStack.length - 1] !== 0) { | ||
pathStack.pop(); | ||
if (!skipStack.pop()) { | ||
generator.pushline(`,`); | ||
} | ||
else { | ||
if (!skipStack.pop()) { | ||
generator.pushline(`,`); | ||
} | ||
} | ||
else { | ||
if (!skipStack.pop()) { | ||
generator.pushline(`,`); | ||
} | ||
} | ||
} | ||
else { | ||
// ... | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
@@ -310,17 +352,37 @@ } | ||
function isJSONablePrimitiveLiteral(node) { | ||
return (node.type === 'NullLiteral' || | ||
node.type === 'BooleanLiteral' || | ||
node.type === 'NumericLiteral' || | ||
node.type === 'StringLiteral' || | ||
return ((node.type === 'Literal' && | ||
((0, shared_1.isString)(node.value) || | ||
(0, shared_1.isNumber)(node.value) || | ||
(0, shared_1.isBoolean)(node.value) || | ||
node.value === null)) || | ||
node.type === 'TemplateLiteral'); | ||
// NOTE: the following code is same the above code | ||
/* | ||
if (node.type === 'Literal') { | ||
if ( | ||
isString(node.value) || | ||
isNumber(node.value) || | ||
isBoolean(node.value) || | ||
node.value === null | ||
) { | ||
return true | ||
} else if (isRegExp(node.value)) { | ||
return false | ||
} else { | ||
return false | ||
} | ||
} else if (node.type === 'TemplateLiteral') { | ||
return true | ||
} else { | ||
return false | ||
} | ||
*/ | ||
} | ||
function getValue(node) { | ||
// prettier-ignore | ||
return node.type === 'StringLiteral' | ||
return node.type === 'Literal' | ||
? node.value | ||
: node.type === 'NullLiteral' | ||
? null | ||
: node.type === 'TemplateLiteral' | ||
? node.quasis.map(quasi => quasi.value.cooked).join('') | ||
: node.value; | ||
: node.type === 'TemplateLiteral' | ||
? node.quasis.map(quasi => quasi.value.cooked).join('') | ||
: undefined; | ||
} |
{ | ||
"name": "@intlify/bundle-utils", | ||
"description": "Bundle utilities for Intlify project", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"author": { | ||
@@ -21,6 +21,7 @@ "name": "kazuya kawaguchi", | ||
"dependencies": { | ||
"@babel/parser": "^7.21.2", | ||
"@babel/traverse": "^7.21.2", | ||
"@intlify/message-compiler": "next", | ||
"@intlify/shared": "next", | ||
"acorn": "^8.8.2", | ||
"esquery": "^1.5.0", | ||
"estree-walker": "^2.0.2", | ||
"jsonc-eslint-parser": "^1.0.1", | ||
@@ -31,3 +32,4 @@ "source-map": "0.6.1", | ||
"devDependencies": { | ||
"@babel/types": "^7.21.2" | ||
"@types/esquery": "^1.0.2", | ||
"@types/estree": "^1.0.0" | ||
}, | ||
@@ -34,0 +36,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132461
18
3340
8
2
4
+ Addedacorn@^8.8.2
+ Addedesquery@^1.5.0
+ Addedestree-walker@^2.0.2
+ Addedacorn@8.14.0(transitive)
+ Addedesquery@1.6.0(transitive)
+ Addedestraverse@5.3.0(transitive)
+ Addedestree-walker@2.0.2(transitive)
- Removed@babel/parser@^7.21.2
- Removed@babel/traverse@^7.21.2
- Removed@babel/code-frame@7.26.2(transitive)
- Removed@babel/generator@7.26.3(transitive)
- Removed@babel/helper-string-parser@7.25.9(transitive)
- Removed@babel/helper-validator-identifier@7.25.9(transitive)
- Removed@babel/parser@7.26.3(transitive)
- Removed@babel/template@7.25.9(transitive)
- Removed@babel/traverse@7.26.4(transitive)
- Removed@babel/types@7.26.3(transitive)
- Removed@jridgewell/gen-mapping@0.3.8(transitive)
- Removed@jridgewell/resolve-uri@3.1.2(transitive)
- Removed@jridgewell/set-array@1.2.1(transitive)
- Removed@jridgewell/sourcemap-codec@1.5.0(transitive)
- Removed@jridgewell/trace-mapping@0.3.25(transitive)
- Removeddebug@4.4.0(transitive)
- Removedglobals@11.12.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjsesc@3.1.0(transitive)
- Removedms@2.1.3(transitive)
- Removedpicocolors@1.1.1(transitive)