Skip to content

Commit 72bc6d8

Browse files
authored
Merge pull request #184 from yajra/useVite
feat: add builder ability to use viteJs by default
2 parents dc8d807 + 3db00e1 commit 72bc6d8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Html/Builder.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Yajra\DataTables\Html;
44

5-
use Yajra\DataTables\Html\HtmlBuilder;
65
use Illuminate\Contracts\Config\Repository;
76
use Illuminate\Contracts\View\Factory;
87
use Illuminate\Support\Collection;
@@ -30,6 +29,11 @@ class Builder
3029
const SELECT_ITEMS_COLUMN = 'column';
3130
const SELECT_ITEMS_CELL = 'cell';
3231

32+
/**
33+
* The default type to use for the DataTables javascript.
34+
*/
35+
protected static string $jsType = 'text/javascript';
36+
3337
/**
3438
* @var Collection<int, \Yajra\DataTables\Html\Column>
3539
*/
@@ -73,6 +77,22 @@ public function __construct(public Repository $config, public Factory $view, pub
7377
];
7478
}
7579

80+
/**
81+
* Set the default type to module for the DataTables javascript.
82+
*/
83+
public static function useVite(): void
84+
{
85+
static::$jsType = 'module';
86+
}
87+
88+
/**
89+
* Set the default type to text/javascript for the DataTables javascript.
90+
*/
91+
public static function useWebpack(): void
92+
{
93+
static::$jsType = 'text/javascript';
94+
}
95+
7696
/**
7797
* Generate DataTable javascript.
7898
*
@@ -83,7 +103,9 @@ public function __construct(public Repository $config, public Factory $view, pub
83103
public function scripts(string $script = null, array $attributes = ['type' => 'text/javascript']): HtmlString
84104
{
85105
$script = $script ?: $this->generateScripts();
86-
$attributes = $this->html->attributes($attributes);
106+
$attributes = $this->html->attributes(
107+
array_merge($attributes, ['type' => static::$jsType])
108+
);
87109

88110
return new HtmlString("<script{$attributes}>$script</script>");
89111
}

tests/BuilderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
class BuilderTest extends TestCase
1313
{
14+
/** @test */
15+
public function it_can_use_vitejs_module_script()
16+
{
17+
Builder::useVite();
18+
19+
$this->assertStringContainsString('type="module"', $this->getHtmlBuilder()->scripts()->toHtml());
20+
21+
Builder::useWebpack();
22+
}
23+
1424
/** @test */
1525
public function it_can_resolved_builder_class()
1626
{

0 commit comments

Comments
 (0)