import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { rollup } from 'rollup'; const root_dir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const root_info = JSON.parse(fs.readFileSync(path.join(root_dir, 'package.json'))); async function build(options) { let bundle; let success = true; try { bundle = await rollup(options.input); await bundle.write(options.output); } catch(error) { success = false; console.error(`Error building ${options}`); console.error(error); } if(bundle) { await bundle.close(); } return success; } for(const package_path of fs.globSync(root_info.workspaces)) { const pkg_info = JSON.parse(fs.readFileSync(path.join(root_dir, package_path, 'package.json'))); const build_dir = path.join('build', 'packages', pkg_info.name); const dist_dir = path.join(build_dir, 'dist'); // Building main package fs.mkdirSync(dist_dir, {recursive: true}); if(!await build({ input: { input: path.join(build_dir, 'src', 'index.js') }, output: { compact: true, extend: true, file: `${dist_dir}/${pkg_info.name}.js`, name: 'OSM', format: 'iife' }})) process.exit(1); // Building tests const html_test_template = fs.readFileSync(path.join(root_dir, 'scripts', 'test_template.html')).toString() .replaceAll('{{bundle}}', `${pkg_info.name}.js`) .replaceAll('{{data}}', '/data/shibuya.osm'); for(const test_file of fs.globSync(path.join(build_dir, 'tests', '*.js'))) { const test_name = path.parse(test_file).name; if(!await build({ input: { input: test_file, external: ['osm-map-nano'], }, output: { compact: true, globals: { 'osm-map-nano': 'OSM' }, extend: true, file: `${dist_dir}/${test_name}.js`, name: 'OSM', format: 'iife' }})) process.exit(1); fs.writeFileSync(`${dist_dir}/${test_name}.html`, html_test_template.replaceAll('{{test_script}}', `${test_name}.js`).replaceAll('{{test_name}}', test_name)); } }