diff --git a/packages/lwc-compiler/package.json b/packages/lwc-compiler/package.json index 28edf5091c..6c9400953b 100755 --- a/packages/lwc-compiler/package.json +++ b/packages/lwc-compiler/package.json @@ -32,6 +32,7 @@ "@types/babel-core": "~6.25.3", "awesome-typescript-loader": "^5.0.0", "magic-string": "^0.22.4", + "source-map": "^0.7.3", "string-replace-webpack-plugin": "^0.1.3", "webpack": "^4.10.2", "webpack-cli": "^3.0.2" diff --git a/packages/lwc-compiler/src/bundler/__tests__/bundler.spec.ts b/packages/lwc-compiler/src/bundler/__tests__/bundler.spec.ts index 7bf3f36ee3..baad95d853 100755 --- a/packages/lwc-compiler/src/bundler/__tests__/bundler.spec.ts +++ b/packages/lwc-compiler/src/bundler/__tests__/bundler.spec.ts @@ -8,4 +8,54 @@ describe('bundler', () => { ), }); }); + + test('does not return sourcemap by default', async () => { + const result = await bundle({ + baseDir: '', + name: 'cmp', + namespace: 'c', + files: { + 'cmp.js': 'let a = 1' + }, + stylesheetConfig: { + customProperties: { + allowDefinition: false, + resolution: { type: 'native' } + } + }, + outputConfig: { + env: { NODE_ENV: 'development' }, + minify: false, + compat: false, + sourcemap: false + } + }); + + expect(result.map).toBeNull(); + }); + + test('returns sourcemap when requested', async () => { + const result = await bundle({ + baseDir: '', + name: 'cmp', + namespace: 'c', + files: { + 'cmp.js': 'const a = 1;' + }, + stylesheetConfig: { + customProperties: { + allowDefinition: false, + resolution: { type: 'native' } + } + }, + outputConfig: { + env: { NODE_ENV: 'development' }, + minify: false, + compat: false, + sourcemap: true + } + }); + + expect(result.map).not.toBeNull(); + }); }); diff --git a/packages/lwc-compiler/src/bundler/bundler.ts b/packages/lwc-compiler/src/bundler/bundler.ts index a802a915eb..4902254df1 100755 --- a/packages/lwc-compiler/src/bundler/bundler.ts +++ b/packages/lwc-compiler/src/bundler/bundler.ts @@ -15,11 +15,12 @@ import { import { collectImportLocations } from "./import-location-collector"; import { Diagnostic, DiagnosticLevel } from "../diagnostics/diagnostic"; +import { SourceMap } from "../compiler/compiler"; export interface BundleReport { code: string; diagnostics: Diagnostic[]; - map: null; + map: SourceMap | null; metadata: BundleMetadata; } @@ -84,14 +85,15 @@ export async function bundle( ); if (outputConfig.compat) { - plugins.push(rollupCompat()); + plugins.push(rollupCompat(outputConfig)); } if (outputConfig.minify) { - plugins.push(rollupMinify()); + plugins.push(rollupMinify(outputConfig)); } let code; + let map = null; try { const rollupBundler = await rollup({ input: name, @@ -103,10 +105,11 @@ export async function bundle( amd: { id: namespace + "/" + name }, interop: false, strict: false, + sourcemap: outputConfig.sourcemap, format }); code = result.code; - + map = result.map; } catch (e) { // populate diagnostics const { message, filename } = e; @@ -125,7 +128,7 @@ export async function bundle( return { diagnostics, code, - map: null, + map, metadata: metadataCollector.getMetadata() }; } diff --git a/packages/lwc-compiler/src/compiler/__tests__/options.spec.ts b/packages/lwc-compiler/src/compiler/__tests__/options.spec.ts index 94d2ecc14b..2db067f6af 100644 --- a/packages/lwc-compiler/src/compiler/__tests__/options.spec.ts +++ b/packages/lwc-compiler/src/compiler/__tests__/options.spec.ts @@ -78,6 +78,22 @@ describe('compiler options', () => { }); }); + it('should validate outputConfig.sourcemap', async () => { + await expect( + compile({ + name: 'foo', + namespace: 'x', + files: { x: 'foo' }, + outputConfig: { + sourcemap: 'true', + }, + }), + ).rejects.toMatchObject({ + message: + "Expected a boolean value for outputConfig.sourcemap, received \"true\".", + }); + }); + it('should validate stylesheetConfig.customProperties.allowDefinition', async () => { await expect( compile({ diff --git a/packages/lwc-compiler/src/compiler/__tests__/result.spec.ts b/packages/lwc-compiler/src/compiler/__tests__/result.spec.ts index 3f46a49a01..7b522e3807 100755 --- a/packages/lwc-compiler/src/compiler/__tests__/result.spec.ts +++ b/packages/lwc-compiler/src/compiler/__tests__/result.spec.ts @@ -1,3 +1,4 @@ +import { SourceMapConsumer } from "source-map"; import { compile } from "../compiler"; import { pretify, readFixture } from "../../__tests__/utils"; import { DiagnosticLevel } from "../../diagnostics/diagnostic"; @@ -200,6 +201,71 @@ describe("compiler result", () => { expect(diagnostics[1].level).toBe(DiagnosticLevel.Fatal); expect(diagnostics[1].message).toContain('foo.html: