Skip to content

Commit 1dad1fb

Browse files
feat: reuse postcss ast from other loaders (i.e postcss-loader) (#840)
1 parent fe94ebc commit 1dad1fb

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

lib/loader.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
const postcss = require('postcss');
6+
const postcssPkg = require('postcss/package.json');
67
const localByDefault = require('postcss-modules-local-by-default');
78
const extractImports = require('postcss-modules-extract-imports');
89
const modulesScope = require('postcss-modules-scope');
@@ -26,7 +27,7 @@ const {
2627
const Warning = require('./Warning');
2728
const CssSyntaxError = require('./CssSyntaxError');
2829

29-
module.exports = function loader(content, map) {
30+
module.exports = function loader(content, map, meta) {
3031
const callback = this.async();
3132
const options = getOptions(this) || {};
3233
const sourceMap = options.sourceMap || false;
@@ -49,6 +50,16 @@ module.exports = function loader(content, map) {
4950
}
5051
/* eslint-enable no-param-reassign */
5152

53+
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
54+
if (meta) {
55+
const { ast } = meta;
56+
57+
if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
58+
// eslint-disable-next-line no-param-reassign
59+
content = ast.root;
60+
}
61+
}
62+
5263
const resolveImport = options.import !== false;
5364
const resolveUrl = options.url !== false;
5465
const loaderContext = this;

test/__snapshots__/loader.test.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ You may need an appropriate loader to handle this file type.
444444

445445
exports[`loader should throws error when no loader for assets: warnings 1`] = `Array []`;
446446

447-
exports[`loader using together with "postcss-loader": errors 1`] = `Array []`;
447+
exports[`loader using together with "postcss-loader" and reuse \`ast\`: errors 1`] = `Array []`;
448448

449-
exports[`loader using together with "postcss-loader": module (evaluated) 1`] = `
449+
exports[`loader using together with "postcss-loader" and reuse \`ast\`: module (evaluated) 1`] = `
450450
Array [
451451
Array [
452452
1,
@@ -513,7 +513,7 @@ a:hover {
513513
]
514514
`;
515515

516-
exports[`loader using together with "postcss-loader": module 1`] = `
516+
exports[`loader using together with "postcss-loader" and reuse \`ast\`: module 1`] = `
517517
"var escape = require(\\"../../../lib/runtime/escape.js\\");
518518
exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
519519
// imports
@@ -526,7 +526,7 @@ exports.push([module.id, \\":root {\\\\n --fontSize: 1rem;\\\\n --mainColor: r
526526
"
527527
`;
528528

529-
exports[`loader using together with "postcss-loader": warnings 1`] = `Array []`;
529+
exports[`loader using together with "postcss-loader" and reuse \`ast\`: warnings 1`] = `Array []`;
530530

531531
exports[`loader using together with "sass-loader": errors 1`] = `Array []`;
532532

test/loader.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ describe('loader', () => {
9090
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
9191
});
9292

93-
it('using together with "postcss-loader"', async () => {
93+
it('using together with "postcss-loader" and reuse `ast`', async () => {
94+
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
9495
const config = {
9596
postcssLoader: true,
9697
postcssLoaderOptions: {

0 commit comments

Comments
 (0)