-
Notifications
You must be signed in to change notification settings - Fork 346
Ensure Dart Sass file:// sources use relative paths
#3527
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { mkdir, writeFile } from 'fs/promises' | ||
| import { dirname } from 'path' | ||
| import { dirname, relative } from 'path' | ||
|
|
||
| /** | ||
| * Write asset helper | ||
|
|
@@ -14,15 +14,27 @@ export async function write (filePath, result) { | |
| const writeTasks = [] | ||
|
|
||
| // Files to write | ||
| /** @type {SourceMap | undefined} */ | ||
| const map = result.map ? JSON.parse(result.map.toString()) : undefined | ||
| const code = 'css' in result ? result.css : result.code | ||
| const map = result.map?.toString() | ||
|
|
||
| // 1. Write code (example.js) | ||
| writeTasks.push(writeFile(filePath, code)) | ||
|
|
||
| // 2. Write source map (example.js.map) | ||
| if (map) { | ||
| writeTasks.push(writeFile(`${filePath}.map`, map)) | ||
| if (map && 'sources' in map) { | ||
| map.sources = map.sources | ||
|
|
||
| /** | ||
| * Make source file:// paths relative (e.g. for Dart Sass) | ||
| * {@link https://sass-lang.com/documentation/js-api/interfaces/CompileResult#sourceMap} | ||
| */ | ||
| .map((path) => path.startsWith('file://') | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PostCSS adds the Dart Sass entry point But all the other Dart Sass sources are absolute path |
||
| ? relative(dirname(filePath), new URL(path).pathname) | ||
| : path | ||
| ) | ||
|
|
||
| writeTasks.push(writeFile(`${filePath}.map`, JSON.stringify(map))) | ||
| } | ||
|
|
||
| await Promise.all(writeTasks) | ||
|
|
@@ -70,3 +82,11 @@ export async function write (filePath, result) { | |
| * | ||
| * @typedef {import('rollup').OutputChunk | import('terser').MinifyOutput | import('postcss').Result} AssetOutput | ||
| */ | ||
|
|
||
| /** | ||
| * Asset source maps | ||
| * | ||
| * {@link https://github.com/source-map/source-map-spec} | ||
| * | ||
| * @typedef {import('@jridgewell/source-map').DecodedSourceMap | import('@jridgewell/source-map').EncodedSourceMap} SourceMap | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,12 @@ describe('package/', () => { | |
| return output | ||
| })) | ||
|
|
||
| // Add Autoprefixer prefixes to all source '*.scss' files | ||
| .flatMap(mapPathTo(['**/*.scss'], ({ dir: requirePath, name }) => [ | ||
| join(requirePath, `${name}.scss`), | ||
| join(requirePath, `${name}.scss.map`) // with source map | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Service teams with source maps enabled will have been inspecting our Autoprefixer-transformed sources, not the original prefix-free |
||
| ])) | ||
|
|
||
| // Replaces source component '*.yaml' with: | ||
| // - `fixtures.json` fixtures for tests | ||
| // - `macro-options.json` component options | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,25 @@ export async function compileStylesheet ([modulePath, { srcPath, destPath, fileP | |
| let css | ||
| let map | ||
|
|
||
| // Configure PostCSS | ||
| /** | ||
| * Configure PostCSS | ||
| * | ||
| * @type {import('postcss').ProcessOptions} | ||
| */ | ||
| const options = { | ||
| from: moduleSrcPath, | ||
| to: moduleDestPath | ||
| to: moduleDestPath, | ||
|
|
||
| /** | ||
| * Always generate source maps for either: | ||
| * | ||
| * 1. PostCSS on Sass compiler result | ||
| * 2. PostCSS on Sass sources (Autoprefixer only) | ||
| */ | ||
| map: { | ||
| annotation: true, | ||
| inline: false | ||
| } | ||
| } | ||
|
|
||
| // Compile Sass to CSS | ||
|
|
@@ -79,10 +94,8 @@ export async function compileStylesheet ([modulePath, { srcPath, destPath, fileP | |
| })) | ||
|
|
||
| // Pass source maps to PostCSS | ||
| options.map = { | ||
| annotation: true, | ||
| inline: false, | ||
| prev: map | ||
| if (typeof options.map === 'object') { | ||
| options.map.prev = map | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PostCSS will automatically discover source maps via |
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need
map.sourcesa few lines down so parse toSourceMaphereOtherwise across Terser, Rollup and PostCSS we see source maps as:
objecttypes that needJSON.stringify().toJSON()and.toString()methodsstringtypeundefinedtypeThis can be seen via the AssetOutput
@typedefin this file