@agoric/bundle-source
Advanced tools
Comparing version 1.2.3 to 1.2.4
@@ -6,2 +6,10 @@ # Change Log | ||
## [1.2.4](https://github.com/Agoric/agoric-sdk/compare/@agoric/bundle-source@1.2.3...@agoric/bundle-source@1.2.4) (2021-03-24) | ||
**Note:** Version bump only for package @agoric/bundle-source | ||
## [1.2.3](https://github.com/Agoric/agoric-sdk/compare/@agoric/bundle-source@1.2.2...@agoric/bundle-source@1.2.3) (2021-03-16) | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "@agoric/bundle-source", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Create source bundles from ES Modules", | ||
@@ -20,3 +20,3 @@ "parsers": { | ||
"devDependencies": { | ||
"@agoric/install-ses": "^0.5.3", | ||
"@agoric/install-ses": "^0.5.4", | ||
"ava": "^3.12.1", | ||
@@ -26,7 +26,7 @@ "nyc": "^15.1.0" | ||
"dependencies": { | ||
"@agoric/acorn-eventual-send": "^2.1.3", | ||
"@agoric/acorn-eventual-send": "^2.1.4", | ||
"@agoric/babel-parser": "^7.6.4", | ||
"@agoric/base64": "^0.1.3", | ||
"@agoric/base64": "^0.1.4", | ||
"@agoric/compartment-mapper": "^0.2.3", | ||
"@agoric/transform-eventual-send": "^1.4.3", | ||
"@agoric/transform-eventual-send": "^1.4.4", | ||
"@babel/generator": "^7.6.4", | ||
@@ -77,3 +77,3 @@ "@babel/traverse": "^7.8.3", | ||
}, | ||
"gitHead": "5ad5f483f05ba20d903b4423bfb4fa0d2ce75fd7" | ||
"gitHead": "be478ee84773b8e403cf896e53df28146a60f976" | ||
} |
203
src/index.js
@@ -39,2 +39,98 @@ /* global process */ | ||
} | ||
function rewriteComment(node, unmapLoc) { | ||
node.type = 'CommentBlock'; | ||
// Within comments... | ||
node.value = node.value | ||
// ...strip extraneous comment whitespace | ||
.replace(/^\s+/gm, ' ') | ||
// ...replace HTML comments with a defanged version to pass SES restrictions. | ||
.replace(HTML_COMMENT_START_RE, '<!X-') | ||
.replace(HTML_COMMENT_END_RE, '-X>') | ||
// ...replace import expressions with a defanged version to pass SES restrictions. | ||
.replace(IMPORT_RE, 'X$1$2') | ||
// ...replace end-of-comment markers | ||
.replace(/\*\//g, '*X/'); | ||
if (unmapLoc) { | ||
unmapLoc(node.loc); | ||
} | ||
// console.log(JSON.stringify(node, undefined, 2)); | ||
} | ||
async function makeLocationUnmapper({ sourceMap, ast }) { | ||
// We rearrange the rolled-up chunk according to its sourcemap to move | ||
// its source lines back to the right place. | ||
// eslint-disable-next-line no-await-in-loop | ||
const consumer = await new SourceMapConsumer(sourceMap); | ||
try { | ||
const unmapped = new WeakSet(); | ||
let lastPos = { ...ast.loc.start }; | ||
return loc => { | ||
if (!loc || unmapped.has(loc)) { | ||
return; | ||
} | ||
// Make sure things start at least at the right place. | ||
loc.end = { ...loc.start }; | ||
for (const pos of ['start', 'end']) { | ||
if (loc[pos]) { | ||
const newPos = consumer.originalPositionFor(loc[pos]); | ||
if (newPos.source !== null) { | ||
lastPos = { | ||
line: newPos.line, | ||
column: newPos.column, | ||
}; | ||
} | ||
loc[pos] = lastPos; | ||
} | ||
} | ||
unmapped.add(loc); | ||
}; | ||
} finally { | ||
consumer.destroy(); | ||
} | ||
} | ||
function transformAst(ast, unmapLoc) { | ||
babelTraverse(ast, { | ||
enter(p) { | ||
const { loc, leadingComments, trailingComments } = p.node; | ||
if (p.node.comments) { | ||
p.node.comments = []; | ||
} | ||
// Rewrite all comments. | ||
(leadingComments || []).forEach(node => rewriteComment(node, unmapLoc)); | ||
if (p.node.type.startsWith('Comment')) { | ||
rewriteComment(p.node, unmapLoc); | ||
} | ||
// If not a comment, and we are unmapping the source maps, | ||
// then do it for this location. | ||
if (unmapLoc) { | ||
unmapLoc(loc); | ||
} | ||
(trailingComments || []).forEach(node => rewriteComment(node, unmapLoc)); | ||
}, | ||
}); | ||
} | ||
async function transformSource(code, { sourceMap, useLocationUnmap }) { | ||
// Parse the rolled-up chunk with Babel. | ||
// We are prepared for different module systems. | ||
const ast = (babelParser.parse || babelParser)(code, { | ||
plugins: ['bigInt'], | ||
}); | ||
let unmapLoc; | ||
if (useLocationUnmap) { | ||
unmapLoc = await makeLocationUnmapper({ | ||
sourceMap, | ||
ast, | ||
}); | ||
} | ||
transformAst(ast, unmapLoc); | ||
// Now generate the sources with the new positions. | ||
return babelGenerate(ast, { retainLines: true }); | ||
} | ||
/** @type {BundleSource} */ | ||
@@ -90,94 +186,25 @@ export default async function bundleSource( | ||
let entrypoint; | ||
for (const chunk of output) { | ||
if (chunk.isAsset) { | ||
throw Error(`unprepared for assets: ${chunk.fileName}`); | ||
} | ||
const { code, fileName, isEntry } = chunk; | ||
if (isEntry) { | ||
entrypoint = fileName; | ||
} | ||
await Promise.all( | ||
output.map(async chunk => { | ||
if (chunk.isAsset) { | ||
throw Error(`unprepared for assets: ${chunk.fileName}`); | ||
} | ||
const { code, fileName, isEntry } = chunk; | ||
if (isEntry) { | ||
entrypoint = fileName; | ||
} | ||
// Parse the rolled-up chunk with Babel. | ||
// We are prepared for different module systems. | ||
const ast = (babelParser.parse || babelParser)(code, { | ||
plugins: ['bigInt'], | ||
}); | ||
const useLocationUnmap = | ||
moduleFormat === 'nestedEvaluate' && !fileName.startsWith('_virtual/'); | ||
let unmapLoc; | ||
if ( | ||
moduleFormat === 'nestedEvaluate' && | ||
!fileName.startsWith('_virtual/') | ||
) { | ||
// We rearrange the rolled-up chunk according to its sourcemap to move | ||
// its source lines back to the right place. | ||
// eslint-disable-next-line no-await-in-loop | ||
const consumer = await new SourceMapConsumer(chunk.map); | ||
const unmapped = new WeakSet(); | ||
let lastPos = { ...ast.loc.start }; | ||
unmapLoc = loc => { | ||
if (!loc || unmapped.has(loc)) { | ||
return; | ||
} | ||
// Make sure things start at least at the right place. | ||
loc.end = { ...loc.start }; | ||
for (const pos of ['start', 'end']) { | ||
if (loc[pos]) { | ||
const newPos = consumer.originalPositionFor(loc[pos]); | ||
if (newPos.source !== null) { | ||
lastPos = { | ||
line: newPos.line, | ||
column: newPos.column, | ||
}; | ||
} | ||
loc[pos] = lastPos; | ||
} | ||
} | ||
unmapped.add(loc); | ||
}; | ||
} | ||
const { code: transformedCode } = await transformSource(code, { | ||
sourceMap: chunk.map, | ||
useLocationUnmap, | ||
}); | ||
sourceBundle[fileName] = transformedCode; | ||
const rewriteComment = node => { | ||
node.type = 'CommentBlock'; | ||
// Within comments... | ||
node.value = node.value | ||
// ...strip extraneous comment whitespace | ||
.replace(/^\s+/gm, ' ') | ||
// ...replace HTML comments with a defanged version to pass SES restrictions. | ||
.replace(HTML_COMMENT_START_RE, '<!X-') | ||
.replace(HTML_COMMENT_END_RE, '-X>') | ||
// ...replace import expressions with a defanged version to pass SES restrictions. | ||
.replace(IMPORT_RE, 'X$1$2') | ||
// ...replace end-of-comment markers | ||
.replace(/\*\//g, '*X/'); | ||
if (unmapLoc) { | ||
unmapLoc(node.loc); | ||
} | ||
// console.log(JSON.stringify(node, undefined, 2)); | ||
}; | ||
// console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`); | ||
}), | ||
); | ||
babelTraverse(ast, { | ||
enter(p) { | ||
const { loc, leadingComments, trailingComments } = p.node; | ||
if (p.node.comments) { | ||
p.node.comments = []; | ||
} | ||
// Rewrite all comments. | ||
(leadingComments || []).forEach(rewriteComment); | ||
if (p.node.type.startsWith('Comment')) { | ||
rewriteComment(p.node); | ||
} | ||
// If not a comment, and we are unmapping the source maps, | ||
// then do it for this location. | ||
if (unmapLoc) { | ||
unmapLoc(loc); | ||
} | ||
(trailingComments || []).forEach(rewriteComment); | ||
}, | ||
}); | ||
// Now generate the sources with the new positions. | ||
sourceBundle[fileName] = babelGenerate(ast, { retainLines: true }).code; | ||
// console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`); | ||
} | ||
if (!entrypoint) { | ||
@@ -184,0 +211,0 @@ throw Error('No entrypoint found in output bundle'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34755
339