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

Use NPM for management of client side dependencies #942

Merged
merged 7 commits into from
Jun 15, 2017
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ DEVELOPMENT

Selfoss uses [composer](https://getcomposer.org/) for installing external libraries. When you clone the repository you have to issue `composer install` to retrieve the external sources.

For the client side, you will also need JavaScript dependencies installed by calling `npm install` in the `public` directory.

If you want to create a package with all the dependencies bundled, you will additionally require [grunt](https://gruntjs.com/). After installing it, execute `npm install` in the selfoss directory to obtain the required tasks. Then you can run `grunt` command to produce a zipball. As a bonus, you can also use `grunt install` as a shortcut for installing the selfoss dependencies described above.

CREDITS
-------

Expand Down
30 changes: 27 additions & 3 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,25 @@ function isNotUnimportant(dest) {
}

module.exports = function(grunt) {
const requiredAssets = (function() {
const files = grunt.file.readJSON('public/package.json').extra.requiredFiles;

return files.css.concat(files.js);
})();

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

/* Install client-side dependencies */
auto_install: {
subdir: {
options: {
cwd: 'public',
npm: '--production'
}
}
},

/* version text replace */
replace: {
version: {
Expand Down Expand Up @@ -88,9 +103,14 @@ module.exports = function(grunt) {
{ expand: true, cwd: 'helpers/', src: ['**'], dest: '/helpers'},
{ expand: true, cwd: 'vendor/', src: ['**'], dest: '/vendor', filter: isNotUnimportant},

// public = don't zip all.js and all.css
// do not pack bundled assets and assets not listed in index.php
{ expand: true, cwd: 'public/', src: ['**'], dest: '/public', filter: function(file) {
return file.indexOf('all.js') === -1 && file.indexOf('all.css') === -1;
const bundle = file === 'public/all.js' || file === 'public/all.css';
const packageDesc = file === 'public/package.json';
const thirdPartyRubbish = file.startsWith('public/node_modules/') && requiredAssets.indexOf(file) === -1;
const allowed = !bundle && !packageDesc && !thirdPartyRubbish;

return allowed;
}},

// copy data: only directory structure and .htaccess for deny
Expand All @@ -115,6 +135,7 @@ module.exports = function(grunt) {
}
});

grunt.loadNpmTasks('grunt-auto-install');
grunt.loadNpmTasks('grunt-text-replace');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.loadNpmTasks('grunt-composer');
Expand All @@ -130,7 +151,10 @@ module.exports = function(grunt) {
}
});

grunt.registerTask('default', ['composer:install:no-dev:optimize-autoloader:prefer-dist', 'versionupdater', 'compress']);
grunt.registerTask('client:install', 'Install client-side dependencies.', ['auto_install']);
grunt.registerTask('server:install', 'Install server-side dependencies.', ['composer:install:no-dev:optimize-autoloader:prefer-dist']);
grunt.registerTask('install', 'Install both client-side and server-side dependencies.', ['client:install', 'server:install']);
grunt.registerTask('default', ['install', 'versionupdater', 'compress']);
grunt.registerTask('version', ['versionupdater']);
grunt.registerTask('zip', ['compress']);
};
5 changes: 5 additions & 0 deletions helpers/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public static function maxmtime(array $filePaths) {
$maxmtime = 0;
foreach ($filePaths as $filePath) {
$fullPath = \F3::get('BASEDIR') . '/' . $filePath;

if (!file_exists($fullPath)) {
throw new \Exception("Missing file “${filePath}”. Did you install the dependencies using npm?");
}

$maxmtime = max($maxmtime, filemtime($fullPath));
}

Expand Down
33 changes: 5 additions & 28 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,18 @@
// init authentication
$f3->set('auth', new \helpers\Authentication());

/** @var stdClass JS client package manifest */
$clientPackage = json_decode(file_get_contents(__DIR__ . '/public/package.json'));

// define js files
$js = [
'public/js/jquery-2.1.1.min.js',
'public/js/jquery-ui.js',
'public/js/jquery.mCustomScrollbar.min.js',
'public/js/jquery.mousewheel.min.js',
'public/js/lazy-image-loader.js',
'public/js/spectrum.js',
'public/js/jquery.hotkeys.js',
'public/js/selfoss-base.js',
'public/js/selfoss-shares.js',
'public/js/selfoss-db.js',
'public/js/selfoss-ui.js',
'public/js/selfoss-events.js',
'public/js/selfoss-events-navigation.js',
'public/js/selfoss-events-search.js',
'public/js/selfoss-events-entries.js',
'public/js/selfoss-events-entriestoolbar.js',
'public/js/selfoss-events-sources.js',
'public/js/selfoss-shortcuts.js',
'public/js/jquery.fancybox.pack.js'
];
$js = $clientPackage->extra->requiredFiles->js;
if (file_exists('user.js')) {
$js[] = 'user.js';
}
$f3->set('js', $js);

// define css files
$css = [
'public/css/jquery.mCustomScrollbar.css',
'public/css/jquery.fancybox.css',
'public/css/spectrum.css',
'public/css/reset.css',
'public/css/style.css'
];
$css = $clientPackage->extra->requiredFiles->css;
if (file_exists('user.css')) {
$css[] = 'user.css';
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-auto-install": "^0.3.1",
"grunt-composer": "^0.4.5",
"grunt-contrib-compress": "^0.10.0",
"grunt-text-replace": "~0.3"
Expand Down
Loading