Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lavapack): comment out unused test & harness #1124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
151 changes: 74 additions & 77 deletions packages/lavapack/test/sourcemaps.spec.js
Expand Up @@ -5,85 +5,82 @@ const test = require('ava')
// const UglifyJS = require('uglify-js')
// const { SourceMapConsumer } = require('source-map')

test.skip('sourcemaps - adjust maps for wrapper', async (t) => {
const { wrapIntoModuleInitializer } = require('../src/sourcemaps')
const fooSource = `
var two = 1 + 1
throw new Error('Boom')
var three = two + 1
`
test.todo('sourcemaps - adjust maps for wrapper')
// test('sourcemaps - adjust maps for wrapper', async (t) => {
// const { wrapIntoModuleInitializer } = require('../src/sourcemaps')
// const fooSource = `
// var two = 1 + 1
// throw new Error('Boom')
// var three = two + 1
// `
// // eslint-disable-next-line no-undef
// const result = UglifyJS.minify(
// { './foo.js': fooSource },
// {
// // skip logical compression like removing unused stuff
// compress: false,
// sourceMap: {
// filename: './foo.js',
// // inline sourcemaps with sources included
// url: 'inline',
// includeSources: true,
// },
// }
// )
// if (result.error) {
// t.ifError(result.error)
// }
// // ensure minification worked
// t.true(indicesOf('\n', fooSource).length > 1)
// t.is(indicesOf('\n', result.code).length, 1)
// // wrap into bundle with external sourcemaps
// const wrappedSourceMeta = wrapIntoModuleInitializer(result.code)
// await validateSourcemaps(t, wrappedSourceMeta)
// })

// eslint-disable-next-line no-undef
const result = UglifyJS.minify(
{ './foo.js': fooSource },
{
// skip logical compression like removing unused stuff
compress: false,
sourceMap: {
filename: './foo.js',
// inline sourcemaps with sources included
url: 'inline',
includeSources: true,
},
}
)
// function indicesOf(substring, string) {
// const result = []
// let index = -1
// while ((index = string.indexOf(substring, index + 1)) >= 0) {
// result.push(index)
// }
// return result
// }

if (result.error) {
t.ifError(result.error)
}
// // this is not perfecct - just a heuristic
// async function validateSourcemaps(t, sourceMeta) {
// const targetSlug = 'new Error'
// // eslint-disable-next-line no-undef
// const consumer = await new SourceMapConsumer(sourceMeta.maps)
// t.true(consumer.hasContentsOfAllSources(), 'has the contents of all sources')

// ensure minification worked
t.true(indicesOf('\n', fooSource).length > 1)
t.is(indicesOf('\n', result.code).length, 1)
// const sourceLines = sourceMeta.code.split('\n')

// wrap into bundle with external sourcemaps
const wrappedSourceMeta = wrapIntoModuleInitializer(result.code)
await validateSourcemaps(t, wrappedSourceMeta)
})
// sourceLines
// .map((line) => indicesOf(targetSlug, line))
// .forEach((errorIndices, lineIndex) => {
// // if (errorIndex === null) return console.log('line does not contain "new Error"')
// errorIndices.forEach((errorIndex) => {
// const position = { line: lineIndex + 1, column: errorIndex }
// const result = consumer.originalPositionFor(position)
// if (!result.source) {
// t.fail(`missing source for position: ${JSON.stringify(position)}`)
// t.log('=======')
// t.log(contentForPosition(sourceLines, position))
// t.log('=======')
// return
// }
// const sourceContent = consumer.sourceContentFor(result.source)
// const sourceLines = sourceContent.split('\n')
// const line = sourceLines[result.line - 1]
// if (!line.includes(targetSlug)) {
// t.fail(`could not find target "${targetSlug}" in source`)
// }
// })
// })
// t.true(true, 'sourcemaps look ok')
// }

function indicesOf(substring, string) {
const result = []
let index = -1
while ((index = string.indexOf(substring, index + 1)) >= 0) {
result.push(index)
}
return result
}

// this is not perfecct - just a heuristic
async function validateSourcemaps(t, sourceMeta) {
const targetSlug = 'new Error'
// eslint-disable-next-line no-undef
const consumer = await new SourceMapConsumer(sourceMeta.maps)
t.true(consumer.hasContentsOfAllSources(), 'has the contents of all sources')

const sourceLines = sourceMeta.code.split('\n')

sourceLines
.map((line) => indicesOf(targetSlug, line))
.forEach((errorIndices, lineIndex) => {
// if (errorIndex === null) return console.log('line does not contain "new Error"')
errorIndices.forEach((errorIndex) => {
const position = { line: lineIndex + 1, column: errorIndex }
const result = consumer.originalPositionFor(position)
if (!result.source) {
t.fail(`missing source for position: ${JSON.stringify(position)}`)
t.log('=======')
t.log(contentForPosition(sourceLines, position))
t.log('=======')
return
}
const sourceContent = consumer.sourceContentFor(result.source)
const sourceLines = sourceContent.split('\n')
const line = sourceLines[result.line - 1]
if (!line.includes(targetSlug)) {
t.fail(`could not find target "${targetSlug}" in source`)
}
})
})
t.true(true, 'sourcemaps look ok')
}

function contentForPosition(sourceLines, position) {
return sourceLines[position.line - 1].slice(position.column)
}
// function contentForPosition(sourceLines, position) {
// return sourceLines[position.line - 1].slice(position.column)
// }