diff --git a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php index b98111c..7d6e3b4 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php +++ b/src/Mariuzzo/LaravelJsLocalization/Commands/LangJsCommand.php @@ -64,7 +64,8 @@ public function handle() $options = [ 'compress' => $this->option('compress'), 'json' => $this->option('json'), - 'no-lib' => $this->option('no-lib'), + 'no-lib' => $this->option('no-lib') || $this->option('module'), + 'module' => $this->option('module'), 'source' => $this->option('source'), 'no-sort' => $this->option('no-sort'), ]; @@ -110,6 +111,7 @@ protected function getOptions() return [ ['compress', 'c', InputOption::VALUE_NONE, 'Compress the JavaScript file.', null], ['no-lib', 'nl', InputOption::VALUE_NONE, 'Do not include the lang.js library.', null], + ['module', 'm', InputOption::VALUE_NONE, 'Output as ES module instead of commonjs, implies --no-lib', null], ['json', 'j', InputOption::VALUE_NONE, 'Only output the messages json.', null], ['source', 's', InputOption::VALUE_REQUIRED, 'Specifying a custom source folder', null], ['no-sort', 'ns', InputOption::VALUE_NONE, 'Do not sort the messages', null], diff --git a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php index 8e2a945..dc06268 100644 --- a/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php +++ b/src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php @@ -74,7 +74,11 @@ public function generate($target, $options) $this->prepareTarget($target); if ($options['no-lib']) { - $template = $this->file->get(__DIR__.'/Templates/messages.js'); + if ($options['module']) { + $template = $this->file->get(__DIR__.'/Templates/messages.mjs'); + } else { + $template = $this->file->get(__DIR__.'/Templates/messages.cjs'); + } } else if ($options['json']) { $template = $this->file->get(__DIR__.'/Templates/messages.json'); } else { diff --git a/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.js b/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.cjs similarity index 100% rename from src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.js rename to src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.cjs diff --git a/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.mjs b/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.mjs new file mode 100644 index 0000000..27c6e11 --- /dev/null +++ b/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.mjs @@ -0,0 +1 @@ +export default '{ messages }'; \ No newline at end of file diff --git a/tests/specs/LangJsCommandTest.php b/tests/specs/LangJsCommandTest.php index 4465677..f9559f9 100644 --- a/tests/specs/LangJsCommandTest.php +++ b/tests/specs/LangJsCommandTest.php @@ -254,7 +254,7 @@ public function testShouldIgnoreDefaultOutputPathFromConfigIfTargetArgumentExist * */ public function testShouldTemplateMessagesHasHandlebars() { - $template = "$this->rootPath/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.js"; + $template = "$this->rootPath/src/Mariuzzo/LaravelJsLocalization/Generators/Templates/messages.cjs"; $this->assertFileExists($template); $contents = file_get_contents($template); @@ -281,6 +281,27 @@ public function testShouldOnlyMessageExported() $this->cleanupOutputDirectory(); } + /* + * test command with option --module + * */ + public function testEsModuleOutputExported() + { + $generator = new LangJsGenerator(new File(), $this->langPath); + $command = new LangJsCommand($generator); + $command->setLaravel($this->app); + + $code = $this->runCommand($command, ['target' => $this->outputFilePath, '--module' => true]); + $this->assertRunsWithSuccess($code); + $this->assertFileExists($this->outputFilePath); + + $contents = file_get_contents($this->outputFilePath); + $this->_assertStringContainsString('export default', $contents); + + $this->assertNotEmpty($contents); + $this->assertHasNotHandlebars('messages', $contents); + $this->cleanupOutputDirectory(); + } + /* * test command with option --json * */