1 | # How it works
|
---|
2 |
|
---|
3 | ## The problem
|
---|
4 |
|
---|
5 | The `resolve-url-loader` is typically used where SASS source files are transpiled to CSS. CSS being a format that webpack can readily ingest. So let's look at a basic example where the structure is basically CSS but is composed using SASS features.
|
---|
6 |
|
---|
7 | Working backwards, this is the final CSS we are after. Just a single rule with a single declaration.
|
---|
8 |
|
---|
9 | ```css
|
---|
10 | .cool {
|
---|
11 | background-image: url(cool.png);
|
---|
12 | }
|
---|
13 | ```
|
---|
14 |
|
---|
15 | When using SASS it's common for rules to come from different [partials](https://sass-lang.com/documentation/at-rules/import#partials), and for declarations to be composed using mixins and functions. Consider this more complicated project with imported files.
|
---|
16 |
|
---|
17 | <img src="detailed-problem.svg" alt="the detailed problem" width="363px" height="651px">
|
---|
18 |
|
---|
19 | All the subdirectories here contributed something to the rule, so we could reasonably place the asset in any of them. And any of these locations might be the "correct" to our way of thinking.
|
---|
20 |
|
---|
21 | There could actually be a separate `cool.png` in each of the subdirectories! π€― In that case, which one gets used?
|
---|
22 |
|
---|
23 | The answer: none. π Webpack expects asset paths to be relative to the root SASS file `src/styles.scss`. So for the CSS `url(cool.png)` it will look for `src/cool.png` which is not present. π₯
|
---|
24 |
|
---|
25 | All our assets are in subdirecties `src/foo/cool.png` or `src/foo/bar/cool.png` or `src/foo/bar/baz/cool.png`. We need to re-write the `url()` to point to the one we intend. But right now that's pretty ambiguous.
|
---|
26 |
|
---|
27 | Worse still, Webpack doesn't know any of these nested SCSS files were part of the SASS composition. Meaing it doesn't know there _are_ nested directories in the first place. How do we rewite to something we don't know about?
|
---|
28 |
|
---|
29 | **The problem:** How to identify contributing directectories and look for the asset in those directories in some well-defined priority order?
|
---|
30 |
|
---|
31 | **The crux:** How to identify what contributed to the SASS compilation, internally and post factum, but from within Webpack? π«
|
---|
32 |
|
---|
33 | ## The solution
|
---|
34 |
|
---|
35 | Sourcemaps! π
|
---|
36 |
|
---|
37 | Wait, don't run away! Sourcemaps might sound scary, but they solve our problem reasonably well. π
|
---|
38 |
|
---|
39 | The SASS compiler source-map can tell us which original SCSS file contributed each character in the resulting CSS.
|
---|
40 |
|
---|
41 | The SASS source-map is also something we can access from within Webpack.
|
---|
42 |
|
---|
43 | ### concept
|
---|
44 |
|
---|
45 | Continuing with the example let's compile SASS on the command line. You can do this several different ways but I prefer [npx](https://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner).
|
---|
46 |
|
---|
47 | ```sh
|
---|
48 | > npx node-sass src/styles.scss --output . --output-style expanded --source-map true
|
---|
49 | ```
|
---|
50 |
|
---|
51 | Using the experimental `sourcemap-to-string` package (also in this repository) we can visualise the SASS source on the left vs the output CSS on the right.
|
---|
52 |
|
---|
53 | ```
|
---|
54 | src/styles.scss
|
---|
55 | -------------------------------------------------------------------------------
|
---|
56 |
|
---|
57 | src/foo/_partial.scss
|
---|
58 | -------------------------------------------------------------------------------
|
---|
59 | 3:01 .coolβββββββββββββββββββββββββββββ 1:01 .coolβββββββββββββββββββββββββββββ
|
---|
60 | 3:06 βββββ ββββββββββββββββββββββββββββ 1:06 βββββ ββββββββββββββββββββββββββββ
|
---|
61 | 3:07 ββββββ{β 1:07 ββββββ{β
|
---|
62 | @include cool-background-image;β ββββββββββββββββββββββββββββββββ
|
---|
63 | }βββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββ
|
---|
64 | -:-- ββββββββββββββββββββββββββββββββββ 3:02 ββ
|
---|
65 | ββββββββββββββββββββββββββββββββββ β
|
---|
66 | ββββββββββββββββββββββββββββββββββ /*# sourceMappingURL=styles.css.ma
|
---|
67 | ββββββββββββββββββββββββββββββββββ p */ββββββββββββββββββββββββββββββ
|
---|
68 |
|
---|
69 | src/foo/bar/_mixins.scss
|
---|
70 | -------------------------------------------------------------------------------
|
---|
71 | 4:03 ββbackground-imageββββββββββββββββ 2:03 ββbackground-imageββββββββββββββββ
|
---|
72 | 4:19 ββββββββββββββββββ: get-url("cool" 2:19 ββββββββββββββββββ: ββββββββββββββ
|
---|
73 | );β ββββββββββββββββββββββββββββββββββ
|
---|
74 | }β ββββββββββββββββββββββββββββββββββ
|
---|
75 |
|
---|
76 | src/foo/bar/baz/_functions.scss
|
---|
77 | -------------------------------------------------------------------------------
|
---|
78 | 2:11 ββββββββββurl(#βββββββββββββββββββ 2:21 ββββββββββββββββββββurl(cool.png)β
|
---|
79 | 2:16 βββββββββββββββ{$temp}.png);β 2:34 βββββββββββββββββββββββββββββββββ;
|
---|
80 | }βββββββββββββββββββββββββββββββββ β
|
---|
81 | ββββββββββββββββββββββββββββββββββ }βββββββββββββββββββββββββββββββββ
|
---|
82 | ```
|
---|
83 |
|
---|
84 | As expected, the pure CSS portions are essentially the same in the source and the output.
|
---|
85 |
|
---|
86 | Meanwhile the indirect `@mixin` and `funtion` substitutes values into the output. But we can still clearly see where in the source that final value originated from.
|
---|
87 |
|
---|
88 | ### algorithm
|
---|
89 |
|
---|
90 | Now we know the original SCSS sources we can use a CSS parser such as `postcss` to process all the declaration values that contain `url()` and rewrite any file paths we find there.
|
---|
91 |
|
---|
92 | 1. Enumerate all declaration values
|
---|
93 | 2. Split the value into path substrings
|
---|
94 | 3. Evaluate the source-map at that location, find the original source file
|
---|
95 | 4. Rebase the path to that original source file.
|
---|
96 |
|
---|
97 | For our example, this algorithm will always give us the asset located in the `baz` subdirectory. Clearly evaluating the source-map at just one location is not enough. Any of the directories that contributed source files to the rule-set might be considered the "correct" place to store the asset and all these files contributed different parts of the rule-set, not just the declaration value.
|
---|
98 |
|
---|
99 | We stop short of evaluating the source-map for _every characer_ in the rule-set and instead we chose a small number of meaningful points.
|
---|
100 |
|
---|
101 | | | label | sampling location | in the example | implies asset |
|
---|
102 | |---|-----------|------------------------------------------|---------------------------|--------------------------------------------------|
|
---|
103 | | 1 | subString | start of **argument** to the `url()` | `c` in `cool.png` | `src/foo/bar/baz/cool.png` |
|
---|
104 | | 2 | value | start of **value** in the declaration | `u` in `url(...)` | `src/foo/bar/baz/cool.png` |
|
---|
105 | | 3 | property | start of **property** in the declaration | `b` in `background-image` | `src/foo/bar/cool.png` |
|
---|
106 | | 4 | selector | start of **selector** in the rule-set | `.` in `.selector` | `src/foo/cool.png` |
|
---|
107 |
|
---|
108 | These locations are tested in order. If an asset of the correct filename is found then we break and use that result.
|
---|
109 |
|
---|
110 | Note it is a quirk of the example that the `value` and `subString` locations imply the same file. In a more complex example this may not be true.
|
---|
111 |
|
---|
112 | If necessary the order can be customised or a custom file search (starting at each location) be implemented. Refer to the [advanced features](advanced-features.md).
|
---|
113 |
|
---|
114 |
|
---|
115 | ### webpack
|
---|
116 |
|
---|
117 | To operate on the `sass-loader` output, both **CSS** and **source-map**, we introduce `resolve-url-loader` containing the algorithm above.
|
---|
118 |
|
---|
119 | The `resolve-url-loader` rewrites asset paths found in `url()` notation using the `postcss` parser.
|
---|
120 |
|
---|
121 | This webpack configuration outlines some important points.
|
---|
122 |
|
---|
123 | ```javascript
|
---|
124 | rules: [
|
---|
125 | {
|
---|
126 | test: /\.scss$/,
|
---|
127 | use: [
|
---|
128 | {
|
---|
129 | loader: 'css-loader' // <-- assets are identified here
|
---|
130 | }, {
|
---|
131 | loader: 'resolve-url-loader' // <-- receives CSS and source-map from SASS compile
|
---|
132 | }, {
|
---|
133 | loader: 'sass-loader',
|
---|
134 | options: {
|
---|
135 | sourceMap: true, // <-- IMPORTANT!
|
---|
136 | sourceMapContents: false
|
---|
137 | }
|
---|
138 | }
|
---|
139 | ],
|
---|
140 | },
|
---|
141 | ...
|
---|
142 | {
|
---|
143 | test: /\.png$/, // <-- assets needs their own loader configuration
|
---|
144 | use: [ ... ]
|
---|
145 | }
|
---|
146 | ]
|
---|
147 | ```
|
---|
148 |
|
---|
149 | Its essential to explicitly configure the `sass-loader` for `sourceMap: true`. That way we definitely get a sourcemap from upstream SASS loader all the time, not just in developement mode or where `devtool` is used.
|
---|
150 |
|
---|
151 | Once the CSS reaches the `css-loader` webpack becomes aware of each of the asset files and will try to separately load and process them. You will need more Webpack configuration to make that work. Refer to the [troubleshooting docs](troubleshooting.md) before raising an issue.
|
---|
152 |
|
---|
153 | ### beyond...?
|
---|
154 |
|
---|
155 | The implementation here is limited to the webpack loader but it's plausible the algorithm could be realised as a `postcss` plugin in isolation using the [root.input.map](https://postcss.org/api/#postcss-input) property to access the incomming source-map.
|
---|
156 |
|
---|
157 | As a separate plugin it could be combined with other plugins in a single `postcss-loader` step. Processing multiple plugins together in this way without reparsing would arguably be more efficient.
|
---|
158 |
|
---|
159 | However as a Webpack loader we have full access to the loader API and the virtual file-system. This means maximum compatibility with `webpack-dev-server` and the rest of the Webpack ecosystem.
|
---|