Skip to content
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
34 changes: 34 additions & 0 deletions test/__snapshots__/built-in-css.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ exports[`built-in CSS support > should emit assets from \`url()\` 2`] = `
[]
`;

exports[`built-in CSS support > should emit less warning as webpack warning 1`] = `
[
"ModuleWarning: Module Warning (from \`replaced original path\`):\\nWARNING: extend ' .body1' has no matches"
]
`;

exports[`built-in CSS support > should emit less warning as webpack warning 2`] = `
[]
`;

exports[`built-in CSS support > should generate source maps 1`] = `
[]
`;
Expand All @@ -24,6 +34,30 @@ exports[`built-in CSS support > should generate source maps 2`] = `
[]
`;

exports[`built-in CSS support > should not treat a file as a CSS module with the \`css\` type 1`] = `
".box {\\n color: #fe33ac;\\n}\\n.nested {\\n color: #fe33ac;\\n padding: 10px;\\n}\\n\\n"
`;

exports[`built-in CSS support > should not treat a file as a CSS module with the \`css\` type 2`] = `
[]
`;

exports[`built-in CSS support > should not treat a file as a CSS module with the \`css\` type 3`] = `
[]
`;

exports[`built-in CSS support > should treat any file as a CSS module with the \`css/module\` type 1`] = `
".built-in-css_basic_less-imported {\\n color: hotpink;\\n}\\n.built-in-css_basic_less-modules-dir-some-module {\\n color: hotpink;\\n}\\n.built-in-css_basic_less-box {\\n color: #fe33ac;\\n border-color: #fdcdea;\\n background: url(circle.svg);\\n}\\n\\n"
`;

exports[`built-in CSS support > should treat any file as a CSS module with the \`css/module\` type 2`] = `
[]
`;

exports[`built-in CSS support > should treat any file as a CSS module with the \`css/module\` type 3`] = `
[]
`;

exports[`built-in CSS support > should work 1`] = `
".imported {\\n color: hotpink;\\n}\\n.modules-dir-some-module {\\n color: hotpink;\\n}\\n.box {\\n color: #fe33ac;\\n border-color: #fdcdea;\\n background: url(circle.svg);\\n}\\n\\n"
`;
Expand Down
38 changes: 38 additions & 0 deletions test/built-in-css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ describe("built-in CSS support", { timeout: 30000 }, () => {
t.assert.snapshot(getErrors(stats));
});

it("should not treat a file as a CSS module with the `css` type", async (t) => {
const testId = "./built-in-css/style.module.less";
const compiler = getCssCompiler(testId, {}, { type: "css" });
const stats = await compile(compiler);
const css = readAsset("main.css", compiler, stats);

assert.match(css, /^\.box\b/m, "Expected local class names to be kept");
t.assert.snapshot(css);
t.assert.snapshot(getWarnings(stats));
t.assert.snapshot(getErrors(stats));
});

it("should treat any file as a CSS module with the `css/module` type", async (t) => {
const testId = "./built-in-css/basic.less";
const compiler = getCssCompiler(testId, {}, { type: "css/module" });
const stats = await compile(compiler);
const css = readAsset("main.css", compiler, stats);

assert.doesNotMatch(
css,
/^\.box\b/m,
"Expected local class names to be renamed",
);
assert.match(css, /^\.[\w-]+-box\b/m);
t.assert.snapshot(css);
t.assert.snapshot(getWarnings(stats));
t.assert.snapshot(getErrors(stats));
});

it("should work with the `lessOptions` option", async (t) => {
const testId = "./built-in-css/variables.less";
const compiler = getCssCompiler(testId, {
Expand Down Expand Up @@ -104,6 +133,15 @@ describe("built-in CSS support", { timeout: 30000 }, () => {
t.assert.snapshot(getErrors(stats));
});

it("should emit less warning as webpack warning", async (t) => {
const testId = "./warn.less";
const compiler = getCssCompiler(testId);
const stats = await compile(compiler);

t.assert.snapshot(getWarnings(stats));
t.assert.snapshot(getErrors(stats));
});

it("should emit an error on a broken file", async (t) => {
const testId = "./error.less";
const compiler = getCssCompiler(testId);
Expand Down
7 changes: 4 additions & 3 deletions test/helpers/getCssCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
* (i.e. `experiments.css`) instead of `css-loader`/`style-loader`.
* @param {string} fixture fixture
* @param {object} loaderOptions loader options
* @param {object} config webpack config
* @param {object} config webpack config, the `type` property is used as the module type of the rule
* @returns {Compiler} compiler
*/
export default (fixture, loaderOptions = {}, config = {}) => {
const { type = "css/auto", ...webpackConfig } = config;
const fullConfig = {
mode: "development",
devtool: config.devtool || false,
Expand All @@ -36,7 +37,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
rules: [
{
test: /\.less$/i,
type: "css/auto",
type,
use: [
{
loader: path.resolve(__dirname, "../../src/index.js"),
Expand All @@ -47,7 +48,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
],
},
plugins: [],
...config,
...webpackConfig,
};

const compiler = webpack(fullConfig);
Expand Down