Skip to content
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

feat: reuse postcss ast from other loaders (i.e postcss-loader) #840

Merged
merged 1 commit into from Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/loader.js
Expand Up @@ -3,6 +3,7 @@
Author Tobias Koppers @sokra
*/
const postcss = require('postcss');
const postcssPkg = require('postcss/package.json');
const localByDefault = require('postcss-modules-local-by-default');
const extractImports = require('postcss-modules-extract-imports');
const modulesScope = require('postcss-modules-scope');
Expand All @@ -26,7 +27,7 @@ const {
const Warning = require('./Warning');
const CssSyntaxError = require('./CssSyntaxError');

module.exports = function loader(content, map) {
module.exports = function loader(content, map, meta) {
const callback = this.async();
const options = getOptions(this) || {};
const sourceMap = options.sourceMap || false;
Expand All @@ -49,6 +50,16 @@ module.exports = function loader(content, map) {
}
/* eslint-enable no-param-reassign */

// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
if (meta) {
const { ast } = meta;

if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
// eslint-disable-next-line no-param-reassign
content = ast.root;
}
}

const resolveImport = options.import !== false;
const resolveUrl = options.url !== false;
const loaderContext = this;
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -444,9 +444,9 @@ You may need an appropriate loader to handle this file type.

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

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

exports[`loader using together with "postcss-loader": module (evaluated) 1`] = `
exports[`loader using together with "postcss-loader" and reuse \`ast\`: module (evaluated) 1`] = `
Array [
Array [
1,
Expand Down Expand Up @@ -513,7 +513,7 @@ a:hover {
]
`;

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

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

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

Expand Down
3 changes: 2 additions & 1 deletion test/loader.test.js
Expand Up @@ -90,7 +90,8 @@ describe('loader', () => {
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
});

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