From 47fa26074b8a98efb78af8ad6c7225d329672a58 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Wed, 4 Mar 2020 20:18:08 +1100 Subject: [PATCH 01/10] Fix Cesium in Node.js 12+. --- Tools/package.json | 3 ++ gulpfile.js => gulpfile.cjs | 0 index.cjs | 42 +++++++++++++++++++++++++ index.js | 11 ------- package.json | 62 ++++++++++++++++++++----------------- 5 files changed, 78 insertions(+), 40 deletions(-) create mode 100644 Tools/package.json rename gulpfile.js => gulpfile.cjs (100%) create mode 100644 index.cjs delete mode 100644 index.js diff --git a/Tools/package.json b/Tools/package.json new file mode 100644 index 000000000000..1cd945a3bfd9 --- /dev/null +++ b/Tools/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/gulpfile.js b/gulpfile.cjs similarity index 100% rename from gulpfile.js rename to gulpfile.cjs diff --git a/index.cjs b/index.cjs new file mode 100644 index 000000000000..fd7d82e0492c --- /dev/null +++ b/index.cjs @@ -0,0 +1,42 @@ +/*eslint-env node*/ +'use strict'; + +var path = require('path'); +debugger; +// If in 'production' mode, use the combined/minified/optimized version of Cesium +if (process.env.NODE_ENV === 'production') { + console.log('prod'); + module.exports = require(path.join(__dirname, 'Build/Cesium/Cesium')); + return; +} + +try { + module.exports = require('esm')(module)('./Source/Cesium.js'); +} catch (e) { + if (e.code !== 'ERR_REQUIRE_ESM') { + throw e; + } + + // Node 12+ throws an exception when trying to load an ES module via `esm`, + // because `esm` loads Cesium ES modules via require instead of import, and + // that is an illegal thing to do because Cesium's package.json declares + // `"type": "module"`. Node doesn't know that `esm` is magically + // transforming the ES modules to CommonJS. + // + // So, we override the loader for .js files to skip the exception. + // Idea from here: + // https://github.com/standard-things/esm/issues/855#issuecomment-566825957 + var fs = require('fs'); + + var originalFunc = require.extensions['.js']; + require.extensions['.js'] = function(module, filename) { + if (filename.startsWith(__dirname)) { + var content = fs.readFileSync(filename, 'utf8'); + module._compile(content, filename); + return; + } + originalFunc(module, filename); + }; + + module.exports = require('esm')(module)('./Source/Cesium.js'); +} diff --git a/index.js b/index.js deleted file mode 100644 index 5e2426959370..000000000000 --- a/index.js +++ /dev/null @@ -1,11 +0,0 @@ -/*eslint-env node*/ -'use strict'; - -var path = require('path'); - -// If in 'production' mode, use the combined/minified/optimized version of Cesium -if (process.env.NODE_ENV === 'production') { - module.exports = require(path.join(__dirname, 'Build/Cesium/Cesium')); - return; -} -module.exports = require('esm')(module)('./Source/Cesium.js'); diff --git a/package.json b/package.json index 7922c04abfd7..8be1a8e24a4f 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,12 @@ "url": "https://github.com/CesiumGS/cesium/issues", "email": "cesium-dev@googlegroups.com" }, - "main": "index.js", - "module": "Source/Cesium.js", + "main": "index.cjs", + "module": "./Source/Cesium.js", + "exports": { + "require": "./index.cjs", + "import": "./Source/Cesium.js" + }, "type": "module", "dependencies": { "esm": "^3.2.25" @@ -81,35 +85,35 @@ "yargs": "^15.0.1" }, "scripts": { - "convertToModules": "gulp convertToModules", + "convertToModules": "gulp -f gulpfile.cjs convertToModules", "start": "node server.cjs", "startPublic": "node server.cjs --public", - "build": "gulp build", - "build-watch": "gulp build-watch", - "buildApps": "gulp buildApps", - "clean": "gulp clean", - "cloc": "gulp cloc", - "combine": "gulp combine", - "combineRelease": "gulp combineRelease", - "coverage": "gulp coverage", - "generateDocumentation": "gulp generateDocumentation", - "generateDocumentation-watch": "gulp generateDocumentation-watch", + "build": "gulp -f gulpfile.cjs build", + "build-watch": "gulp -f gulpfile.cjs build-watch", + "buildApps": "gulp -f gulpfile.cjs buildApps", + "clean": "gulp -f gulpfile.cjs clean", + "cloc": "gulp -f gulpfile.cjs cloc", + "combine": "gulp -f gulpfile.cjs combine", + "combineRelease": "gulp -f gulpfile.cjs combineRelease", + "coverage": "gulp -f gulpfile.cjs coverage", + "generateDocumentation": "gulp -f gulpfile.cjs generateDocumentation", + "generateDocumentation-watch": "gulp -f gulpfile.cjs generateDocumentation-watch", "eslint": "eslint \"./**/*.js\" \"./**/*.html\" --cache --quiet", - "makeZipFile": "gulp makeZipFile", - "minify": "gulp minify", - "minifyRelease": "gulp minifyRelease", - "release": "gulp release", - "build-specs": "gulp build-specs", - "test": "gulp test", - "test-all": "gulp test --all", - "test-webgl": "gulp test --include WebGL", - "test-non-webgl": "gulp test --exclude WebGL", - "test-webgl-validation": "gulp test --webglValidation", - "test-webgl-stub": "gulp test --webglStub", - "test-release": "gulp test --release", - "sortRequires": "gulp sortRequires", - "deploy-s3": "gulp deploy-s3", - "deploy-status": "gulp deploy-status", - "deploy-set-version": "gulp deploy-set-version" + "makeZipFile": "gulp -f gulpfile.cjs makeZipFile", + "minify": "gulp -f gulpfile.cjs minify", + "minifyRelease": "gulp -f gulpfile.cjs minifyRelease", + "release": "gulp -f gulpfile.cjs release", + "build-specs": "gulp -f gulpfile.cjs build-specs", + "test": "gulp -f gulpfile.cjs test", + "test-all": "gulp -f gulpfile.cjs test --all", + "test-webgl": "gulp -f gulpfile.cjs test --include WebGL", + "test-non-webgl": "gulp -f gulpfile.cjs test --exclude WebGL", + "test-webgl-validation": "gulp -f gulpfile.cjs test --webglValidation", + "test-webgl-stub": "gulp -f gulpfile.cjs test --webglStub", + "test-release": "gulp -f gulpfile.cjs test --release", + "sortRequires": "gulp -f gulpfile.cjs sortRequires", + "deploy-s3": "gulp -f gulpfile.cjs deploy-s3", + "deploy-status": "gulp -f gulpfile.cjs deploy-status", + "deploy-set-version": "gulp -f gulpfile.cjs deploy-set-version" } } From 3d6d5b7c0ffbadc0571fab1868790b8e8223b165 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Wed, 4 Mar 2020 20:44:27 +1100 Subject: [PATCH 02/10] Fix makeZipFile. --- gulpfile.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gulpfile.cjs b/gulpfile.cjs index 4bf30b5db619..6f0caf9f4a9c 100644 --- a/gulpfile.cjs +++ b/gulpfile.cjs @@ -341,7 +341,7 @@ gulp.task('makeZipFile', gulp.series('release', function() { 'Specs/**', 'ThirdParty/**', 'favicon.ico', - 'gulpfile.js', + 'gulpfile.cjs', 'server.cjs', 'package.json', 'LICENSE.md', @@ -457,7 +457,7 @@ function deployCesium(bucketName, uploadDirectory, cacheControl, done) { 'ThirdParty/**', '*.md', 'favicon.ico', - 'gulpfile.js', + 'gulpfile.cjs', 'index.html', 'package.json', 'server.cjs', From 289eac90fc15500395a42bb25944b101ba957335 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Wed, 4 Mar 2020 21:32:04 +1100 Subject: [PATCH 03/10] Fix CI problems. --- .eslintrc.json | 2 +- .travis.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 03dab94dea3a..514d51dab8a5 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -9,7 +9,7 @@ "overrides": [ { "files": [ - "index.js", + "index.cjs", "server.cjs", "gulpfile.js", "Source/Workers/transferTypedArrayTest.js" diff --git a/.travis.yml b/.travis.yml index ae9f7c80d9d7..0fd9153d4bb8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,7 @@ script: # Various Node.js smoke-screen tests - node -e "const Cesium = require('./');" - - NODE_ENV=development node index.js - - NODE_ENV=production node index.js + - NODE_ENV=development node index.cjs + - NODE_ENV=production node index.cjs - npm --silent run cloc From 9ddfd95dc707365a5c08b62562a2de3e1609e85e Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Wed, 4 Mar 2020 22:38:42 +1100 Subject: [PATCH 04/10] Remove debug code. --- index.cjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.cjs b/index.cjs index fd7d82e0492c..09e2efec8604 100644 --- a/index.cjs +++ b/index.cjs @@ -2,10 +2,9 @@ 'use strict'; var path = require('path'); -debugger; + // If in 'production' mode, use the combined/minified/optimized version of Cesium if (process.env.NODE_ENV === 'production') { - console.log('prod'); module.exports = require(path.join(__dirname, 'Build/Cesium/Cesium')); return; } @@ -37,6 +36,6 @@ try { } originalFunc(module, filename); }; - + module.exports = require('esm')(module)('./Source/Cesium.js'); } From 6eafc26c851a758e27d49901d23736a800afe434 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 20:42:36 +1100 Subject: [PATCH 05/10] Add .gulp.json instead of explicitly specifying cjs in npm cmds. --- .gulp.json | 5 +++++ package.json | 54 ++++++++++++++++++++++++++-------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 .gulp.json diff --git a/.gulp.json b/.gulp.json new file mode 100644 index 000000000000..96507cb26cf8 --- /dev/null +++ b/.gulp.json @@ -0,0 +1,5 @@ +{ + "flags": { + "gulpfile": "gulpfile.cjs" + } +} diff --git a/package.json b/package.json index 8be1a8e24a4f..565424efc639 100644 --- a/package.json +++ b/package.json @@ -85,35 +85,35 @@ "yargs": "^15.0.1" }, "scripts": { - "convertToModules": "gulp -f gulpfile.cjs convertToModules", + "convertToModules": "gulp convertToModules", "start": "node server.cjs", "startPublic": "node server.cjs --public", - "build": "gulp -f gulpfile.cjs build", - "build-watch": "gulp -f gulpfile.cjs build-watch", - "buildApps": "gulp -f gulpfile.cjs buildApps", - "clean": "gulp -f gulpfile.cjs clean", - "cloc": "gulp -f gulpfile.cjs cloc", - "combine": "gulp -f gulpfile.cjs combine", - "combineRelease": "gulp -f gulpfile.cjs combineRelease", - "coverage": "gulp -f gulpfile.cjs coverage", - "generateDocumentation": "gulp -f gulpfile.cjs generateDocumentation", - "generateDocumentation-watch": "gulp -f gulpfile.cjs generateDocumentation-watch", + "build": "gulp build", + "build-watch": "gulp build-watch", + "buildApps": "gulp buildApps", + "clean": "gulp clean", + "cloc": "gulp cloc", + "combine": "gulp combine", + "combineRelease": "gulp combineRelease", + "coverage": "gulp coverage", + "generateDocumentation": "gulp generateDocumentation", + "generateDocumentation-watch": "gulp generateDocumentation-watch", "eslint": "eslint \"./**/*.js\" \"./**/*.html\" --cache --quiet", - "makeZipFile": "gulp -f gulpfile.cjs makeZipFile", - "minify": "gulp -f gulpfile.cjs minify", - "minifyRelease": "gulp -f gulpfile.cjs minifyRelease", - "release": "gulp -f gulpfile.cjs release", - "build-specs": "gulp -f gulpfile.cjs build-specs", - "test": "gulp -f gulpfile.cjs test", - "test-all": "gulp -f gulpfile.cjs test --all", - "test-webgl": "gulp -f gulpfile.cjs test --include WebGL", - "test-non-webgl": "gulp -f gulpfile.cjs test --exclude WebGL", - "test-webgl-validation": "gulp -f gulpfile.cjs test --webglValidation", - "test-webgl-stub": "gulp -f gulpfile.cjs test --webglStub", - "test-release": "gulp -f gulpfile.cjs test --release", - "sortRequires": "gulp -f gulpfile.cjs sortRequires", - "deploy-s3": "gulp -f gulpfile.cjs deploy-s3", - "deploy-status": "gulp -f gulpfile.cjs deploy-status", - "deploy-set-version": "gulp -f gulpfile.cjs deploy-set-version" + "makeZipFile": "gulp makeZipFile", + "minify": "gulp minify", + "minifyRelease": "gulp minifyRelease", + "release": "gulp release", + "build-specs": "gulp build-specs", + "test": "gulp test", + "test-all": "gulp test --all", + "test-webgl": "gulp test --include WebGL", + "test-non-webgl": "gulp test --exclude WebGL", + "test-webgl-validation": "gulp test --webglValidation", + "test-webgl-stub": "gulp test --webglStub", + "test-release": "gulp test --release", + "sortRequires": "gulp sortRequires", + "deploy-s3": "gulp deploy-s3", + "deploy-status": "gulp deploy-status", + "deploy-set-version": "gulp deploy-set-version" } } From 3f60568ac23140fea2dcd6c958277cdf9c1515c1 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 22:54:39 +1100 Subject: [PATCH 06/10] Use built CJS module in CommonJS and esm package environments. --- gulpfile.cjs | 13 ++++++++----- index.cjs | 34 ++-------------------------------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/gulpfile.cjs b/gulpfile.cjs index 6f0caf9f4a9c..35d263944e29 100644 --- a/gulpfile.cjs +++ b/gulpfile.cjs @@ -115,6 +115,8 @@ function rollupWarning(message) { console.log(message); } +var copyrightHeader = fs.readFileSync(path.join('Source', 'copyrightHeader.js'), "utf8"); + function createWorkers() { rimraf.sync('Build/createWorkers'); @@ -997,7 +999,9 @@ function combineCesium(debug, optimizer, combineOutput) { return bundle.write({ format: 'umd', name: 'Cesium', - file: path.join(combineOutput, 'Cesium.js') + file: path.join(combineOutput, 'Cesium.js'), + sourcemap: debug, + banner: copyrightHeader }); }); } @@ -1047,7 +1051,9 @@ function combineWorkers(debug, optimizer, combineOutput) { }).then(function(bundle) { return bundle.write({ dir: path.join(combineOutput, 'Workers'), - format: 'amd' + format: 'amd', + sourcemap: true, + banner: copyrightHeader }); }); }); @@ -1073,7 +1079,6 @@ function combineJavaScript(options) { var removePragmas = options.removePragmas; var combineOutput = path.join('Build', 'combineOutput', optimizer); - var copyrightHeader = fs.readFileSync(path.join('Source', 'copyrightHeader.js')); var promise = Promise.join( combineCesium(!removePragmas, optimizer, combineOutput), @@ -1086,7 +1091,6 @@ function combineJavaScript(options) { //copy to build folder with copyright header added at the top var stream = gulp.src([combineOutput + '/**']) - .pipe(gulpInsert.prepend(copyrightHeader)) .pipe(gulp.dest(outputDirectory)); promises.push(streamToPromise(stream)); @@ -1391,7 +1395,6 @@ function buildCesiumViewer() { ); promise = promise.then(function() { - var copyrightHeader = fs.readFileSync(path.join('Source', 'copyrightHeader.js')); var stream = mergeStream( gulp.src('Build/Apps/CesiumViewer/CesiumViewer.js') .pipe(gulpInsert.prepend(copyrightHeader)) diff --git a/index.cjs b/index.cjs index 09e2efec8604..548d3f56d927 100644 --- a/index.cjs +++ b/index.cjs @@ -6,36 +6,6 @@ var path = require('path'); // If in 'production' mode, use the combined/minified/optimized version of Cesium if (process.env.NODE_ENV === 'production') { module.exports = require(path.join(__dirname, 'Build/Cesium/Cesium')); - return; -} - -try { - module.exports = require('esm')(module)('./Source/Cesium.js'); -} catch (e) { - if (e.code !== 'ERR_REQUIRE_ESM') { - throw e; - } - - // Node 12+ throws an exception when trying to load an ES module via `esm`, - // because `esm` loads Cesium ES modules via require instead of import, and - // that is an illegal thing to do because Cesium's package.json declares - // `"type": "module"`. Node doesn't know that `esm` is magically - // transforming the ES modules to CommonJS. - // - // So, we override the loader for .js files to skip the exception. - // Idea from here: - // https://github.com/standard-things/esm/issues/855#issuecomment-566825957 - var fs = require('fs'); - - var originalFunc = require.extensions['.js']; - require.extensions['.js'] = function(module, filename) { - if (filename.startsWith(__dirname)) { - var content = fs.readFileSync(filename, 'utf8'); - module._compile(content, filename); - return; - } - originalFunc(module, filename); - }; - - module.exports = require('esm')(module)('./Source/Cesium.js'); +} else { + module.exports = require(path.join(__dirname, 'Build/CesiumUnminified/Cesium')); } From e64a1e6ae3f120ceca1fe5a3c5c1502f89a9044e Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 23:04:27 +1100 Subject: [PATCH 07/10] Style. --- index.cjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.cjs b/index.cjs index 548d3f56d927..fcae39adb52a 100644 --- a/index.cjs +++ b/index.cjs @@ -6,6 +6,7 @@ var path = require('path'); // If in 'production' mode, use the combined/minified/optimized version of Cesium if (process.env.NODE_ENV === 'production') { module.exports = require(path.join(__dirname, 'Build/Cesium/Cesium')); -} else { - module.exports = require(path.join(__dirname, 'Build/CesiumUnminified/Cesium')); + return; } + +module.exports = require(path.join(__dirname, 'Build/CesiumUnminified/Cesium')); From 1520e3bcf57d6acaad2d1321f2afd0c7c4af3311 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 23:22:24 +1100 Subject: [PATCH 08/10] Mark Build dir as commonjs. --- gulpfile.cjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gulpfile.cjs b/gulpfile.cjs index 35d263944e29..c04c9a89c19b 100644 --- a/gulpfile.cjs +++ b/gulpfile.cjs @@ -152,6 +152,9 @@ function createWorkers() { gulp.task('build', function() { mkdirp.sync('Build'); + fs.writeFileSync('Build/package.json', JSON.stringify({ + type: 'commonjs' + }), "utf8"); glslToJavaScript(minifyShaders, 'Build/minifyShaders.state'); createCesiumJs(); createSpecList(); From e2fe87b643da5ca66e4c6cd6ffa997c3189c3f72 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 23:30:15 +1100 Subject: [PATCH 09/10] Only sourcemap workers in debug builds. --- gulpfile.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gulpfile.cjs b/gulpfile.cjs index c04c9a89c19b..6ae377e8d828 100644 --- a/gulpfile.cjs +++ b/gulpfile.cjs @@ -1055,7 +1055,7 @@ function combineWorkers(debug, optimizer, combineOutput) { return bundle.write({ dir: path.join(combineOutput, 'Workers'), format: 'amd', - sourcemap: true, + sourcemap: debug, banner: copyrightHeader }); }); From f67eb82ac812b6aed50c75b4f11a2a6e3fb20b5e Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Thu, 19 Mar 2020 23:45:13 +1100 Subject: [PATCH 10/10] Remove esm dependency, update CHANGES.md. --- CHANGES.md | 2 ++ package.json | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 49d72c91124b..005c551baa9d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,9 +6,11 @@ Change Log ##### Additions :tada: * Added basic underground rendering support. When the camera is underground the globe will be rendered as a solid surface and underground entities will not be culled. [#8572](https://github.com/AnalyticalGraphicsInc/cesium/pull/8572) +* The `CesiumUnminified` build now includes sourcemaps. ##### Fixes :wrench: +* Cesium can now be used in Node.JS 12 and later, with or without `--experimental-modules`. It can still be used in earlier versions as well. * Interacting with the Cesium canvas will now blur the previously focused element. This prevents unintended modification of input elements when interacting with the globe. * `TileMapServiceImageryProvider` will now force `minimumLevel` to 0 if the `tilemapresource.xml` metadata request fails and the `rectangle` is too large for the given detail level [#8448](https://github.com/AnalyticalGraphicsInc/cesium/pull/8448) * Fixed ground atmosphere rendering when using a samller ellipsoid. [#8683](https://github.com/CesiumGS/cesium/issues/8683) diff --git a/package.json b/package.json index 565424efc639..19b6b363dcbf 100644 --- a/package.json +++ b/package.json @@ -36,9 +36,6 @@ "import": "./Source/Cesium.js" }, "type": "module", - "dependencies": { - "esm": "^3.2.25" - }, "devDependencies": { "aws-sdk": "^2.531.0", "bluebird": "^3.4.6",