Skip to content

Commit dd561f2

Browse files
committed
Add assest file. closes #37
1 parent 9a46836 commit dd561f2

4 files changed

Lines changed: 38 additions & 11 deletions

File tree

assets.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"vendor": {
3+
"js": "vendor.d4412575.chunk.js"
4+
},
5+
"add": {
6+
"js": "add.c1f8097d.min.js"
7+
},
8+
"search": {
9+
"js": "search.2e1d3d6d.min.js"
10+
}
11+
}

class/Factory/React.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,24 @@ public static function getScript($filename)
3333
if (SYSTEMS_REACT_DEV) {
3434
$path = "dev/$filename.js";
3535
} else {
36-
$path = "build/$filename.js";
36+
$path = "build/" . React::getAssetPath($filename);
3737
}
3838
$script = "<script type='text/javascript' src='{$root_directory}$path'></script>";
3939
return $script;
4040
}
41+
42+
public static function getAssetPath($scriptName)
43+
{
44+
$rootDirectory = PHPWS_SOURCE_DIR . "mod/systemsinventory/";
45+
if (!is_file($rootDirectory . 'assets.json')) {
46+
exit('Missing assets.json file. Run "npm run build" in the systemsinventory directory.');
47+
}
48+
$jsonRaw = file_get_contents($rootDirectory . 'assets.json');
49+
$json = json_decode($jsonRaw, true);
50+
if (!isset($json[$scriptName]['js'])) {
51+
throw new \Exception('Script file not found among assets.');
52+
}
53+
return $json[$scriptName]['js'];
54+
}
4155

4256
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@babel/core": "^7.1.6",
1313
"@babel/preset-env": "^7.1.6",
1414
"@babel/preset-react": "^7.0.0",
15+
"assets-webpack-plugin": "^3.5.1",
1516
"babel-loader": "^8.0.5",
1617
"browser-sync": "^2.26.3",
1718
"browser-sync-webpack-plugin": "^2.2.2",

webpack.config.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,18 @@ module.exports = (env, argv) => {
7575
// require('webpack-bundle-analyzer').BundleAnalyzerPlugin
7676
// settings.plugins.push(new BundleAnalyzerPlugin()) const AssetsPlugin =
7777
// require('assets-webpack-plugin')
78-
settings.plugins.push(
79-
new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify('production')})
80-
)
78+
const AssetsPlugin = require('assets-webpack-plugin')
79+
settings.plugins.push(
80+
new webpack.DefinePlugin({'process.env.NODE_ENV': JSON.stringify('production')})
81+
)
8182

82-
// settings.plugins.push( new AssetsPlugin({filename: 'assets.json',
83-
// prettyPrint: true,}) )
84-
settings.output = {
85-
path: setup.path.join(setup.APP_DIR, 'build'),
86-
filename: '[name].js',
87-
chunkFilename: '[name].js'
88-
}
83+
settings.plugins.push( new AssetsPlugin({filename: 'assets.json',
84+
prettyPrint: true,}) )
85+
settings.output = {
86+
path: setup.path.join(setup.APP_DIR, 'build'),
87+
filename: '[name].[chunkhash:8].min.js',
88+
chunkFilename: '[name].[chunkhash:8].chunk.js'
89+
}
8990
}
9091
return settings
9192
}

0 commit comments

Comments
 (0)