@@ -40,6 +40,7 @@ import { preprocess } from "./external/builder/builder.mjs";
4040import relative from "metalsmith-html-relative" ;
4141import rename from "gulp-rename" ;
4242import replace from "gulp-replace" ;
43+ import sourcemaps from "gulp-sourcemaps" ;
4344import stream from "stream" ;
4445import TerserPlugin from "terser-webpack-plugin" ;
4546import Vinyl from "vinyl" ;
@@ -1526,27 +1527,13 @@ gulp.task("types", function (done) {
15261527} ) ;
15271528
15281529function buildLibHelper ( bundleDefines , inputStream , outputDir ) {
1529- function preprocessLib ( content ) {
1530- const skipBabel = bundleDefines . SKIP_BABEL ;
1531- content = babel . transform ( content , {
1532- sourceType : "module" ,
1533- presets : skipBabel
1534- ? undefined
1535- : [
1536- [
1537- "@babel/preset-env" ,
1538- { ...BABEL_PRESET_ENV_OPTS , loose : false , modules : false } ,
1539- ] ,
1540- ] ,
1541- plugins : [ [ babelPluginPDFJSPreprocessor , ctx ] ] ,
1542- targets : BABEL_TARGETS ,
1543- } ) . code ;
1544- content = content . replaceAll (
1545- / ( \s f r o m \s " .* ?) (?: \/ s r c ) ( \/ [ ^ " ] * " ? ; ) $ / gm,
1546- ( all , prefix , suffix ) => prefix + suffix
1547- ) ;
1548- return licenseHeaderLibre + content ;
1549- }
1530+ const licenseHeader = fs
1531+ . readFileSync ( "./src/license_header.js" )
1532+ . toString ( )
1533+ . split ( "\n" )
1534+ . slice ( 1 , - 2 )
1535+ . map ( line => line . replace ( / ^ \s * \* \s ? / , "" ) ) ;
1536+
15501537 const ctx = {
15511538 rootPath : __dirname ,
15521539 defines : bundleDefines ,
@@ -1564,12 +1551,82 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
15641551 "web-null_l10n" : "../web/genericl10n.js" ,
15651552 } ,
15661553 } ;
1567- const licenseHeaderLibre = fs
1568- . readFileSync ( "./src/license_header_libre.js" )
1569- . toString ( ) ;
1570- return inputStream
1571- . pipe ( transform ( "utf8" , preprocessLib ) )
1572- . pipe ( gulp . dest ( outputDir ) ) ;
1554+ const enableSourceMaps = bundleDefines . TESTING ;
1555+
1556+ function preprocessLib ( file , _enc , callback ) {
1557+ const skipBabel = bundleDefines . SKIP_BABEL ;
1558+
1559+ if ( file . isNull ( ) ) {
1560+ return callback ( null , file ) ;
1561+ }
1562+
1563+ if ( file . isStream ( ) ) {
1564+ return callback ( new Error ( "Streaming not supported" ) ) ;
1565+ }
1566+
1567+ try {
1568+ // Calculate where the output file will be
1569+ const outputFilePath = path . join ( __dirname , outputDir , file . relative ) ;
1570+ const outputFileDir = path . dirname ( outputFilePath ) ;
1571+ // Calculate relative path from output directory to source file
1572+ const relativeSourcePath = path . relative ( outputFileDir , file . path ) ;
1573+
1574+ const result = babel . transform ( file . contents . toString ( ) , {
1575+ sourceType : "module" ,
1576+ presets : skipBabel
1577+ ? undefined
1578+ : [
1579+ [
1580+ "@babel/preset-env" ,
1581+ { ...BABEL_PRESET_ENV_OPTS , loose : false , modules : false } ,
1582+ ] ,
1583+ ] ,
1584+ plugins : [
1585+ [ babelPluginPDFJSPreprocessor , ctx ] ,
1586+ [
1587+ "add-header-comment" ,
1588+ {
1589+ header : licenseHeader ,
1590+ } ,
1591+ ] ,
1592+ ] ,
1593+ targets : BABEL_TARGETS ,
1594+ sourceMaps : enableSourceMaps ,
1595+ sourceFileName : relativeSourcePath ,
1596+ } ) ;
1597+
1598+ let code = result . code ;
1599+ code = code . replaceAll (
1600+ / ( \s f r o m \s " .* ?) (?: \/ s r c ) ( \/ [ ^ " ] * " ? ; ) $ / gm,
1601+ ( all , prefix , suffix ) => prefix + suffix
1602+ ) ;
1603+
1604+ file . contents = Buffer . from ( code ) ;
1605+ // Attach the source map to the file for gulp-sourcemaps
1606+ if ( result . map ) {
1607+ file . sourceMap = result . map ;
1608+ }
1609+
1610+ return callback ( null , file ) ;
1611+ } catch ( err ) {
1612+ return callback ( err ) ;
1613+ }
1614+ }
1615+
1616+ let pipeline = inputStream ;
1617+ if ( enableSourceMaps ) {
1618+ pipeline = pipeline . pipe ( sourcemaps . init ( { loadMaps : true } ) ) ;
1619+ }
1620+ pipeline = pipeline . pipe (
1621+ new stream . Transform ( {
1622+ objectMode : true ,
1623+ transform : preprocessLib ,
1624+ } )
1625+ ) ;
1626+ if ( enableSourceMaps ) {
1627+ pipeline = pipeline . pipe ( sourcemaps . write ( "." ) ) ;
1628+ }
1629+ return pipeline . pipe ( gulp . dest ( outputDir ) ) ;
15731630}
15741631
15751632function buildLib ( defines , dir ) {
@@ -1956,6 +2013,7 @@ gulp.task(
19562013 jasmineProcess = spawn ( "node" , options , { stdio : "inherit" } ) ;
19572014 } else {
19582015 const options = [
2016+ "--enable-source-maps" ,
19592017 "node_modules/jasmine/bin/jasmine" ,
19602018 "JASMINE_CONFIG_PATH=test/unit/clitests.json" ,
19612019 ] ;
0 commit comments