Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 29 additions & 40 deletions packages/bundle-utils/src/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,46 @@ import type { CodeGenerator, CodeGenFunction, CodeGenOptions, CodeGenResult } fr

export class DynamicResourceError extends Error {}

/**
* @internal
*/
export const DEFAULT_OPTIONS: CodeGenOptions = {
type: 'plain',
filename: 'vue-i18n-loader.js',
inSourceMap: undefined,
locale: '',
isGlobal: false,
sourceMap: false,
env: 'development',
forceStringify: false,
onError: undefined,
onWarn: undefined,
strictMessage: true,
escapeHtml: false,
allowDynamic: false,
jit: false
}

/**
* @internal
*/
export function generate(
targetSource: string | Buffer,
{
type = 'plain',
filename = 'vue-i18n-loader.js',
inSourceMap = undefined,
locale = '',
isGlobal = false,
sourceMap = false,
env = 'development',
forceStringify = false,
onError = undefined,
onWarn = undefined,
strictMessage = true,
escapeHtml = false,
allowDynamic = false,
jit = false
}: CodeGenOptions
options: CodeGenOptions
): CodeGenResult<Node> {
const value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource

const options = {
type,
source: value,
sourceMap,
locale,
isGlobal,
inSourceMap,
env,
filename,
forceStringify,
onError,
onWarn,
strictMessage,
escapeHtml,
jit
} as CodeGenOptions
const generator = createCodeGenerator(options)

const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value })
const generator = createCodeGenerator(_options)
const ast = parseJavaScript(value, {
ecmaVersion: 'latest',
sourceType: 'module',
sourceFile: filename,
sourceFile: _options.filename,
allowImportExportEverywhere: true
}) as Node

const exportResult = scanAst(ast)
if (!allowDynamic) {
if (!_options.allowDynamic) {
if (!exportResult) {
throw new Error(`You need to define an object as the locale message with 'export default'.`)
}
Expand All @@ -95,17 +84,17 @@ export function generate(
return {
ast,
code: value,
map: inSourceMap
map: _options.inSourceMap
}
}
}

const codeMaps = _generate(generator, ast, options)
const codeMaps = _generate(generator, ast, _options)

const { code, map } = generator.context()
// prettier-ignore
const newMap = map
? mapLinesColumns((map as any).toJSON(), codeMaps, inSourceMap) || null
? mapLinesColumns((map as any).toJSON(), codeMaps, _options.inSourceMap) || null
: null
return {
ast,
Expand Down Expand Up @@ -162,7 +151,7 @@ function _generate(
const componentNamespace = '_Component'
const variableDeclarations: string[] = []

// slice and reseuse imports and top-level variable declarations as-is
// slice and reuse imports and top-level variable declarations as-is
// NOTE: this prevents optimization/compilation of top-level variables, we may be able to add support for this
walk(node, {
/**
Expand Down
44 changes: 6 additions & 38 deletions packages/bundle-utils/src/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,26 @@
import { transform } from 'esbuild'
import { Node } from 'estree'
import { CodeGenOptions, CodeGenResult } from './codegen'
import { generate as generateJavaScript } from './js'
import { DEFAULT_OPTIONS, generate as generateJavaScript } from './js'

/**
* @internal
*/
export async function generate(
targetSource: string | Buffer,
{
type = 'plain',
filename = 'vue-i18n-loader.js',
inSourceMap = undefined,
locale = '',
isGlobal = false,
sourceMap = false,
env = 'development',
forceStringify = false,
onError = undefined,
onWarn = undefined,
strictMessage = true,
escapeHtml = false,
allowDynamic = false,
jit = false
}: CodeGenOptions
options: CodeGenOptions
): Promise<CodeGenResult<Node>> {
let value = Buffer.isBuffer(targetSource) ? targetSource.toString() : targetSource

const options = {
type,
source: value,
sourceMap,
locale,
isGlobal,
inSourceMap,
env,
filename,
forceStringify,
onError,
onWarn,
strictMessage,
escapeHtml,
allowDynamic,
jit
} as CodeGenOptions

if (options.filename && /.[c|m]?ts$/.test(options.filename)) {
const _options = Object.assign({}, DEFAULT_OPTIONS, options, { source: value })
if (_options.filename && /.[c|m]?ts$/.test(_options.filename)) {
const transformed = await transform(value, { loader: 'ts' })

if (transformed && transformed.code) {
value = transformed.code
options.source = transformed.code
_options.source = transformed.code
}
}

return generateJavaScript(value, options)
return generateJavaScript(value, _options)
}