Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to generate ES module file #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '{ messages }';
23 changes: 22 additions & 1 deletion tests/specs/LangJsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
* */
Expand Down