From 8e80016d8b61c5d68759bfd1caa9fb827e46643c Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Wed, 15 Apr 2020 01:16:52 -0700 Subject: [PATCH] CSS SourceMaps (#4440) * Convert $FlowFixMe for untyped imports to flowlint off * Implement CSS sourcemaps * update source-map * update sourcemap tests * all remaining typescript tests * Allow whitespace characters while checking mappings * fix tests Co-authored-by: Jasper De Moor --- flow-libs/postcss.js.flow | 24 +- packages/core/cache/src/Cache.js | 2 +- packages/core/core/src/utils.js | 2 +- packages/core/integration-tests/test/css.js | 35 +- packages/core/integration-tests/test/html.js | 18 +- .../sourcemap-css-import/package.json | 6 + .../sourcemap-css-import/yarn.lock | 0 .../integration/sourcemap-css/package.json | 6 + .../test/integration/sourcemap-css/yarn.lock | 0 .../integration/sourcemap-less/package.json | 6 + .../test/integration/sourcemap-less/yarn.lock | 0 .../sourcemap-sass-imported/package.json | 6 + .../sourcemap-sass-imported/yarn.lock | 0 .../integration/sourcemap-sass/package.json | 6 + .../test/integration/sourcemap-sass/yarn.lock | 0 .../sourcemap-typescript-nested/package.json | 6 + .../sourcemap-typescript-nested/yarn.lock | 0 .../sourcemap-typescript/package.json | 6 + .../sourcemap-typescript/yarn.lock | 0 .../core/integration-tests/test/javascript.js | 10 +- packages/core/integration-tests/test/less.js | 2 +- packages/core/integration-tests/test/sass.js | 2 +- .../core/integration-tests/test/sourcemaps.js | 770 +++++++++--------- packages/core/register/src/register.js | 6 +- packages/core/test-utils/src/utils.js | 17 + packages/core/utils/src/index.js | 2 +- packages/core/utils/src/schema.js | 2 +- packages/optimizers/cssnano/package.json | 1 + .../cssnano/src/CSSNanoOptimizer.js | 44 +- .../htmlnano/src/HTMLNanoOptimizer.js | 2 +- packages/packagers/css/package.json | 1 + packages/packagers/css/src/CSSPackager.js | 70 +- packages/packagers/js/src/JSPackager.js | 3 +- packages/transformers/css/package.json | 1 + .../transformers/css/src/CSSTransformer.js | 25 +- 35 files changed, 634 insertions(+), 447 deletions(-) create mode 100644 packages/core/integration-tests/test/integration/sourcemap-css-import/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-css-import/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-css/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-css/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-less/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-less/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-sass-imported/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-sass-imported/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-sass/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-sass/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-typescript-nested/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-typescript-nested/yarn.lock create mode 100644 packages/core/integration-tests/test/integration/sourcemap-typescript/package.json create mode 100644 packages/core/integration-tests/test/integration/sourcemap-typescript/yarn.lock diff --git a/flow-libs/postcss.js.flow b/flow-libs/postcss.js.flow index 705bd5df272..065edf416b5 100644 --- a/flow-libs/postcss.js.flow +++ b/flow-libs/postcss.js.flow @@ -4,7 +4,8 @@ // Derived from the PostCSS docs available at // http://api.postcss.org/postcss.html. -import type {SourceMapGenerator} from 'source-map'; +// eslint-disable-next-line import/no-extraneous-dependencies +import typeof {SourceMapGenerator} from 'source-map'; declare module 'postcss' { declare type NodeCallback = (Node, number) => false | void; @@ -23,20 +24,23 @@ declare module 'postcss' { declare interface Root extends Container {} declare class Processor { - process(css: string | Result, opts?: processOptions): Promise; + process( + css: string | Result | Root, + opts?: ProcessOptions, + ): Promise; } - declare type ProcessOptions = {| - from?: string, - to?: string, - map?: MapOptions, - parser?: parser, - stringifier?: stringifier, - syntax?: {| + declare type ProcessOptions = $Shape<{| + from: string, + to: string, + map: MapOptions, + parser: parser, + stringifier: stringifier, + syntax: {| parser: parser, stringifier: stringifier, |}, - |}; + |}>; declare type MapOptions = {| inline?: boolean, diff --git a/packages/core/cache/src/Cache.js b/packages/core/cache/src/Cache.js index 922d5e01e92..e5b85a0bec9 100644 --- a/packages/core/cache/src/Cache.js +++ b/packages/core/cache/src/Cache.js @@ -8,7 +8,7 @@ import type {FileSystem} from '@parcel/fs'; import path from 'path'; import logger from '@parcel/logger'; import {serialize, deserialize, registerSerializableClass} from '@parcel/core'; -// $FlowFixMe this is untyped +// flowlint-next-line untyped-import:off import packageJson from '../package.json'; export default class Cache { diff --git a/packages/core/core/src/utils.js b/packages/core/core/src/utils.js index 8a21633bb89..b55c9654120 100644 --- a/packages/core/core/src/utils.js +++ b/packages/core/core/src/utils.js @@ -10,7 +10,7 @@ import Graph from './Graph'; import ParcelConfig from './ParcelConfig'; import {RequestGraph} from './RequestTracker'; import Config from './public/Config'; -// $FlowFixMe this is untyped +// flowlint-next-line untyped-import:off import packageJson from '../package.json'; export function getBundleGroupId(bundleGroup: BundleGroup): string { diff --git a/packages/core/integration-tests/test/css.js b/packages/core/integration-tests/test/css.js index c42859a98a4..b11ca128e15 100644 --- a/packages/core/integration-tests/test/css.js +++ b/packages/core/integration-tests/test/css.js @@ -264,6 +264,7 @@ describe('css', () => { path.join(__dirname, '/integration/cssnano/index.js'), { minify: true, + sourceMaps: false, }, ); @@ -275,19 +276,43 @@ describe('css', () => { assert(css.includes('.local')); assert(css.includes('.index')); - // TODO: Make this `2` when a `sourceMappingURL` is added assert.equal(css.split('\n').length, 1); }); + it('should produce a sourcemap when sourceMaps are used', async function() { + await bundle(path.join(__dirname, '/integration/cssnano/index.js'), { + minify: true, + }); + + let css = await outputFS.readFile(path.join(distDir, 'index.css'), 'utf8'); + assert(css.includes('.local')); + assert(css.includes('.index')); + + let lines = css.trim().split('\n'); + assert.equal(lines.length, 2); + assert.equal(lines[1], '/*# sourceMappingURL=index.css.map */'); + + let map = JSON.parse( + await outputFS.readFile(path.join(distDir, 'index.css.map'), 'utf8'), + ); + assert.equal(map.file, 'index.css.map'); + assert.equal(map.mappings, 'AAAA,OACA,WACA,CCFA,OACA,SACA'); + assert.deepEqual(map.sources, [ + 'integration/cssnano/local.css', + 'integration/cssnano/index.css', + ]); + }); + it('should inline data-urls for text-encoded files', async () => { - await bundle(path.join(__dirname, '/integration/data-url/text.css')); + await bundle(path.join(__dirname, '/integration/data-url/text.css'), { + sourceMaps: false, + }); let css = await outputFS.readFile(path.join(distDir, 'text.css'), 'utf8'); assert.equal( - css, + css.trim(), `.svg-img { background-image: url('data:image/svg+xml,%3Csvg%3E%0A%0A%3C%2Fsvg%3E%0A'); -} -`, +}`, ); }); diff --git a/packages/core/integration-tests/test/html.js b/packages/core/integration-tests/test/html.js index 5ec98f3b388..24389970b1b 100644 --- a/packages/core/integration-tests/test/html.js +++ b/packages/core/integration-tests/test/html.js @@ -329,8 +329,8 @@ describe('html', function() { // mergeStyles assert( - html.includes( - '', + html.match( + /')); + assert( + html.match( + /