diff --git a/gulp/generate-shims.js b/gulp/generate-shims.js deleted file mode 100644 index 36b1245..0000000 --- a/gulp/generate-shims.js +++ /dev/null @@ -1,43 +0,0 @@ -'use strict'; - -var gutil = require('gulp-util'); -var pluginStream = require('./plugin-stream'); - -var PLUGIN_NAME = 'cesium-sensors-generate-shims'; - -module.exports = function() { - var shims = {}; - - return pluginStream(PLUGIN_NAME, function(file, enc, cb) { - var contents = file.contents.toString(); - // Search for Cesium modules and add shim - // modules that pull from the Cesium global - - var cesiumRequireRegex = /'Cesium\/\w*\/(\w*)'/g; - var match; - while ((match = cesiumRequireRegex.exec(contents)) !== null) { - if (match[0] in shims) { - continue; - } - - shims[match[0]] = 'define(' + match[0] + ', function() { return Cesium[\'' + match[1] + '\']; });'; - } - - cb(); - }, function(cb) { - var shimContents = Object.keys(shims).map(function(key) { - return shims[key]; - }).join('\n'); - - shimContents = '\'use strict\';\n' + - '/* global Cesium */\n' + - shimContents; - - this.push(new gutil.File({ - path: 'cesium-shims.js', - contents: Buffer.from(shimContents) - })); - - cb(); - }); -}; diff --git a/gulp/plugin-stream.js b/gulp/plugin-stream.js deleted file mode 100644 index 4e633c1..0000000 --- a/gulp/plugin-stream.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -var gutil = require('gulp-util'); -var through = require('through2'); - -module.exports = function(PLUGIN_NAME, transform, flush) { - return through.obj(function(file, enc, cb) { - if (file.isNull()) { - cb(null, file); - return; - } - - if (file.isStream()) { - cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported')); - return; - } - - transform(file, enc, cb); - }, flush); -}; diff --git a/gulp/process-shaders.js b/gulp/process-shaders.js deleted file mode 100644 index fe999e2..0000000 --- a/gulp/process-shaders.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -var gutil = require('gulp-util'); -var pluginStream = require('./plugin-stream'); - -var PLUGIN_NAME = 'cesium-sensors-process-shaders'; - -module.exports = function() { - var shaderLicenseComments = []; - - return pluginStream(PLUGIN_NAME, function(file, enc, cb) { - var contents = file.contents.toString(); - contents = contents.replace(/\r\n/gm, '\n'); - - // eslint-disable-next-line no-useless-escape - var licenseComments = contents.match(/\/\*\*(?:[^*\/]|\*(?!\/)|\n)*?@license(?:.|\n)*?\*\//gm); - if (licenseComments !== null) { - shaderLicenseComments = shaderLicenseComments.concat(licenseComments); - } - - var newContents = []; - // Remove comments. Code ported from - // https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/filters/StripJavaComments.java - for (var i = 0; i < contents.length; ++i) { - var c = contents.charAt(i); - if (c === '/') { - c = contents.charAt(++i); - if (c === '/') { - while (c !== '\r' && c !== '\n' && i < contents.length) { - c = contents.charAt(++i); - } - } else if (c === '*') { - while (i < contents.length) { - c = contents.charAt(++i); - // eslint-disable-next-line max-depth - if (c === '*') { - c = contents.charAt(++i); - // eslint-disable-next-line max-depth - while (c === '*') { - c = contents.charAt(++i); - } - // eslint-disable-next-line max-depth - if (c === '/') { - c = contents.charAt(++i); - break; - } - } - } - } else { - --i; - c = '/'; - } - } - newContents.push(c); - } - - newContents = newContents.join(''); - newContents = newContents.replace(/\s+$/gm, '').replace(/^\s+/gm, '').replace(/\n+/gm, '\n'); - - cb(null, new gutil.File({ - path: file.relative, - contents: Buffer.from(newContents) - })); - }, function(cb) { - this.push(new gutil.File({ - path: 'shader-copyright-header.js', - contents: Buffer.from(shaderLicenseComments.join('\n')) - })); - cb(); - }); -}; diff --git a/gulpfile.js b/gulpfile.js index 757885d..c28f242 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,165 +3,167 @@ var fs = require('fs'); var path = require('path'); -var es = require('event-stream'); -var globby = require('globby'); var gulp = require('gulp'); -var assign = require('lodash.assign'); - -// load plugins -var browserSync = require('browser-sync').create(); -var concat = require('gulp-concat'); +var through = require('through2'); var del = require('del'); -var gulpif = require('gulp-if'); -var order = require('gulp-order'); -var requirejsOptimize = require('gulp-requirejs-optimize'); -var size = require('gulp-size'); var xo = require('gulp-xo'); +var glsl = require('gulp-glsl'); -var reload = browserSync.reload; +var rollup = require('rollup'); +var { string } = require('rollup-plugin-string'); +var { terser } = require('rollup-plugin-terser'); -var generateShims = require('./gulp/generate-shims'); -var processShaders = require('./gulp/process-shaders'); +var browserSync = require('browser-sync').create(); -var runLint = function(src) { +var reload = browserSync.reload; + +function runLint(src) { return gulp.src(src) .pipe(xo()); -}; - -gulp.task('lint', () => { +} +function lint() { return runLint(['lib/**/*.js', 'gulp/**/*.js', 'gulpfile.js']); -}); +} +exports.lint = lint; + +function clean() { + return del(['coverage', '.tmp', 'dist']); +} +exports.clean = clean; -gulp.task('shaders', () => { +function preprocessShaders() { return gulp.src('lib/**/*.glsl') - .pipe(processShaders()) - .pipe(gulp.dest('.tmp/shaders')); -}); + .pipe(glsl({ format: 'raw', ext: '.glsl' })) + .pipe(gulp.dest('.tmp')); +} -gulp.task('create-main-js', () => { +function preprocessJs() { return gulp.src(['lib/**/*.js']) - .pipe(gulpif('!main.js', generateShims())) - .pipe(order([ - '!main.js', - 'main.js' - ])) - .pipe(concat('main.js')) .pipe(gulp.dest('.tmp')); -}); - -function getCopyrightHeaders() { - var copyrightHeader = fs.readFileSync('lib/copyright-header.js').toString(); - var shaderCopyrightHeader = fs.readFileSync('.tmp/shaders/shader-copyright-header.js').toString(); - - return copyrightHeader + '\n' + shaderCopyrightHeader; } -function optimize(options) { - var source = path.join(options.baseUrl, options.include) + '.js'; - return gulp.src(source) - .pipe(requirejsOptimize(options)); +function getCopyrightHeaders() { + return fs.readFileSync('lib/copyright-header.js').toString(); } -gulp.task('scripts', gulp.series('create-main-js', 'shaders', () => { - var copyright = getCopyrightHeaders(); - - var requirejsOptions = { - name: '../node_modules/almond/almond', - - wrap: { - start: copyright + '(function() {', - end: '})();' - }, - - useStrict: true, - - inlineText: true, - stubModules: ['text'], - - skipModuleInsertion: true, - - baseUrl: 'lib', - - include: '../.tmp/main', - paths: { - text: '../node_modules/requirejs-text/text' - } - }; - - var unminified = optimize(assign({}, requirejsOptions, { - out: 'cesium-sensor-volumes.js', - optimize: 'none' - })); +async function buildEs() { + const bundle = await rollup.rollup({ + input: '.tmp/cesium-sensor-volumes.js', + plugins: [ + string({ + include: '**/*.glsl' + }) + ], + external: id => /Cesium/.test(id) + }); - var minifiedOptions = assign({}, requirejsOptions, { - out: 'cesium-sensor-volumes.min.js', - optimize: 'uglify2' + await bundle.write({ + file: 'dist/cesium-sensor-volumes.es.js', + format: 'es', + banner: getCopyrightHeaders() + }); + await bundle.write({ + file: 'dist/cesium-sensor-volumes.es.min.js', + format: 'es', + plugins: [terser({ + format: { + comments: function(node, comment) { + if (comment.type === 'comment2') { + return /Copyright/i.test(comment.value); + } + } + } + })], + banner: getCopyrightHeaders() }); +} +exports.buildEs = gulp.series(clean, gulp.parallel(preprocessShaders, preprocessJs), buildEs); + +function generateShims() { + // Search for Cesium modules and add shim modules that pull from the Cesium global + return gulp.src(['./dist/cesium-sensor-volumes.es.js']) + .pipe(through.obj(function(file, _, cb) { + if (file.isBuffer()) { + var cesiumRequireRegex = /import (\w*) from 'Cesium\/\w*\/(\w*)'/; + const output = file.contents.toString().split('\n').map(line => { + const match = cesiumRequireRegex.exec(line); + if (match) { + return `const ${match[1]} = Cesium['${match[2]}'];`; + } + return line; + }); + file.contents = Buffer.from(output.join('\n')); + } + cb(null, file); + })) + .pipe(gulp.dest('.tmp/shimmed')); +} - // Use minified versions of shaders - globby.sync(['lib/**/*.glsl']).forEach(function(shader) { - shader = path.relative('lib', shader).replace(/\\/g, '/').replace(/\.glsl$/, ''); - minifiedOptions.paths[shader] = path.join('../.tmp/shaders', shader); +async function buildUmd() { + const bundle = await rollup.rollup({ + input: '.tmp/shimmed/cesium-sensor-volumes.es.js' }); + await bundle.write({ + file: 'dist/cesium-sensor-volumes.js', + name: 'CesiumSensorVolumes', + format: 'umd' + }); + await bundle.write({ + file: 'dist/cesium-sensor-volumes.min.js', + name: 'CesiumSensorVolumes', + plugins: [terser({ + format: { + comments: function(node, comment) { + if (comment.type === 'comment2') { + return /Copyright/i.test(comment.value); + } + } + } + })], + format: 'umd' + }); +} +exports.build = gulp.series(exports.buildEs, generateShims, buildUmd); - var minified = optimize(minifiedOptions); +exports.buildReload = gulp.series(exports.build, reload); - return es.merge(unminified, minified) - .pipe(gulp.dest('dist')); -})); +function run(cb) { + browserSync.init({ + server: '.' + }, cb); +} -gulp.task('clean', () => del(['coverage', '.tmp', 'dist'])); +function watch(cb) { + gulp.watch(['examples/**/*.html', 'examples/**/*.czml'], reload); + gulp.watch(['lib/**/*.glsl'], exports.buildReload); + gulp.watch(['lib/**/*.js'], exports.buildReload); + cb(); +} +exports.serve = gulp.series(exports.build, run, watch); -gulp.task('test-lint', () => { +function lintTest() { return runLint(['test/**/*.js']); -}); +} function test(done, options) { var Server = require('karma').Server; - var server = new Server(assign({ + var server = new Server(Object.assign({ configFile: path.join(__dirname, '/test/karma.conf.js'), singleRun: true }, options), done); server.start(); } +exports.test = gulp.series(lintTest, test); -gulp.task('test', gulp.series('test-lint', done => { - test(done); -})); - -gulp.task('test-ci', gulp.series('test-lint', done => { +function testCI(done) { test(done, { browsers: ['Electron'], client: { args: [true] } }); -})); - -gulp.task('build', gulp.series('lint', 'scripts', () => { - return gulp.src('dist/**/*') - .pipe(size({ title: 'build', gzip: true })); -})); - -gulp.task('build-reload', gulp.series('build', reload)); - -gulp.task('run', done => { - browserSync.init({ - server: '.' - }, done); -}); - -gulp.task('watch', done => { - gulp.watch(['examples/**/*.html', 'examples/**/*.czml'], reload); - gulp.watch(['lib/**/*.glsl'], gulp.series('build-reload')); - gulp.watch(['lib/**/*.js'], gulp.series('build-reload')); - done(); -}); - -gulp.task('serve', gulp.series('build', 'run', 'watch', done => done())); - -gulp.task('ci', gulp.series('lint', 'test-ci', 'build', done => done())); - -gulp.task('default', gulp.series('clean', 'build', done => done())); +} +exports.testCI = gulp.series(lintTest, testCI); +exports.ci = gulp.series(lint, testCI, exports.build); diff --git a/lib/cesium-sensor-volumes.js b/lib/cesium-sensor-volumes.js index 6d78d5d..3a3a1d2 100755 --- a/lib/cesium-sensor-volumes.js +++ b/lib/cesium-sensor-volumes.js @@ -1,5 +1,3 @@ -'use strict'; - import initialize from './initialize'; import ConicSensorGraphics from './conic/conic-sensor-graphics'; import ConicSensorVisualizer from './conic/conic-sensor-visualizer'; @@ -13,12 +11,12 @@ import RectangularSensorVisualizer from './rectangular/rectangular-sensor-visual initialize(); export default { - ConicSensorGraphics: ConicSensorGraphics, - ConicSensorVisualizer: ConicSensorVisualizer, - CustomPatternSensorGraphics: CustomPatternSensorGraphics, - CustomPatternSensorVisualizer: CustomPatternSensorVisualizer, - CustomSensorVolume: CustomSensorVolume, - RectangularPyramidSensorVolume: RectangularPyramidSensorVolume, - RectangularSensorGraphics: RectangularSensorGraphics, - RectangularSensorVisualizer: RectangularSensorVisualizer + ConicSensorGraphics, + ConicSensorVisualizer, + CustomPatternSensorGraphics, + CustomPatternSensorVisualizer, + CustomSensorVolume, + RectangularPyramidSensorVolume, + RectangularSensorGraphics, + RectangularSensorVisualizer }; diff --git a/lib/custom/custom-sensor-volume.js b/lib/custom/custom-sensor-volume.js index 4d2bff7..6d58871 100755 --- a/lib/custom/custom-sensor-volume.js +++ b/lib/custom/custom-sensor-volume.js @@ -22,9 +22,12 @@ import CullFace from 'Cesium/Scene/CullFace'; import Material from 'Cesium/Scene/Material'; import SceneMode from 'Cesium/Scene/SceneMode'; -const SensorVolume = require('../sensor-volume.glsl'); -const CustomSensorVolumeFS = require('./custom-sensor-volume-fs.glsl'); -const CustomSensorVolumeVS = require('./custom-sensor-volume-vs.glsl'); +// eslint-disable-next-line import/extensions +import SensorVolume from '../sensor-volume.glsl'; +// eslint-disable-next-line import/extensions +import CustomSensorVolumeFS from './custom-sensor-volume-fs.glsl'; +// eslint-disable-next-line import/extensions +import CustomSensorVolumeVS from './custom-sensor-volume-vs.glsl'; const attributeLocations = { position: 0, diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index c49c454..0000000 --- a/lib/main.js +++ /dev/null @@ -1,22 +0,0 @@ -// eslint-disable-next-line import/no-dynamic-require -require([ - 'cesium-sensor-volumes' -], function( - CesiumSensorVolumes -) { - 'use strict'; - /* global window, self */ - - var scope; - if (typeof window === 'undefined') { - if (typeof self === 'undefined') { - scope = {}; - } else { - scope = self; - } - } else { - scope = window; - } - - scope.CesiumSensorVolumes = CesiumSensorVolumes; -}, undefined, true); diff --git a/package.json b/package.json index c2346d4..63c2ccf 100644 --- a/package.json +++ b/package.json @@ -1,38 +1,29 @@ { "name": "cesium-sensor-volumes", - "version": "1.32.0", + "version": "1.33.0", "description": "A Cesium plugin for visualizing sensor volumes.", - "homepage": "https://cesiumjs.org", + "homepage": "https://github.com/Flowm/cesium-sensor-volumes", "license": "Apache-2.0", "repository": { "type": "git", - "url": "https://github.com/jlouns/cesium-sensor-volumes.git" + "url": "https://github.com/Flowm/cesium-sensor-volumes.git" }, "keywords": [ "cesium" ], + "main": "dist/cesium-sensor-volumes.js", + "module": "dist/cesium-sensor-volumes.es.js", "dependencies": { - "cesium": "1.77", - "requirejs": "^2.3.3", - "requirejs-text": "^2.0.15" + "cesium": "1.77" }, "devDependencies": { - "@babel/core": "^7.12.10", - "@babel/preset-env": "^7.12.11", - "almond": "^0.3.3", "browser-sync": "^2.18.8", "del": "^2.2.2", "electron": "1.6.5", - "event-stream": "^3.3.4", - "globby": "^6.1.0", + "glsl-strip-comments": "^1.0.0", "gulp": "^4.0.2", "gulp-babel": "^8.0.0", - "gulp-concat": "^2.6.1", - "gulp-if": "^2.0.2", - "gulp-order": "^1.1.1", - "gulp-requirejs-optimize": "^1.2.0", - "gulp-size": "^2.0.0", - "gulp-util": "^3.0.8", + "gulp-glsl": "^1.2.4", "gulp-xo": "^0.15.0", "jasmine-core": "^2.5.2", "karma": "^1.6.0", @@ -41,13 +32,15 @@ "karma-electron-launcher": "^0.2.0", "karma-jasmine": "^1.1.0", "karma-requirejs": "^1.1.0", - "lodash.assign": "^4.2.0", - "merge-stream": "^1.0.1", + "rollup": "^2.36.1", + "rollup-plugin-string": "^3.0.0", + "rollup-plugin-terser": "^7.0.2", "through2": "^2.0.3" }, "scripts": { "build": "gulp build", "start": "gulp serve", + "lint": "gulp lint", "test": "gulp test", "ci": "gulp ci", "gulp": "gulp" diff --git a/transform-to-es6.sh b/transform-to-es6.sh deleted file mode 100755 index 220038b..0000000 --- a/transform-to-es6.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash -set -x - -npm install -D jscodeshift 5to6-codemod - -node_modules/jscodeshift/bin/jscodeshift.sh -t node_modules/5to6-codemod/transforms/amd.js lib --useTabs=true -node_modules/jscodeshift/bin/jscodeshift.sh -t node_modules/5to6-codemod/transforms/cjs.js lib --useTabs=true -git checkout -- lib/main.js - -sed -i "s/^var /const /" lib/conic/conic-sensor-graphics.js -sed -i "s/^var /const /" lib/conic/conic-sensor-visualizer.js -sed -i "s/^var /const /" lib/custom/custom-pattern-sensor-graphics.js -sed -i "s/^var /const /" lib/custom/custom-pattern-sensor-visualizer.js -sed -i "s/^var /const /" lib/custom/custom-sensor-volume.js -sed -i "s/^var /const /" lib/rectangular/rectangular-pyramid-sensor-volume.js -sed -i "s/^var /const /" lib/rectangular/rectangular-sensor-graphics.js -sed -i "s/^var /const /" lib/rectangular/rectangular-sensor-visualizer.js