index.js (2047B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 const path = require("path"); 6 const util = require("util"); 7 const fs = require("fs"); 8 const _ = require("lodash"); 9 const Bundler = require("parcel-bundler"); 10 const convertSourceMap = require("convert-source-map"); 11 12 const TARGET_NAME = "parcel"; 13 14 module.exports = exports = async function(tests, dirname) { 15 const fixtures = []; 16 for (const [name, input] of tests) { 17 const testFnName = _.camelCase(`${TARGET_NAME}-${name}`); 18 const evalMaps = name.match(/-eval/); 19 20 console.log(`Building ${TARGET_NAME} test ${name}`); 21 22 const scriptPath = path.join(dirname, "output", TARGET_NAME, `${name}.js`); 23 const bundler = new Bundler(input, { 24 global: testFnName, 25 hmr: false, 26 watch: false, 27 logLevel: 0, 28 publicURL: ".", 29 30 outDir: path.dirname(scriptPath), 31 outFile: path.basename(scriptPath), 32 }); 33 34 await bundler.bundle(); 35 36 let code = fs.readFileSync(scriptPath, "utf8"); 37 38 // Parcel doesn't offer a way to explicitly export the default property 39 // of the default object. 40 code += `\n;${testFnName} = ${testFnName}.default;`; 41 fs.writeFileSync(scriptPath, code); 42 43 44 const mapMatch = convertSourceMap.mapFileCommentRegex.exec(code); 45 const mapPath = path.resolve(path.dirname(scriptPath), mapMatch[1] || mapMatch[2]); 46 47 const map = JSON.parse(fs.readFileSync(mapPath, "utf8")); 48 map.sourceRoot = undefined; 49 map.sources = map.sources.map(source => 50 source.replace(/^/, `${TARGET_NAME}://./${name}/`) 51 ); 52 fs.writeFileSync(mapPath, JSON.stringify(map)); 53 54 fixtures.push({ 55 name, 56 testFnName, 57 scriptPath, 58 assets: [ 59 scriptPath, 60 `${path.join(path.dirname(scriptPath), path.basename(scriptPath, path.extname(scriptPath)))}.map`, 61 ] 62 }); 63 } 64 65 return { 66 target: TARGET_NAME, 67 fixtures 68 }; 69 };