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 ability to overwrite the link and script tags returned by the variable #3

Open
wants to merge 3 commits 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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ npm install html-webpack-plugin write-file-webpack-plugin underscore-template-lo
## Configuration

In the `config` folder at the root of your Craft project, create a `webpackassets.php` file with the following content
and adjust the path to the JSON generated by Webpack.
and adjust the path to the JSON generated by Webpack and optionally add custom templates for the link and script tags

```php
<?php

return [
'jsonPath' => realpath(__DIR__ . '/path/to/generated-assets-files.json'),
'jsonPath' => realpath(__DIR__ . '/../web/dist/generated-assets-files.json'),
'cssTagTemplate' => function($file) {
return '<link rel="preload" href="' . $file . '" as="style" onload="this.rel=\'stylesheet\'"/>';
},
'jsTagTemplate' => function($file) {
return '<script src="' . $file . '" async></script>';
}
];
```

Expand Down
8 changes: 8 additions & 0 deletions src/variables/WebpackAssetsVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function isPublicPathAbsoluteUrl()
*/
private function getCssTagFromFile($file)
{
$config = \Craft::$app->config->getConfigFromFile('webpackassets');

if(key_exists('cssTagTemplate', $config)) return $config['cssTagTemplate']($file);

return '<link rel="stylesheet" href="' . $file . '"/>';
}

Expand All @@ -69,6 +73,10 @@ private function getCssTagFromFile($file)
*/
private function getJsTagFromFile($file)
{
$config = \Craft::$app->config->getConfigFromFile('webpackassets');

if(key_exists('jsTagTemplate', $config)) return $config['jsTagTemplate']($file);

return '<script src="' . $file . '"></script>';
}
}