Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit 4fb7459

Browse files
Dobromir Hristovmartinlindhe
authored andcommitted
feat: Add optional UMD style module export (#19)
* feat: Add optional UMD style module export Add ability to export the locales into an UMD styled export object. * test: Fix failing tests.
1 parent 35c8d8b commit 4fb7459

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/Commands/GenerateInclude.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GenerateInclude extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'vue-i18n:generate';
14+
protected $signature = 'vue-i18n:generate {--umd}';
1515

1616
/**
1717
* The console command description.
@@ -28,8 +28,10 @@ public function handle()
2828
{
2929
$root = base_path() . config('vue-i18n-generator.langPath');
3030

31+
$umd = $this->option('umd');
32+
3133
$data = (new Generator)
32-
->generateFromPath($root);
34+
->generateFromPath($root, $umd);
3335

3436
$jsFile = base_path() . config('vue-i18n-generator.jsFile');
3537

src/Generator.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ class Generator
77
{
88
/**
99
* @param string $path
10+
* @param boolean $umd
1011
* @return string
1112
* @throws Exception
1213
*/
13-
public function generateFromPath($path)
14+
public function generateFromPath($path, $umd = null)
1415
{
1516
if (!is_dir($path)) {
1617
throw new Exception('Directory not found: '.$path);
1718
}
1819

1920
$locales = [];
2021
$dir = new DirectoryIterator($path);
22+
$jsBody = '';
2123

2224
foreach ($dir as $fileinfo) {
2325
if (!$fileinfo->isDot()
@@ -40,8 +42,15 @@ public function generateFromPath($path)
4042
}
4143
}
4244

43-
return 'export default '
44-
. json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
45+
$jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
46+
47+
if(!$umd) {
48+
$jsBody = $this->getES6Module($jsonLocales);
49+
} else {
50+
$jsBody = $this->getUMDModule($jsonLocales);
51+
}
52+
53+
return $jsBody;
4554
}
4655

4756
/**
@@ -155,4 +164,33 @@ private function removeExtension($filename)
155164

156165
return mb_substr($filename, 0, $pos);
157166
}
158-
}
167+
168+
/**
169+
* Returns an UMD style module
170+
* @param string $body
171+
* @return string
172+
*/
173+
private function getUMDModule($body)
174+
{
175+
$js = <<<HEREDOC
176+
(function (global, factory) {
177+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
178+
typeof define === 'function' && define.amd ? define(factory) :
179+
(global.vuei18nLocales = factory());
180+
}(this, (function () { 'use strict';
181+
return {$body}
182+
})));
183+
HEREDOC;
184+
return $js;
185+
}
186+
187+
/**
188+
* Returns an ES6 style module
189+
* @param string $body
190+
* @return string
191+
*/
192+
private function getES6Module($body)
193+
{
194+
return "export default {$body}";
195+
}
196+
}

0 commit comments

Comments
 (0)