Skip to content

Commit 616066d

Browse files
authored
Merge pull request #4 from g3n1us/update-for-laravel57
Adapt for different assets file structure in Laravel 5.7
2 parents 4a1fd8c + b7b08a9 commit 616066d

File tree

5 files changed

+84
-46
lines changed

5 files changed

+84
-46
lines changed

src/LaravelReactSyncServiceProvider.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace G3n1us\LaravelReactSync;
2+
namespace G3n1us\LaravelReactSync;
33

44
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
55
use Illuminate\Support\Facades\View;
@@ -11,6 +11,11 @@
1111

1212
class LaravelReactSyncServiceProvider extends LaravelServiceProvider{
1313

14+
15+
private function getJsPath(){
16+
return is_dir(resource_path('js')) ? resource_path('js') : resource_path('assets/js');
17+
}
18+
1419
/**
1520
* Register bindings in the container.
1621
*
@@ -21,8 +26,8 @@ public function register()
2126
$this->mergeConfigFrom(
2227
__DIR__.'/config.php', 'react_sync'
2328
);
24-
25-
29+
30+
2631
View::creator('*', function ($view) {
2732
$alldata = collect($view->getFactory()->getShared())
2833
->merge($view->getData())
@@ -40,11 +45,11 @@ public function register()
4045
}
4146
}
4247
}
43-
48+
4449
// $model_arr now exists and can be used somewhere. Hmmm ... where?
45-
50+
4651
View::share('page_data', $alldata);
47-
52+
4853
// auto_jsonable route stuff below ... TODO apply middleware from the original route to secure the ajax requested version. Is this needed??
4954
$route = \Route::current();
5055
if($route){
@@ -67,53 +72,53 @@ public function register()
6772
}
6873
Blade::directive('page_state_embed', function ($expression) use($alldata){
6974
$return = $alldata->toJson();
70-
75+
7176
$page_state_embed = "<script>window.page_state_data=$return</script>";
72-
77+
7378
return "<?php echo '$page_state_embed'; ?>";
7479
});
75-
});
76-
80+
});
81+
7782
// Allow Laravel 5.5.* by checking version and polyfilling where needed
7883
if(version_compare((app())::VERSION, "5.6.0", "<")){
7984
Blade::directive('csrf', function ($expression) {
8085
return "<?php echo csrf_field(); ?>";
8186
});
8287
}
83-
84-
85-
}
86-
87-
88+
89+
90+
}
91+
92+
8893
/**
8994
* Perform post-registration booting of services.
9095
*
9196
* @return void
9297
*/
9398
public function boot()
9499
{
95-
100+
96101
$this->loadRoutesFrom(__DIR__.'/routes.php');
97-
98-
$this->loadViewsFrom(__DIR__.'/views', 'react_sync');
102+
103+
$this->loadViewsFrom(__DIR__.'/views', 'react_sync');
99104

100105
$this->publishes([
101106
__DIR__.'/config.php' => config_path('react_sync.php'),
102-
__DIR__.'/assets' => resource_path('assets/js/vendor/laravel-react-sync'),
107+
__DIR__.'/assets' => $this->getJsPath() . '/vendor/laravel-react-sync',
103108
]);
104-
109+
105110
// Load this into the `preset` Artisan command as the type: `react-sync`
106-
107-
111+
112+
108113
PresetCommand::macro('react-sync', function ($command_instance) {
109-
114+
110115
ReactSyncPreset::install();
111-
116+
112117
$command_instance->info('ReactSync scaffolding installed successfully.');
113118
$command_instance->comment('Please run "npm install && npm run dev" to compile your fresh scaffolding.');
114119
});
115120

116-
}
121+
}
122+
117123

118-
119124
}

src/ReactSyncPreset.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class ReactSyncPreset extends Preset
1010
{
11+
12+
private function getJsPath(){
13+
return is_dir(resource_path('js')) ? resource_path('js') : resource_path('assets/js');
14+
}
15+
1116
/**
1217
* Install the preset.
1318
*
@@ -31,11 +36,13 @@ public static function install()
3136
*/
3237
protected static function updatePackageArray(array $packages)
3338
{
39+
40+
$js_path = is_dir(resource_path('js')) ? 'resources/js' : 'resources/assets/js';
3441
return [
3542
'babel-preset-react' => '^6.23.0',
3643
'react' => '^15.4.2',
3744
'react-dom' => '^15.4.2',
38-
'laravel-react-sync' => 'file:resources/assets/js/vendor/laravel-react-sync',
45+
'laravel-react-sync' => "file:$js_path/vendor/laravel-react-sync",
3946
] + Arr::except($packages, ['vue']);
4047
}
4148

@@ -46,7 +53,11 @@ protected static function updatePackageArray(array $packages)
4653
*/
4754
protected static function updateWebpackConfiguration()
4855
{
49-
copy(__DIR__.'/react-sync-stubs/webpack.mix.js', base_path('webpack.mix.js'));
56+
$js_path = is_dir(resource_path('js')) ? 'js' : 'assets/js';
57+
if(is_dir(resource_path('assets/js')))
58+
copy(__DIR__.'/react-sync-stubs/webpack56.mix.js', base_path('webpack.mix.js'));
59+
else
60+
copy(__DIR__.'/react-sync-stubs/webpack.mix.js', base_path('webpack.mix.js'));
5061
}
5162

5263
/**
@@ -56,33 +67,35 @@ protected static function updateWebpackConfiguration()
5667
*/
5768
protected static function updateComponent()
5869
{
70+
$js_path = is_dir(resource_path('js')) ? 'js' : 'assets/js';
71+
5972
(new Filesystem)->delete(
60-
resource_path('assets/js/components/ExampleComponent.vue')
73+
resource_path("$js_path/components/ExampleComponent.vue")
6174
);
6275

6376
copy(
6477
__DIR__.'/react-sync-stubs/Example.js',
65-
resource_path('assets/js/components/Example.js')
78+
resource_path("$js_path/components/Example.js")
6679
);
67-
80+
6881
// make the vendor directory if it doesn't exist
69-
if(!is_dir(resource_path('assets/js/vendor')))
70-
mkdir(resource_path('assets/js/vendor'));
71-
72-
if(!is_dir(resource_path('assets/js/vendor/laravel-react-sync')))
73-
mkdir(resource_path('assets/js/vendor/laravel-react-sync'));
74-
82+
if(!is_dir(resource_path("$js_path/vendor")))
83+
mkdir(resource_path("$js_path/vendor"));
84+
85+
if(!is_dir(resource_path("$js_path/vendor/laravel-react-sync")))
86+
mkdir(resource_path("$js_path/vendor/laravel-react-sync"));
87+
7588
copy(
7689
__DIR__.'/assets/LaravelReactSync.js',
77-
resource_path('assets/js/vendor/laravel-react-sync/LaravelReactSync.js')
90+
resource_path("$js_path/vendor/laravel-react-sync/LaravelReactSync.js")
7891
);
79-
80-
92+
93+
8194
copy(
8295
__DIR__.'/assets/package.json',
83-
resource_path('assets/js/vendor/laravel-react-sync/package.json')
96+
resource_path("$js_path/vendor/laravel-react-sync/package.json")
8497
);
85-
98+
8699
}
87100

88101
/**
@@ -92,6 +105,7 @@ protected static function updateComponent()
92105
*/
93106
protected static function updateBootstrapping()
94107
{
95-
copy(__DIR__.'/react-sync-stubs/app.js', resource_path('assets/js/app.js'));
108+
$js_path = is_dir(resource_path('js')) ? 'js' : 'assets/js';
109+
copy(__DIR__.'/react-sync-stubs/app.js', resource_path("$js_path/app.js"));
96110
}
97111
}

src/react-sync-stubs/webpack.mix.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ let mix = require('laravel-mix');
1111
|
1212
*/
1313

14-
mix.react('resources/assets/js/app.js', 'public/js')
15-
.sass('resources/assets/sass/app.scss', 'public/css');
14+
mix.react('resources/js/app.js', 'public/js')
15+
.sass('resources/sass/app.scss', 'public/css');

src/react-sync-stubs/webpack56.mix.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let mix = require('laravel-mix');
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Mix Asset Management
6+
|--------------------------------------------------------------------------
7+
|
8+
| Mix provides a clean, fluent API for defining some Webpack build steps
9+
| for your Laravel application. By default, we are compiling the Sass
10+
| file for the application as well as bundling up all the JS files.
11+
|
12+
*/
13+
14+
mix.react('resources/assets/js/app.js', 'public/js')
15+
.sass('resources/assets/sass/app.scss', 'public/css');

src/views/page_js.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
// This way you can always get the current state of the page easily via JS and creates a defacto API that can be used to access any application state.
88
99
(function(){
10-
10+
11+
//☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️
12+
1113
var {{config('react_sync.global_variable_name', 'ReactSyncAppData')}} = function(){
1214
var _this = this;
1315
_this.config = {!! collect(config('react_sync')) !!};
@@ -50,6 +52,8 @@
5052
5153
window.ReactSyncGlobal = '{{config('react_sync.global_variable_name', 'ReactSyncAppData')}}';
5254
55+
//☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️☠︎☣︎☠︎☣︎☠︎☠️
56+
5357
})();
5458
5559
</script>

0 commit comments

Comments
 (0)