diff --git a/src/Html/Builder.php b/src/Html/Builder.php index 6ab72f6..0eb8e77 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -2,7 +2,6 @@ namespace Yajra\DataTables\Html; -use Yajra\DataTables\Html\HtmlBuilder; use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Collection; @@ -30,6 +29,11 @@ class Builder const SELECT_ITEMS_COLUMN = 'column'; const SELECT_ITEMS_CELL = 'cell'; + /** + * The default type to use for the DataTables javascript. + */ + protected static string $jsType = 'text/javascript'; + /** * @var Collection */ @@ -73,6 +77,22 @@ public function __construct(public Repository $config, public Factory $view, pub ]; } + /** + * Set the default type to module for the DataTables javascript. + */ + public static function useVite(): void + { + static::$jsType = 'module'; + } + + /** + * Set the default type to text/javascript for the DataTables javascript. + */ + public static function useWebpack(): void + { + static::$jsType = 'text/javascript'; + } + /** * Generate DataTable javascript. * @@ -83,7 +103,9 @@ public function __construct(public Repository $config, public Factory $view, pub public function scripts(string $script = null, array $attributes = ['type' => 'text/javascript']): HtmlString { $script = $script ?: $this->generateScripts(); - $attributes = $this->html->attributes($attributes); + $attributes = $this->html->attributes( + array_merge($attributes, ['type' => static::$jsType]) + ); return new HtmlString("$script"); } diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 685a1bc..c2196b8 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -11,6 +11,16 @@ class BuilderTest extends TestCase { + /** @test */ + public function it_can_use_vitejs_module_script() + { + Builder::useVite(); + + $this->assertStringContainsString('type="module"', $this->getHtmlBuilder()->scripts()->toHtml()); + + Builder::useWebpack(); + } + /** @test */ public function it_can_resolved_builder_class() {