Skip to content

Commit

Permalink
Flowify sourcemaps.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed May 10, 2022
1 parent b1edad8 commit b24752f
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions packages/core/integration-tests/test/sourcemaps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// @flow
import assert from 'assert';
import invariant from 'assert';
import path from 'path';
import SourceMap from '@parcel/source-map';
import type {InitialParcelOptions} from '@parcel/types';
import {
bundle as _bundle,
inputFS,
Expand All @@ -11,11 +14,11 @@ import {
mergeParcelOptions,
} from '@parcel/test-utils';
import {loadSourceMapUrl} from '@parcel/utils';
import nullthrows from 'nullthrows';

const bundle = (name, opts = {}) => {
const bundle = (name, opts?: InitialParcelOptions) => {
return _bundle(
name,
// $FlowFixMe
mergeParcelOptions(
{
defaultTargetOptions: {
Expand Down Expand Up @@ -43,7 +46,15 @@ function checkSourceMapping({
generatedStr = str,
sourcePath,
msg = '',
}) {
}: {|
map: SourceMap,
source: string,
generated: string,
str: string,
generatedStr?: string,
sourcePath: string,
msg?: string,
|}) {
assert(
generated.indexOf(generatedStr) !== -1,
"'" + generatedStr + "' not found in generated code",
Expand Down Expand Up @@ -90,13 +101,14 @@ function checkSourceMapping({
mapping = map.indexedMappingToStringMapping(mappings[closestIndex]);
}

assert(mapping, "no mapping for '" + str + "'" + msg);
invariant(mapping, "no mapping for '" + str + "'" + msg);

let generatedDiff = {
line: generatedPosition.line - mapping.generated.line,
column: generatedPosition.column - mapping.generated.column,
};

invariant(mapping.original);
let computedSourcePosition = {
line: mapping.original.line + generatedDiff.line,
column: mapping.original.column + generatedDiff.column,
Expand Down Expand Up @@ -610,7 +622,9 @@ describe('sourcemaps', function () {
'/integration/sourcemap-css/style.css',
);

await bundle(inputFilePath, {minify});
await bundle(inputFilePath, {
defaultTargetOptions: {shouldOptimize: minify},
});
let distDir = path.join(__dirname, '../dist/');
let filename = path.join(distDir, 'style.css');
let raw = await outputFS.readFile(filename, 'utf8');
Expand Down Expand Up @@ -656,7 +670,9 @@ describe('sourcemaps', function () {
'/integration/sourcemap-css-import/style.css',
);

await bundle(inputFilePath, {minify});
await bundle(inputFilePath, {
defaultTargetOptions: {shouldOptimize: minify},
});
let distDir = path.join(__dirname, '../dist/');
let filename = path.join(distDir, 'style.css');
let raw = await outputFS.readFile(filename, 'utf8');
Expand All @@ -673,22 +689,33 @@ describe('sourcemaps', function () {
sourceMap.addVLQMap(map);

let mapData = sourceMap.getMap();
assert.deepEqual(mapData.sources, [
'other-style.css',
'another-style.css',
'style.css',
]);
let sources = minify
? ['style.css', 'other-style.css', 'another-style.css']
: ['other-style.css', 'another-style.css', 'style.css'];
assert.deepEqual(mapData.sources, sources);

let otherStyle = await inputFS.readFile(
path.join(path.dirname(filename), map.sourceRoot, map.sources[0]),
path.join(
path.dirname(filename),
map.sourceRoot,
map.sources[sources.indexOf('other-style.css')],
),
'utf-8',
);
let anotherStyle = await inputFS.readFile(
path.join(path.dirname(filename), map.sourceRoot, map.sources[1]),
path.join(
path.dirname(filename),
map.sourceRoot,
map.sources[sources.indexOf('another-style.css')],
),
'utf-8',
);
let style = await inputFS.readFile(
path.join(path.dirname(filename), map.sourceRoot, map.sources[2]),
path.join(
path.dirname(filename),
map.sourceRoot,
map.sources[sources.indexOf('style.css')],
),
'utf8',
);

Expand Down Expand Up @@ -1123,7 +1150,9 @@ describe('sourcemaps', function () {
__dirname,
'/integration/sourcemap-css-existing/style.css',
);
let b = await bundle(sourceFilename, {minify});
let b = await bundle(sourceFilename, {
defaultTargetOptions: {shouldOptimize: minify},
});

let filename = b.getBundles()[0].filePath;
let raw = await outputFS.readFile(filename, 'utf8');
Expand Down Expand Up @@ -1281,7 +1310,7 @@ describe('sourcemaps', function () {
let sourceMap = new SourceMap('/');
sourceMap.addVLQMap(map);
let sourcePath = 'index.js';
let sourceContent = sourceMap.getSourceContent(sourcePath);
let sourceContent = nullthrows(sourceMap.getSourceContent(sourcePath));

checkSourceMapping({
map: sourceMap,
Expand Down Expand Up @@ -1326,7 +1355,7 @@ describe('sourcemaps', function () {
let sourceMap = new SourceMap('/');
sourceMap.addVLQMap(map);
let sourcePath = 'index.tsx';
let sourceContent = sourceMap.getSourceContent(sourcePath);
let sourceContent = nullthrows(sourceMap.getSourceContent(sourcePath));

checkSourceMapping({
map: sourceMap,
Expand Down

0 comments on commit b24752f

Please sign in to comment.