Skip to content

Commit 7f2d801

Browse files
committed
test: add test for special use case
1 parent f30c0b2 commit 7f2d801

File tree

10 files changed

+83
-0
lines changed

10 files changed

+83
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.desktop {
2+
color: green;
3+
}

test/cases/css-import-dynamic-custom/expected/main.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mobile {
2+
color: red;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
html,
2+
body {
3+
width: 100%;
4+
height: 100%;
5+
}
6+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.desktop {
2+
color: green;
3+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import mobileHref from './mobile.css?rawurl';
2+
import desktopHref from './desktop.css?rawurl';
3+
4+
/**
5+
* Your custom dynamic CSS loader.
6+
*
7+
* @param {string} href
8+
*/
9+
function loadCss(href) {
10+
const link = document.createElement('link');
11+
link.rel = 'stylesheet';
12+
link.href = href;
13+
document.head.appendChild(link);
14+
}
15+
16+
let isMobile = false;
17+
let isDesktop = true;
18+
19+
if (isMobile) {
20+
loadCss(mobileHref);
21+
}
22+
23+
if (isDesktop) {
24+
loadCss(desktopHref);
25+
}
26+
27+
console.log('main', { isMobile, isDesktop });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mobile {
2+
color: red;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
html,
2+
body {
3+
width: 100%;
4+
height: 100%;
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const path = require('path');
2+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
3+
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
4+
5+
module.exports = {
6+
mode: 'production',
7+
output: {
8+
path: path.join(__dirname, 'dist/'),
9+
},
10+
entry: {
11+
main: ['./src/main.js' ],
12+
style: ['./src/style.css'],
13+
},
14+
module: {
15+
rules: [
16+
{
17+
test: /\.css$/,
18+
oneOf: [
19+
{ resourceQuery: /rawurl/, type: 'asset/resource', generator: { filename: '[name].css' } }, // ?rawurl => returns URL string
20+
{ use: [MiniCssExtractPlugin.loader, 'css-loader'] } // normal imports
21+
]
22+
}
23+
],
24+
},
25+
plugins: [
26+
new RemoveEmptyScriptsPlugin(),
27+
new MiniCssExtractPlugin({
28+
filename: '[name].css',
29+
}),
30+
],
31+
};

test/integration.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('common use case tests', () => {
1717
test('css-entry-only', () => compareFiles( 'css-entry-only'));
1818
test('css-import', () => compareFiles( 'css-import'));
1919
test('css-import-dynamic', () => compareFiles( 'css-import-dynamic'));
20+
test('css-import-dynamic-custom', () => compareFiles( 'css-import-dynamic-custom'));
2021
test('css-entry-with-ignored-hmr', () => compareFiles( 'css-entry-with-ignored-hmr', false));
2122
test('css-entry-with-query', () => compareFiles( 'css-entry-with-query'));
2223
test('multi-configuration', () => compareFiles( 'multi-configuration'));

0 commit comments

Comments
 (0)