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

feat(compiler): add sourcemap to options and result #696

Merged
merged 12 commits into from
Oct 4, 2018

Conversation

jodarove
Copy link
Contributor

@jodarove jodarove commented Oct 1, 2018

of the compile method.

  • Add options to disable (enabled by default) sourcemaps in compat/minify lwc rollup plugins.
  • Removes the template/style from the output sourcemap.
  • javascript transformer returns the sourcemap.
  • javascript transformer sourcemap defaulted to false.
  • make rollup bundler of the compiler to return the sourcemaps.

Details

This PR adds a sourcemap property to the OutputConfig of the compiler options. When sourcemap=true the compile method will return a SourceMap in the map property of the result, null otherwise.

Does this PR introduce a breaking change?

  • Yes
  • No

to the compile method.

- Add options for disable sourcemaps in compat/minify lwc rollup plugins.
- Removes the template/style from the output sourcemap.
- javascript transformer returns the sourcemap.
- javascript transformer sourcemap defaulted to false.
- make rollup bundler of the compiler to return the sourcemaps.
@jodarove jodarove requested review from pmdartus, apapko and diervo October 1, 2018 20:35
@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 4e3ac4e | Target commit: a82d417

lwc-engine-benchmark-ie11

table-append-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table/append/1k duration 816.70 (±0.00 ms) 801.10 (±0.00 ms) -15.6ms (1.9%) 👌
table-clear-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table/clear/1k duration 79.30 (±0.00 ms) 75.20 (±0.00 ms) -4.1ms (5.2%) 👌
table-create-10k metric base(4e3ac4e) target(a82d417) trend
benchmark-table/create/10k duration 7364.80 (±0.00 ms) 7528.20 (±0.00 ms) +163.4ms (2.2%) 👌
table-create-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table/create/1k duration 704.40 (±0.00 ms) 689.30 (±0.00 ms) -15.1ms (2.1%) 👌
table-update-10th-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table/update-10th/1k duration 113.00 (±0.00 ms) 109.40 (±0.00 ms) -3.6ms (3.2%) 👌
tablecmp-append-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table-component/append/1k duration 1026.60 (±0.00 ms) 1039.00 (±0.00 ms) +12.4ms (1.2%) 👌
tablecmp-clear-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table-component/clear/1k duration 176.20 (±0.00 ms) 193.40 (±0.00 ms) +17.2ms (9.8%) 👌
tablecmp-create-10k metric base(4e3ac4e) target(a82d417) trend
benchmark-table-component/create/10k duration 11415.90 (±0.00 ms) 10549.30 (±0.00 ms) -866.6ms (7.6%) 👌
tablecmp-create-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table-component/create/1k duration 952.40 (±0.00 ms) 941.50 (±0.00 ms) -10.9ms (1.1%) 👌
tablecmp-update-10th-1k metric base(4e3ac4e) target(a82d417) trend
benchmark-table-component/update-10th/1k duration 168.10 (±0.00 ms) 99.20 (±0.00 ms) -68.9ms (41.0%) 👌

@@ -76,6 +78,7 @@ export interface NormalizedStylesheetConfig extends StylesheetConfig {
export interface NormalizedOutputConfig extends OutputConfig {
compat: boolean;
minify: boolean;
sourcemap: boolean | 'inline';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only support 'inline' why do we have that option in the first place?

Copy link
Contributor Author

@jodarove jodarove Oct 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually inline does not make much sense here in the compiler (unless we actually want to add it to the generated .code)

The compiler only return the sourcemap for the specific transformation if this option is true (which by default is false) or inline

how those are bundled together is not responsibility of the compiler.

removing inline option.

@@ -72,6 +73,8 @@ export async function transformFile(

let compatResult;
if (options.outputConfig.compat) {
// @todo: Evaluate for removal.
// **Note: this is dead code since it was only used in the rollup-plugin-lwc, but it was refactored to do this as part of the rollup-plugin-compat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can simplify this dance.

@@ -56,14 +57,16 @@ export async function bundle(

// TODO: remove format option once tests are converted to 'amd' format
const format = (outputConfig as any).format || DEFAULT_FORMAT;
const sourcemap = outputConfig.sourcemap;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use destructuring

const { sourmap } = outputConfig;

packages/lwc-compiler/src/bundler/bundler.ts Show resolved Hide resolved
@@ -0,0 +1,23 @@
import lwcCompatFactory from '../compat';

const codeFixture = "const a = 1;\nconsole.log(a);";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use template string instead of multiline +

@@ -0,0 +1,20 @@
import lwcMinifierFactory from '../minify';

const codeFixture = "/*some comment*/var a = 1;\nconsole.log(a);";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use template string

/**
* Rollup plugin applying minification to the generated bundle.
*/
export default function() {
export default function(options?: LwcMinifierOptions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's pass the NormalizedOutputConfig instead of a custom config.

@@ -13,7 +13,9 @@ export default function(
options: NormalizedCompilerOptions,
metadataCollector?: MetadataCollector
): FileTransformerResult {
const config = Object.assign({}, BABEL_CONFIG_BASE, {

const overrides = { sourceMaps: options.outputConfig.sourcemap };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the override in the object literal instead of a new object.

@@ -1,4 +1,5 @@
import { bundle } from '../bundler';
import { normalizeOptions } from '../../compiler/options';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't invoke the normalizeOptions here, if you want to normlize the options invoke compile.

Copy link
Collaborator

@apapko apapko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure SourceMap type support is added to lwc-compiler as well: https://git.soma.salesforce.com/lwc/lwc-platform/blob/master/lwc/src/main/java/org/lwc/OutputConfig.java

@@ -15,11 +15,12 @@ import {

import { collectImportLocations } from "./import-location-collector";
import { Diagnostic, DiagnosticLevel } from "../diagnostics/diagnostic";
import {SourceMap} from "../compiler/compiler";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add spaces between { SourceMap }

}),
).rejects.toMatchObject({
message:
"Expected a boolean or string 'inline' for outputConfig.sourcemap, received \"true\".",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message will need to be changed once you remove 'inline'

from dval, pm and alex.
this is dead code since it was only used in the rollup-plugin-lwc, but it was refactored to do this as part of the rollup-plugin-compat
option when instantiating the rollup-plugin-lwc-compiler
@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 039a2af | Target commit: f7e87d5

const { DEFAULT_OPTIONS, DEFAULT_MODE } = require("./constants");

module.exports = function rollupLwcCompiler(pluginOptions = {}) {
const { include, exclude } = pluginOptions;
const filter = pluginUtils.createFilter(include, exclude);
const mergedPluginOptions = Object.assign({}, DEFAULT_OPTIONS, pluginOptions);
const { resolveFromPackages, resolveFromSource } = mergedPluginOptions;
const { sourcemap = false } = pluginOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sourcemap default value should be added to the DEFAULT_OPTIONS instead of here.

}

return result;
return await transformer(src, id, options, metadataCollector);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary async and await.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return await is equivalent to return. So the await is not needed.

If you don't have an await in the function body you don't need the async either.


// @ts-ignore
await SourceMapConsumer.with(result!.map as RawSourceMap, null, sourceMapConsumer => {
let gp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reusing the gp create new variable: commentPosition, varPosition, ...
This would also remove the need to have comments in your code explaining what you are looking at.

// @ts-ignore
await SourceMapConsumer.with(result!.map as RawSourceMap, null, sourceMapConsumer => {

let gp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reusing the gp create new variable: commentPosition, varPosition, ...
This would also remove the need to have comments in your code explaining what you are looking at.

let gp;

// @ts-ignore
gp = sourceMapConsumer.allGeneratedPositionsFor({ line: 2, source: "unknown" });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the source is unkown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cause no sourceFileName option is passed to the babel transform method (https://babeljs.io/docs/en/options#sourcefilename) unknown is the default.

Since this is used in a rollup plugin, it does not look into source property, only to the mappings one.

metadata: BundleMetadata;
outputConfig: NormalizedOutputConfig;
}

export interface SourceMap {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it as any. Each tool as a different slightly different format for the sourcemap, and since we are not in the business of manipulating the source map in the compiler directly, I feel more confident to put it as any.

type SourceMap = any;

Thoughts @jodarove ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally agree

packages/lwc-compiler/src/bundler/bundler.ts Show resolved Hide resolved
packages/lwc-compiler/src/bundler/bundler.ts Show resolved Hide resolved
} catch (e) {
// populate diagnostics
const { message, filename } = e;
map = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can initialize map to null on line 96 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, fixing it

let gp;

// m in main from utils;
gp = sourceMapConsumer.generatedPositionFor({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move this initialization to line 232

}
});
// @ts-ignore
await SourceMapConsumer.with(result!.map as RawSourceMap, null, sourceMapConsumer => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add expect.assertions count at the top of the test to ensure all the validations take place after promise resolves.

expect(result.map).not.toBeNull();

// @ts-ignore
await SourceMapConsumer.with(result!.map as RawSourceMap, null, sourceMapConsumer => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls add expect.assertions into this test as well

@jodarove jodarove force-pushed the jodarove/compiler-sourcemaps branch from 7ab06fa to c077554 Compare October 3, 2018 17:41
@jodarove jodarove force-pushed the jodarove/compiler-sourcemaps branch from c077554 to b130763 Compare October 3, 2018 18:57
to the asyn method.
@jodarove jodarove force-pushed the jodarove/compiler-sourcemaps branch from 1a7f3f3 to 3bf8fb9 Compare October 3, 2018 21:45
Copy link
Collaborator

@apapko apapko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change


export interface BundleReport {
code: string;
diagnostics: Diagnostic[];
map: SourceMap | null;
map: any | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need null if the type is any

@@ -15,20 +15,11 @@ export interface CompilerOutput {

export interface BundleResult {
code: string;
map: SourceMap | null;
map: any | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -22,7 +21,7 @@ export interface FileTransformerResult {
metadata?:
| TemplateMetadata
| lwcClassTransformPlugin.Metadata;
map: SourceMap | null;
map: any | null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: b787190 | Target commit: 90d26ac

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 18f1f17 | Target commit: 90d26ac

lwc-engine-benchmark

table-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/append/1k duration 151.80 (±7.05 ms) 157.80 (±6.25 ms) +6.0ms (4.0%) 👎
table-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/clear/1k duration 12.30 (±0.60 ms) 12.45 (±0.65 ms) +0.1ms (1.2%) 👌
table-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table/create/10k duration 892.05 (±8.20 ms) 873.25 (±5.75 ms) -18.8ms (2.1%) 👍
table-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/create/1k duration 110.70 (±2.25 ms) 110.50 (±2.00 ms) -0.2ms (0.2%) 👌
table-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/update-10th/1k duration 85.20 (±2.90 ms) 98.35 (±1.85 ms) +13.2ms (15.4%) 👎
tablecmp-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/append/1k duration 231.00 (±5.40 ms) 231.10 (±5.65 ms) +0.1ms (0.0%) 👌
tablecmp-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/clear/1k duration 19.65 (±1.90 ms) 20.60 (±2.05 ms) +1.0ms (4.8%) 👎
tablecmp-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/create/10k duration 1600.55 (±12.05 ms) 1571.70 (±11.85 ms) -28.9ms (1.8%) 👍
tablecmp-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/create/1k duration 182.40 (±5.90 ms) 182.25 (±6.30 ms) -0.2ms (0.1%) 👌
tablecmp-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/update-10th/1k duration 81.05 (±5.25 ms) 81.50 (±4.00 ms) +0.5ms (0.6%) 👌
wc-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/append/1k duration 261.10 (±17.15 ms) 273.70 (±16.35 ms) +12.6ms (4.8%) 👌
wc-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/clear/1k duration 29.80 (±2.50 ms) 29.65 (±2.75 ms) -0.1ms (0.5%) 👌
wc-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/create/10k duration 2091.65 (±9.30 ms) 2065.10 (±11.05 ms) -26.6ms (1.3%) 👍
wc-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/create/1k duration 222.55 (±5.05 ms) 231.80 (±5.30 ms) +9.3ms (4.2%) 👎
wc-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/update-10th/1k duration 82.85 (±4.55 ms) 84.60 (±6.40 ms) +1.8ms (2.1%) 👌

@salesforce-best-lwc-internal
Copy link

Benchmark results

Base commit: 18f1f17 | Target commit: 90d26ac

lwc-engine-benchmark

table-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/append/1k duration 151.80 (±7.05 ms) 158.10 (±5.90 ms) +6.3ms (4.2%) 👎
table-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/clear/1k duration 12.30 (±0.60 ms) 12.30 (±0.65 ms) 0.0ms (0.0%) 👌
table-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table/create/10k duration 892.05 (±8.20 ms) 877.00 (±5.60 ms) -15.0ms (1.7%) 👍
table-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/create/1k duration 110.70 (±2.25 ms) 110.15 (±1.90 ms) -0.5ms (0.5%) 👌
table-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table/update-10th/1k duration 85.20 (±2.90 ms) 84.30 (±1.45 ms) -0.9ms (1.1%) 👌
tablecmp-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/append/1k duration 231.00 (±5.40 ms) 228.95 (±10.00 ms) -2.1ms (0.9%) 👌
tablecmp-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/clear/1k duration 19.65 (±1.90 ms) 19.70 (±2.00 ms) +0.1ms (0.3%) 👌
tablecmp-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/create/10k duration 1600.55 (±12.05 ms) 1573.90 (±8.60 ms) -26.6ms (1.7%) 👍
tablecmp-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/create/1k duration 182.40 (±5.90 ms) 180.85 (±6.70 ms) -1.5ms (0.8%) 👌
tablecmp-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-component/update-10th/1k duration 81.05 (±5.25 ms) 82.30 (±5.45 ms) +1.3ms (1.5%) 👌
wc-append-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/append/1k duration 261.10 (±17.15 ms) 263.70 (±15.10 ms) +2.6ms (1.0%) 👌
wc-clear-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/clear/1k duration 29.80 (±2.50 ms) 30.00 (±2.20 ms) +0.2ms (0.7%) 👌
wc-create-10k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/create/10k duration 2091.65 (±9.30 ms) 2079.25 (±10.55 ms) -12.4ms (0.6%) 👍
wc-create-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/create/1k duration 222.55 (±5.05 ms) 222.15 (±6.05 ms) -0.4ms (0.2%) 👌
wc-update-10th-1k metric base(18f1f17) target(90d26ac) trend
benchmark-table-wc/update-10th/1k duration 82.85 (±4.55 ms) 84.05 (±6.35 ms) +1.2ms (1.4%) 👌

@jodarove jodarove merged commit 2a3ce0f into master Oct 4, 2018
@jodarove jodarove deleted the jodarove/compiler-sourcemaps branch October 4, 2018 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants