Skip to content

Commit cdd50fd

Browse files
committed
fix(webpack): fix shared shadow-dom styles
to-string-loader does not work with style-loader.
1 parent 5b2f88a commit cdd50fd

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

app-min/src/main.ext

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import 'promise-polyfill/lib/polyfill';
44
// @endif
55
import Aurelia/* @if shadow-dom *//*, { StyleConfiguration }*//* @endif */ from 'aurelia';
66
import { MyApp } from './my-app';
7+
// @if shadow-dom
8+
// @if webpack
9+
// Css files imported in this main file are NOT processed by style-loader
10+
// They are for sharedStyles in shadowDOM.
11+
// However, css files imported in other js/ts files are processed by style-loader.
12+
// @endif
13+
// import shared from './shared./* @if css */css/* @endif *//* @if less */less/* @endif *//* @if sass */scss/* @endif */';
14+
// @endif
715

816
Aurelia
917
// @if shadow-dom
1018
/*
1119
.register(StyleConfiguration.shadowDOM({
1220
// optionally add the shared styles for all components
13-
sharedStyles: []
21+
sharedStyles: [shared]
1422
}))
1523
*/
1624
// @endif

webpack/webpack.config.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ const cssLoader = {
1717
}
1818
};
1919
// @endif
20+
// @if sass
21+
const sassLoader = {
22+
loader: 'sass-loader',
23+
options: {
24+
sassOptions: {
25+
includePaths: ['node_modules']
26+
}
27+
}
28+
};
29+
// @endif
2030

2131
const postcssLoader = {
2232
loader: 'postcss-loader',
@@ -57,33 +67,31 @@ module.exports = function(env/* @if jasmine || tape || mocha*/, { runTest }/* @e
5767
},
5868
module: {
5969
rules: [
60-
// @if css
61-
// @if !shadow-dom
62-
{ test: /\.css$/i, use: [ "style-loader", cssLoader, postcssLoader ] },
63-
// @endif
64-
// @if shadow-dom
65-
{ test: /\.css$/i, issuer: /\.(js|ts)$/, use: [ "to-string-loader", "style-loader", cssLoader, postcssLoader ] },
66-
{ test: /\.css$/i, issuer: /\.html$/, use: [ "to-string-loader", cssLoader, postcssLoader ] },
67-
// @endif
68-
// @endif
69-
// @if less
7070
// @if !shadow-dom
71-
{ test: /\.less$/i, use: [ "style-loader", cssLoader, postcssLoader, "less-loader" ] },
71+
{ test: /\./* @if css */css/* @endif *//* @if less */less/* @endif *//* @if sass */scss/* @endif */$/i, use: [ 'style-loader', cssLoader, postcssLoader/* @if less */, 'less-loader'/* @endif *//* @if sass */, sassLoader/* @endif */ ] },
7272
// @endif
7373
// @if shadow-dom
74-
{ test: /\.less$/i, issuer: /\.(js|ts)$/, use: [ "to-string-loader", "style-loader", cssLoader, postcssLoader, "less-loader" ] },
75-
{ test: /\.less$/i, issuer: /\.html$/, use: [ "to-string-loader", cssLoader, postcssLoader, "less-loader" ] },
76-
// @endif
77-
// @endif
78-
// @if sass
79-
// @if !shadow-dom
80-
{ test: /\.scss$/i, use: [ "style-loader", cssLoader, postcssLoader, { loader: "sass-loader", options: { sassOptions: { includePaths: ["node_modules"] } } } ] },
81-
// @endif
82-
// @if shadow-dom
83-
{ test: /\.scss$/i, issuer: /\.(js|ts)$/, use: [ "to-string-loader", "style-loader", cssLoader, postcssLoader, { loader: "sass-loader", options: { sassOptions: { includePaths: ["node_modules"] } } } ] },
84-
{ test: /\.scss$/i, issuer: /\.html$/, use: [ "to-string-loader", cssLoader, postcssLoader, { loader: "sass-loader", options: { sassOptions: { includePaths: ["node_modules"] } } } ] },
85-
// @endif
74+
{
75+
test: /\./* @if css */css/* @endif *//* @if less */less/* @endif *//* @if sass */scss/* @endif */$/i,
76+
// For style loaded in src/main.js, it's not loaded by style-loader.
77+
// It's for shared styles for shadow-dom only.
78+
issuer: /\/src\/main\.(js|ts)$/,
79+
use: [ 'to-string-loader', cssLoader, postcssLoader/* @if less */, 'less-loader'/* @endif *//* @if sass */, sassLoader/* @endif */ ]
80+
},
81+
{
82+
test: /\./* @if css */css/* @endif *//* @if less */less/* @endif *//* @if sass */scss/* @endif */$/i,
83+
// For style loaded in other js/ts files, it's loaded by style-loader.
84+
// They are directly injected to HTML head.
85+
issuer: /(?<!\/src\/main)\.(js|ts)$/,
86+
use: [ 'style-loader', cssLoader, postcssLoader/* @if less */, 'less-loader'/* @endif *//* @if sass */, sassLoader/* @endif */ ]
87+
},
88+
{
89+
test: /\./* @if css */css/* @endif *//* @if less */less/* @endif *//* @if sass */scss/* @endif */$/i,
90+
issuer: /\.html$/,
91+
use: [ 'to-string-loader', cssLoader, postcssLoader/* @if less */, 'less-loader'/* @endif *//* @if sass */, sassLoader/* @endif */ ]
92+
},
8693
// @endif
94+
8795
// @if babel
8896
{ test: /\.js$/i, use: ['babel-loader', '@aurelia/webpack-loader'], exclude: /node_modules/ },
8997
// @endif
@@ -96,7 +104,7 @@ module.exports = function(env/* @if jasmine || tape || mocha*/, { runTest }/* @e
96104
use: {
97105
loader: '@aurelia/webpack-loader',
98106
options: {
99-
// The other possible Shadow DOM mode is "closed".
107+
// The other possible Shadow DOM mode is 'closed'.
100108
// If you turn on "closed" mode, there will be difficulty to perform e2e
101109
// tests (such as Cypress). Because shadowRoot is not accessible through
102110
// standard DOM APIs in "closed" mode.

0 commit comments

Comments
 (0)