-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.php
84 lines (73 loc) · 2.32 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Functions and definitions
*
* @package WordPress
* @subpackage themrishvite
*/
//develop mode config
define( "IS_VITE_DEVELOPMENT", true );
//define
define( 'DIST_DEF', 'dist' );
define( 'DIST_URI', get_template_directory_uri() . '/' . DIST_DEF );
define( 'DIST_PATH', get_template_directory() . '/' . DIST_DEF );
define( 'JS_DEPENDENCY', array() ) ; // array( 'jquery' ) as example
define( 'JS_LOAD_IN_FOOTER', true ) ; // load scripts in footer?
define('VITE_SERVER', 'http://localhost:3000');
define('VITE_ENTRY_POINT', '/main.js');
/*
* init theme support
*/
function themrishvite_theme_support() {
add_theme_support( 'html5', array (
'comment-form',
'comment-list',
'search-form',
'gallery',
'caption',
'style',
'script'
) );
add_theme_support( "post-thumbnails" );
add_theme_support( 'title-tag' );
add_theme_support( 'editor-styles' );
add_theme_support( 'custom-logo' );
add_theme_support( 'automatic-feed-links' );
register_nav_menus( array (
'main-menu' => __( 'mainmenu', 'themrishvite' )
) );
}
add_action( 'after_setup_theme', 'themrishvite_theme_support' );
function cors_http_header() {
header( "Access-Control-Allow-Origin: *" );
}
add_action( 'send_headers', 'cors_http_header' );
add_action( 'wp_enqueue_scripts', function() {
if ( defined( 'IS_VITE_DEVELOPMENT') && IS_VITE_DEVELOPMENT === true ) {
//develop mode
function vite_head_module_hook() {
echo '<script type="module" crossorigin src="' . VITE_SERVER . VITE_ENTRY_POINT . '"></script>';
}
add_action( 'wp_footer', 'vite_head_module_hook' );
} else {
// production mode, 'npm run build' must be executed in order to generate assets
// read manifest.json to figure out what to enqueue
$manifest = json_decode( file_get_contents( DIST_PATH . '/manifest.json'), true );
// is ok
if ( is_array( $manifest ) ) {
// get first key, by default is 'main.js'
$manifest_key = array_keys( $manifest );
if ( isset( $manifest_key[0] ) ) {
// enqueue CSS files
foreach( @$manifest["main.css"] as $css_file ) {
wp_enqueue_style( 'main', DIST_URI . '/' . $css_file );
}
// enqueue main JS file
$js_file = @$manifest["main.js"]['file'];
if ( ! empty( $js_file ) ) {
wp_enqueue_script( 'main', DIST_URI . '/' . $js_file, JS_DEPENDENCY, '', JS_LOAD_IN_FOOTER );
}
}
}
}
} );