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

Babel ast location, diagnostic, and source location remapping #6238

Merged
merged 40 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fc88e8b
wip ast location remapping
DeMoorJasper Feb 17, 2021
424888a
slight cleanup in generate
DeMoorJasper Feb 17, 2021
6c1bb67
remove some debug and todo stuff
DeMoorJasper Feb 17, 2021
309cfaa
add a todo
DeMoorJasper Feb 17, 2021
ef8dca4
add a small util to babylon-walk for walking over all nodes with a si…
DeMoorJasper Feb 18, 2021
72cc8a6
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Feb 18, 2021
9096141
tweak
DeMoorJasper Feb 18, 2021
dbdd78b
null mappings
DeMoorJasper Feb 18, 2021
1efc8d6
undo formatting...
DeMoorJasper Feb 18, 2021
d624437
wathever I give up, it works it's wonderful...
DeMoorJasper Feb 18, 2021
823da35
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Feb 20, 2021
5eebda4
fix sourcemap tests
DeMoorJasper Feb 20, 2021
8d47c46
remove umd example
DeMoorJasper Feb 20, 2021
70c00d6
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Feb 22, 2021
95acc95
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Feb 28, 2021
bf6591b
runtime sourcemaps?
DeMoorJasper Feb 28, 2021
64b434a
some sources content logic
DeMoorJasper Feb 28, 2021
e509474
finish up sources content handling
DeMoorJasper Mar 3, 2021
df3ed6d
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Mar 3, 2021
7266f84
update source-map dep
DeMoorJasper Mar 5, 2021
f6e0e03
use the new sourcemap util for source contents fetching
DeMoorJasper Mar 5, 2021
f1c6d66
undo test skipping
DeMoorJasper Mar 5, 2021
1cb16cd
fix sourcemap tests
DeMoorJasper Mar 5, 2021
d6da582
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Mar 20, 2021
754ad53
update sourcemap dep
DeMoorJasper Mar 20, 2021
4a83253
tweaks hopefully this works?
DeMoorJasper Mar 20, 2021
d96a906
tweak
DeMoorJasper Mar 20, 2021
6c8d560
fix linting issues
DeMoorJasper Mar 20, 2021
842d2cf
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper Mar 21, 2021
269d2c9
Merge branch 'v2' into babel-ast-remapping
DeMoorJasper May 6, 2021
f52e2bf
Merge branch 'v2' into babel-ast-remapping
May 6, 2021
c39fa92
Merge remote-tracking branch 'origin/babel-ast-remapping' into wbinns…
May 12, 2021
51aa11e
Port sourcesContent to new scope hoisting and initialize in Transform…
May 6, 2021
6aeb81a
Add integration test for original sourcesContent through transformations
May 6, 2021
a62fb08
Merge branch 'v2' into wbinnssmith/swc-babel-remap-asts
DeMoorJasper May 13, 2021
a2819a3
Merge branch 'v2' of github.com:parcel-bundler/parcel into wbinnssmit…
devongovett May 15, 2021
c674b9c
Merge branch 'v2' of github.com:parcel-bundler/parcel into wbinnssmit…
devongovett May 15, 2021
337f67d
Update test for new sourcemaps API
devongovett May 15, 2021
d2f4f86
Simplify source content handling (#6286)
devongovett May 16, 2021
789e4e1
Merge branch 'v2' into wbinnssmith/swc-babel-remap-asts
DeMoorJasper May 16, 2021
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
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PROJECT_ROOT>/packages/core/integration-tests/dist/**
<PROJECT_ROOT>/packages/core/integration-tests/test/input/**
<PROJECT_ROOT>/packages/core/integration-tests/test/integration/babel-strip-flow-types/**
<PROJECT_ROOT>/packages/core/integration-tests/test/integration/diagnostic-sourcemap/**

[untyped]
.*/node_modules/graphql/error/GraphQLError.js.flow
Expand Down
14 changes: 11 additions & 3 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ export default class Transformation {

let asset = await this.loadAsset();

// Load existing sourcemaps
if (SOURCEMAP_EXTENSIONS.has(asset.value.type)) {
if (!asset.mapBuffer && SOURCEMAP_EXTENSIONS.has(asset.value.type)) {
// Load existing sourcemaps, this automatically runs the source contents extraction
let existing;
try {
await asset.loadExistingSourcemap();
existing = await asset.loadExistingSourcemap();
} catch (err) {
logger.verbose([
{
Expand All @@ -158,6 +159,13 @@ export default class Transformation {
},
]);
}

if (existing == null) {
// If no existing sourcemap was found, initialize asset.sourceContent
// with the original contents. This will be used when the transformer
// calls setMap to ensure the source content is in the sourcemap.
asset.sourceContent = await asset.getCode();
}
}

for (let {moduleSpecifier, resolveFrom} of this.request.invalidDevDeps) {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/core/src/UncommittedAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class UncommittedAsset {
options: ParcelOptions;
content: ?(Blob | Promise<Buffer>);
mapBuffer: ?Buffer;
sourceContent: ?string;
map: ?SourceMap;
ast: ?AST;
isASTDirty: boolean;
Expand Down Expand Up @@ -272,8 +273,15 @@ export default class UncommittedAsset {
}

setMap(map: ?SourceMap): void {
// If we have sourceContent available, it means this asset is source code without
// a previous source map. Ensure that the map set by the transformer has the original
// source content available.
if (map && this.sourceContent != null) {
map.setSourceContent(this.value.filePath, this.sourceContent);
}

this.map = map;
this.mapBuffer = map?.toBuffer();
this.mapBuffer = this.map?.toBuffer();
}

getAST(): Promise<?AST> {
Expand Down
9 changes: 8 additions & 1 deletion packages/core/core/src/applyRuntimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type ParcelConfig from './ParcelConfig';
import type PluginOptions from './public/PluginOptions';
import type {RunAPI} from './RequestTracker';

import path from 'path';
import assert from 'assert';
import invariant from 'assert';
import nullthrows from 'nullthrows';
Expand All @@ -22,6 +23,7 @@ import BundleGraph from './public/BundleGraph';
import InternalBundleGraph from './BundleGraph';
import {NamedBundle} from './public/Bundle';
import {PluginLogger} from '@parcel/logger';
import {md5FromString} from '@parcel/utils';
import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic';
import {dependencyToInternalDependency} from './public/Dependency';
import createAssetGraphRequest from './requests/AssetGraphRequest';
Expand Down Expand Up @@ -76,9 +78,14 @@ export default async function applyRuntimes({
if (applied) {
let runtimeAssets = Array.isArray(applied) ? applied : [applied];
for (let {code, dependency, filePath, isEntry} of runtimeAssets) {
let sourceName = path.join(
path.dirname(filePath),
`runtime-${md5FromString(code)}.${bundle.type}`,
);

let assetGroup = {
code,
filePath,
filePath: sourceName,
env: bundle.env,
// Runtime assets should be considered source, as they should be
// e.g. compiled to run in the target environment
Expand Down
49 changes: 48 additions & 1 deletion packages/core/integration-tests/test/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
assertBundles,
distDir,
removeDistDirectory,
inputFS,
outputFS,
} from '@parcel/test-utils';

Expand Down Expand Up @@ -78,7 +79,6 @@ describe('css', () => {
'css-loader.js',
'index.js',
'js-loader.js',
'JSRuntime.js',
],
},
{name: /local\.[0-9a-f]{8}\.js/, assets: ['local.js']},
Expand Down Expand Up @@ -332,4 +332,51 @@ describe('css', () => {
background-image: url('data:image/webp;base64,UklGR`),
);
});

it('should remap locations in diagnostics using the input source map', async () => {
let fixture = path.join(
__dirname,
'integration/diagnostic-sourcemap/index.scss',
);
let code = await inputFS.readFileSync(fixture, 'utf8');
// $FlowFixMe
await assert.rejects(
() =>
bundle(fixture, {
defaultTargetOptions: {
shouldOptimize: true,
},
}),
{
name: 'BuildError',
diagnostics: [
{
message: "Failed to resolve 'x.png' from './index.scss'",
origin: '@parcel/core',
filePath: fixture,
codeFrame: {
code,
codeHighlights: [
{
start: {
line: 5,
column: 3,
},
end: {
line: 5,
column: 3,
},
},
],
},
},
{
message: "Cannot load file './x.png' in './'.",
origin: '@parcel/resolver-default',
hints: [],
},
],
},
);
});
});
2 changes: 1 addition & 1 deletion packages/core/integration-tests/test/elm.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('elm', function() {
assertBundles(b, [
{
type: 'js',
assets: ['HMRRuntime.js', 'Main.elm', 'index.js'],
assets: ['Main.elm', 'index.js'],
},
]);

Expand Down
15 changes: 3 additions & 12 deletions packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ describe('html', function() {

assertBundles(b, [
{
name: 'index.html',
type: 'js',
assets: ['index.html'],
},
{
type: 'js',
assets: ['HMRRuntime.js', 'index.html'],
name: 'index.html',
assets: ['index.html'],
},
]);
});
Expand Down Expand Up @@ -1197,8 +1197,6 @@ describe('html', function() {
'index.js',
'index.js',
'js-loader.js',
'JSRuntime.js',
'JSRuntime.js',
'relative-path.js',
],
},
Expand Down Expand Up @@ -1282,9 +1280,6 @@ describe('html', function() {
'get-worker-url.js',
'index.js',
'js-loader.js',
'JSRuntime.js',
'JSRuntime.js',
'JSRuntime.js',
'relative-path.js',
],
},
Expand Down Expand Up @@ -1348,9 +1343,6 @@ describe('html', function() {
'get-worker-url.js',
'index.js',
'js-loader.js',
'JSRuntime.js',
'JSRuntime.js',
'JSRuntime.js',
'relative-path.js',
],
},
Expand Down Expand Up @@ -1598,7 +1590,6 @@ describe('html', function() {
'esmodule-helpers.js',
'get-worker-url.js',
'index.js',
'JSRuntime.js',
],
},
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow

type Test = {|
foo: string
|};

let test: Test = {
foo: 'hi'
};

import foo from 'foo';

console.log(test);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$foo: red;

.foo {
color: $foo;
background: url(x.png);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"flow-bin": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-react", "@babel/preset-flow"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @flow

import * as React from "react";
import * as ReactDOM from "react-dom";

type Props = {|
bar: string,
|};

function App(props: Props) {
return <div>{props.bar}</div>;
}

ReactDOM.render(<App bar="bar" />, document.getElementById("root"));