diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js index ae8d3b19..22b1bb58 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/transform-module-test.js @@ -162,9 +162,11 @@ describe('transforming JS modules:', () => { it('creates source maps', done => { transformModule(sourceCode, options(), (error, result) => { const {code, map} = result.details.transformed.default; - const column = code.indexOf('code'); + const position = findColumnAndLine(code, 'code'); + expect(position).not.toBeNull(); + const consumer = new SourceMapConsumer(map); - expect(consumer.originalPositionFor({line: 1, column})).toEqual( + expect(consumer.originalPositionFor(position)).toEqual( expect.objectContaining({line: 1, column: sourceCode.indexOf('code')}), ); done(); @@ -273,3 +275,15 @@ function createTestData() { transformedCode: generate(fileAst).code, }; } + +function findColumnAndLine(text, string) { + const lines = text.split('\n'); + for (let i = 0; i < lines.length; i++) { + const column = lines[i].indexOf(string); + if (column !== -1) { + const line = i + 1; + return {line, column}; + } + } + return null; +} diff --git a/packages/metro-bundler/src/ModuleGraph/worker/generate.js b/packages/metro-bundler/src/ModuleGraph/worker/generate.js index a0f2d755..321ad2b4 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/generate.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/generate.js @@ -12,10 +12,10 @@ const babelGenerate = require('babel-generator').default; -function generate(ast: Object, filename: string, sourceCode: string) { +function generate(ast: Object, filename: string, sourceCode: string, compact: boolean) { return babelGenerate(ast, { comments: false, - compact: true, + compact, filename, sourceFileName: filename, sourceMaps: true, diff --git a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js index 7c31b3aa..ca4f7214 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js @@ -73,7 +73,7 @@ function optimize(transformed, file, originalCode, options) { } const inputMap = transformed.map; - const gen = generate(optimized.ast, file, originalCode); + const gen = generate(optimized.ast, file, originalCode, true); const min = minify.withSourceMap( gen.code, diff --git a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js index c0ff1ae8..0719f7e9 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/transform-module.js @@ -178,7 +178,7 @@ function makeResult(ast, filename, sourceCode, isPolyfill = false) { file = JsFileWrapping.wrapModule(ast, dependencyMapName); } - const gen = generate(file, filename, sourceCode); + const gen = generate(file, filename, sourceCode, false); return {code: gen.code, map: gen.map, dependencies, dependencyMapName}; }