|
1 | 1 | import path from "path"; |
2 | 2 |
|
| 3 | +import postcss from "postcss"; |
3 | 4 | import postcssPresetEnv from "postcss-preset-env"; |
4 | 5 |
|
5 | 6 | import { |
@@ -207,6 +208,84 @@ describe("loader", () => { |
207 | 208 | expect(getErrors(stats)).toMatchSnapshot("errors"); |
208 | 209 | }); |
209 | 210 |
|
| 211 | + it("should not pass a BOM added by a previous loader to postcss", async () => { |
| 212 | + const BOM = "\uFEFF"; |
| 213 | + const processSpy = jest.spyOn(Object.getPrototypeOf(postcss()), "process"); |
| 214 | + const compiler = getCompiler( |
| 215 | + "./modules/issue-1678/source.js", |
| 216 | + {}, |
| 217 | + { |
| 218 | + module: { |
| 219 | + rules: [ |
| 220 | + { |
| 221 | + test: /\.css$/i, |
| 222 | + use: [ |
| 223 | + { loader: path.resolve(__dirname, "../src") }, |
| 224 | + { loader: "./modules/issue-1678/with-bom-loader.js" }, |
| 225 | + ], |
| 226 | + }, |
| 227 | + ], |
| 228 | + }, |
| 229 | + }, |
| 230 | + ); |
| 231 | + |
| 232 | + const stats = await compile(compiler); |
| 233 | + const executed = getExecutedCode("main.bundle.js", compiler, stats); |
| 234 | + |
| 235 | + expect(processSpy).toHaveBeenCalledTimes(2); |
| 236 | + |
| 237 | + for (const [css] of processSpy.mock.calls) { |
| 238 | + expect(css.startsWith(BOM)).toBe(false); |
| 239 | + } |
| 240 | + |
| 241 | + // postcss >= 8.5.24 keeps the BOM, so without stripping it lands between |
| 242 | + // the two stylesheets and a browser drops the rule after it. |
| 243 | + expect(executed).not.toContain(BOM); |
| 244 | + expect(executed).toContain(".first::after"); |
| 245 | + expect(executed).toContain(".second::after"); |
| 246 | + |
| 247 | + expect( |
| 248 | + getModuleSource("./modules/issue-1678/first.css", stats), |
| 249 | + ).toMatchSnapshot("module"); |
| 250 | + expect(executed).toMatchSnapshot("result"); |
| 251 | + expect(getWarnings(stats)).toMatchSnapshot("warnings"); |
| 252 | + expect(getErrors(stats)).toMatchSnapshot("errors"); |
| 253 | + |
| 254 | + processSpy.mockRestore(); |
| 255 | + }); |
| 256 | + |
| 257 | + it("should not keep a BOM in an ast reused from a previous loader", async () => { |
| 258 | + const BOM = "\uFEFF"; |
| 259 | + const compiler = getCompiler( |
| 260 | + "./modules/issue-1678/source.js", |
| 261 | + {}, |
| 262 | + { |
| 263 | + module: { |
| 264 | + rules: [ |
| 265 | + { |
| 266 | + test: /\.css$/i, |
| 267 | + use: [ |
| 268 | + { loader: path.resolve(__dirname, "../src") }, |
| 269 | + { loader: require.resolve("./helpers/ast-loader") }, |
| 270 | + { loader: "./modules/issue-1678/with-bom-loader.js" }, |
| 271 | + ], |
| 272 | + }, |
| 273 | + ], |
| 274 | + }, |
| 275 | + }, |
| 276 | + ); |
| 277 | + |
| 278 | + const stats = await compile(compiler); |
| 279 | + const executed = getExecutedCode("main.bundle.js", compiler, stats); |
| 280 | + |
| 281 | + expect(executed).not.toContain(BOM); |
| 282 | + expect(executed).toContain(".first::after"); |
| 283 | + expect(executed).toContain(".second::after"); |
| 284 | + |
| 285 | + expect(getWarnings(stats)).toMatchSnapshot("warnings"); |
| 286 | + expect(getErrors(stats)).toMatchSnapshot("errors"); |
| 287 | + }); |
| 288 | + |
210 | 289 | it('should work with "sass-loader"', async () => { |
211 | 290 | const compiler = getCompiler( |
212 | 291 | "./scss/source.js", |
|
0 commit comments