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: use logger #991

Merged
merged 4 commits into from
Oct 27, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,393 changes: 1,073 additions & 1,320 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"del": "^6.0.0",
"del-cli": "^4.0.1",
"enhanced-resolve": "^5.8.2",
"eslint": "^7.30.0",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.3",
"fibers": "^5.0.0",
Expand Down
44 changes: 40 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,7 @@ async function getSassOptions(
: content;

// opt.outputStyle
if (
typeof options.outputStyle === "undefined" &&
isProductionLikeMode(loaderContext)
) {
if (!options.outputStyle && isProductionLikeMode(loaderContext)) {
options.outputStyle = "compressed";
}

Expand Down Expand Up @@ -231,6 +228,45 @@ async function getSassOptions(
options.charset = true;
}

if (!options.logger) {
const logger = loaderContext.getLogger("sass-loader");
const formatSpan = (span) =>
`${span.url || "-"}:${span.start.line}:${span.start.column}: `;

options.logger = {
debug(message, loggerOptions) {
let builtMessage = "";

if (loggerOptions.span) {
builtMessage = formatSpan(loggerOptions.span);
}

builtMessage += message;

logger.debug(builtMessage);
},
warn(message, loggerOptions) {
let builtMessage = "";

if (loggerOptions.deprecation) {
builtMessage += "Deprecation ";
}

if (loggerOptions.span && !loggerOptions.stack) {
builtMessage = formatSpan(loggerOptions.span);
}

builtMessage += message;

if (loggerOptions.stack) {
builtMessage += `\n\n${loggerOptions.stack}`;
}

logger.warn(builtMessage);
},
};
}

return options;
}

Expand Down
110 changes: 110 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should allow to use own logger (dart-sass) (sass): css 1`] = `
"a {
color: red;
}"
`;

exports[`loader should allow to use own logger (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should allow to use own logger (dart-sass) (sass): logs 1`] = `
Array [
Object {
"message": "My debug message",
"type": "debug",
},
Object {
"message": "My warning message",
"type": "warn",
},
]
`;

exports[`loader should allow to use own logger (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should allow to use own logger (dart-sass) (scss): css 1`] = `
"a {
color: red;
}"
`;

exports[`loader should allow to use own logger (dart-sass) (scss): errors 1`] = `Array []`;

exports[`loader should allow to use own logger (dart-sass) (scss): logs 1`] = `
Array [
Object {
"message": "My debug message",
"type": "debug",
},
Object {
"message": "My warning message",
"type": "warn",
},
]
`;

exports[`loader should allow to use own logger (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should import .import.sass files (dart-sass) (sass): css 1`] = `
"a {
display: block;
Expand Down Expand Up @@ -1326,6 +1372,70 @@ SassError: Can't find stylesheet to import.",

exports[`loader should throw an error with a explicit file and a file does not exist using "@use" rule (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should use webpack logger (dart-sass) (sass): css 1`] = `
"a {
color: red;
}"
`;

exports[`loader should use webpack logger (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should use webpack logger (dart-sass) (sass): logs 1`] = `
Array [
Array [
Object {
"args": Array [
"file:///<cwd>/sass/logging.sass:0:0: My debug message",
],
"type": "debug",
},
Object {
"args": Array [
"My warning message

test/sass/logging.sass 2:1 root stylesheet
",
],
"type": "warn",
},
],
]
`;

exports[`loader should use webpack logger (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should use webpack logger (dart-sass) (scss): css 1`] = `
"a {
color: red;
}"
`;

exports[`loader should use webpack logger (dart-sass) (scss): errors 1`] = `Array []`;

exports[`loader should use webpack logger (dart-sass) (scss): logs 1`] = `
Array [
Array [
Object {
"args": Array [
"file:///<cwd>/scss/logging.scss:0:0: My debug message",
],
"type": "debug",
},
Object {
"args": Array [
"My warning message

test/scss/logging.scss 2:1 root stylesheet
",
],
"type": "warn",
},
],
]
`;

exports[`loader should use webpack logger (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should watch firstly in the "includePaths" values (dart-sass) (sass): css 1`] = `
".bar {
color: red;
Expand Down
2 changes: 2 additions & 0 deletions test/helpers/getCodeFromSass.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ function getCodeFromSass(testId, options) {
.concat([testImporter])
: [testImporter];

sassOptions.logger = { debug: () => {}, warn: () => {} };

const { css, map } = implementation.renderSync(sassOptions);

return { css: css.toString(), sourceMap: map };
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function getCompiler(fixture, config = {}, options = {}) {
// eslint-disable-next-line no-undefined
resolve: config.resolve || undefined,
};

// Compiler Options
// eslint-disable-next-line no-param-reassign
options = Object.assign({ output: false }, options);
Expand All @@ -71,7 +72,6 @@ export default function getCompiler(fixture, config = {}, options = {}) {

if (!options.output) {
compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.outputFileSystem.join = path.join.bind(path);
}

return compiler;
Expand Down
64 changes: 64 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "path";
import url from "url";

import nodeSass from "node-sass";
import dartSass from "sass";
Expand Down Expand Up @@ -1737,6 +1738,69 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should use webpack logger (${implementationName}) (${syntax})`, async () => {
const testId = getTestId("logging", syntax);
const options = {
implementation: getImplementationByName(implementationName),
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromSass = getCodeFromSass(testId, options);
const logs = [];

for (const [name, value] of stats.compilation.logging) {
if (/sass-loader/.test(name)) {
logs.push(
value.map((item) => {
return {
type: item.type,
args: item.args.map((arg) =>
arg
.replace(url.pathToFileURL(__dirname), "file:///<cwd>")
.replace(/\\/g, "/")
),
};
})
);
}
}

expect(codeFromBundle.css).toBe(codeFromSass.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(logs).toMatchSnapshot("logs");
});

it(`should allow to use own logger (${implementationName}) (${syntax})`, async () => {
const testId = getTestId("logging", syntax);
const logs = [];
const options = {
implementation: getImplementationByName(implementationName),
sassOptions: {
logger: {
warn: (message) => {
logs.push({ type: "warn", message });
},
debug: (message) => {
logs.push({ type: "debug", message });
},
},
},
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromSass = getCodeFromSass(testId, options);

expect(codeFromBundle.css).toBe(codeFromSass.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(logs).toMatchSnapshot("logs");
});
}
});
});
Expand Down
5 changes: 5 additions & 0 deletions test/sass/logging.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@debug "My debug message"
@warn "My warning message"

a
color: red
6 changes: 6 additions & 0 deletions test/scss/logging.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@debug "My debug message";
@warn "My warning message";

a {
color: red;
}