From 93326c049b40ebe8823bb9f3e9c225788eaede78 Mon Sep 17 00:00:00 2001 From: Peter Rushforth Date: Thu, 14 Nov 2024 13:25:38 -0500 Subject: [PATCH] Remove grunt-rollup grunt plugin, replace with customRollup task, config done via external rollup.config.js. Remove uglify grunt plugin, replace with terser rollup plugin (does a better job with source maps and chained minification) Add nrcan-basemap grunt task for sibling project to get nrcan vector tile basemap working with pmtiles. # Conflicts: # Gruntfile.js --- Gruntfile.js | 79 +++++++++++++++++------------------------------- package.json | 7 ++--- rollup.config.js | 33 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 rollup.config.js diff --git a/Gruntfile.js b/Gruntfile.js index b2b880ab8..44d8b0aa6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,8 +1,7 @@ module.exports = function(grunt) { + const rollup = require('rollup'); + const rollupConfig = require('./rollup.config.js'); const Diff = require('diff'); - const nodeResolve = require('@rollup/plugin-node-resolve'); - const loadLocalePlugin = require('./load-locales.js'); - const alias = require('@rollup/plugin-alias'); grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), cssmin: { @@ -12,23 +11,10 @@ module.exports = function(grunt) { }, combine: { files: { - 'dist/mapml.css': ['node_modules/leaflet/dist/leaflet.css', 'node_modules/leaflet.locatecontrol/dist/L.Control.Locate.css', 'src/mapml.css'] + 'dist/mapml.css': ['node_modules/leaflet/dist/leaflet.css', 'node_modules/leaflet.locatecontrol/dist/L.Control.Locate.css', 'src/mapml.css'] } } }, - uglify: { - options: { - banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n', - sourceMap: { - includeSources: true - } - }, - dist: { - files: { - 'dist/mapml.js': ['<%= rollup.main.dest %>'] - } - } - }, jshint: { files: ['Gruntfile.js', 'src/**/*.js','test/**/*.spec.js'], options: { @@ -49,7 +35,7 @@ module.exports = function(grunt) { tasks: ['jshint'] }, copy : { - main : { + main : { files: [ { expand: true, @@ -100,7 +86,7 @@ module.exports = function(grunt) { expand: true, src: ['dist/*'], dest: '../experiments' - } + } ] }, extension: { @@ -152,29 +138,7 @@ module.exports = function(grunt) { src: ['../web-map-doc/dist'] } }, - rollup: { - options: { - format: 'es', - plugins: [ - nodeResolve(), - loadLocalePlugin(), - alias({ - entries: [ - { - find: 'leaflet', - replacement: 'leaflet/dist/leaflet-src.esm.js' - } - ] - }) - ], - external: './pmtilesRules.js' - }, - main: { - dest: 'dist/mapmlviewer.js', - src: 'src/mapml/index.js' // Only one source file is permitted - } - }, - prettier: { + prettier: { options: { // https://prettier.io/docs/en/options.html progress: true @@ -188,22 +152,35 @@ module.exports = function(grunt) { } }); - grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTasks('grunt-contrib-clean'); - grunt.loadNpmTasks('grunt-rollup'); grunt.loadNpmTasks('grunt-prettier'); + // "grunt-rollup" plugin seems no longer maintained + grunt.registerTask('customRollup', 'A custom Rollup build task', async function() { + const done = this.async(); + try { + // Use the configuration loaded from rollup.config.js + const bundle = await rollup.rollup(rollupConfig); + await bundle.write(rollupConfig.output); + + console.log('Rollup build completed successfully.'); + done(); + } catch (error) { + console.error('Rollup build failed:', error); + done(false); + } + }); grunt.registerTask('format', ['prettier', 'jshint']); - grunt.registerTask('default', ['clean:dist', 'copy:main', 'copy:images', 'format', 'rollup', - 'uglify', 'cssmin','clean:tidyup']); - grunt.registerTask('experiments',['clean:experiments','default','copy:experiments']); - grunt.registerTask('extension',['clean:extension','default','copy:extension']); - grunt.registerTask('geoserver',['clean:geoserver','default','copy:geoserver']); - grunt.registerTask('docs', ['clean:docs','default','copy:docs']); - grunt.registerTask('sync', ['default','experiments','extension','docs']); + grunt.registerTask('default', ['clean:dist', 'copy:main', 'copy:images', 'format', 'customRollup', 'cssmin']); + grunt.registerTask('experiments', ['clean:experiments', 'default', 'copy:experiments']); + grunt.registerTask('extension', ['clean:extension', 'default', 'copy:extension']); + grunt.registerTask('geoserver', ['clean:geoserver', 'default', 'copy:geoserver']); + grunt.registerTask('basemap', ['clean:basemap', 'default', 'copy:basemap']); + grunt.registerTask('docs', ['clean:docs', 'default', 'copy:docs']); + grunt.registerTask('sync', ['default', 'experiments', 'extension', 'docs']); }; diff --git a/package.json b/package.json index a2bf3d617..5f8311064 100644 --- a/package.json +++ b/package.json @@ -32,14 +32,15 @@ } ], "scripts": { - "postinstall": "npx playwright install --with-deps", - "test": "npx playwright test --retries=3 --workers=1" + "postinstall": "npx playwright install --with-deps", + "test": "npx playwright test --retries=3 --workers=1" }, "devDependencies": { "@playwright/test": "^1.39.0", "@rollup/plugin-alias": "^5.1.1", "@rollup/plugin-legacy": "^3.0.2", "@rollup/plugin-node-resolve": "^15.2.3", + "@rollup/plugin-terser": "^0.4.4", "mapml-extension": "git+https://github.com/Maps4HTML/mapml-extension", "diff": "^5.1.0", "express": "^4.17.1", @@ -49,11 +50,9 @@ "grunt-contrib-copy": "~1.0.0", "grunt-contrib-cssmin": "^4.0.0", "grunt-contrib-jshint": "~3.2.0", - "grunt-contrib-uglify": "~5.0.0", "grunt-contrib-watch": "~1.1.0", "grunt-html": "^15.2.2", "grunt-prettier": "^2.2.0", - "grunt-rollup": "^12.0.0", "leaflet": "^1.9.4", "leaflet.locatecontrol": "^0.81.1", "path": "^0.12.7", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 000000000..dac1b6399 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,33 @@ +const nodeResolve = require('@rollup/plugin-node-resolve'); +const alias = require('@rollup/plugin-alias'); +const loadLocalePlugin = require('./load-locales.js'); +const terser = require('@rollup/plugin-terser'); + +module.exports = { + input: 'src/mapml/index.js', + plugins: [ + nodeResolve(), + loadLocalePlugin(), + alias({ + entries: [ + { + find: 'leaflet', + replacement: 'leaflet/dist/leaflet-src.esm.js' + } + ] + }), + terser({ + compress: true, + mangle: true, + format: { + comments: false + }, + sourceMap: true // Maintain source maps even during minification + }) + ], + output: { + file: 'dist/mapml.js', + format: 'esm', + sourcemap: true + } +};