-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
137 lines (122 loc) · 3.72 KB
/
Copy pathwebpack.config.js
File metadata and controls
137 lines (122 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const postcss = require( 'postcss' );
const prefixSelector = require( 'postcss-prefix-selector' );
const PREFIX = '.draad-adreszoeker';
/**
* Webpack plugin that scopes CSS strings injected at runtime by @gemeente-denhaag
* and @utrecht component packages. These packages embed their CSS as string
* literals and inject them via <style> tags when their JS modules load.
* Without scoping, those styles leak globally and can break other page elements.
*
* This plugin intercepts emitted JS bundles, finds the embedded CSS strings,
* and prefixes all selectors with .draad-adreszoeker before the files are written.
*/
class ScopeInjectedCssPlugin {
apply( compiler ) {
compiler.hooks.thisCompilation.tap(
'ScopeInjectedCssPlugin',
( compilation ) => {
compilation.hooks.processAssets.tapPromise(
{
name: 'ScopeInjectedCssPlugin',
stage: compiler.webpack.Compilation
.PROCESS_ASSETS_STAGE_SUMMARIZE,
},
async ( assets ) => {
const processor = postcss( [
prefixSelector( {
prefix: PREFIX,
transform( prefix, selector, prefixed ) {
if (
selector === prefix ||
selector.startsWith( prefix + ' ' ) ||
selector.startsWith( prefix + '.' ) ||
selector.startsWith( prefix + ':' )
) {
return selector;
}
if (
selector === ':root' ||
selector === 'html' ||
selector === 'body'
) {
return prefix;
}
return prefixed;
},
} ),
] );
for ( const [ filename, asset ] of Object.entries(
assets
) ) {
if ( ! filename.endsWith( '.js' ) ) continue;
let source = asset.source();
const replacements = [];
// Find quoted CSS strings from design system packages.
// These are string literals that start with a .denhaag- or
// .utrecht- selector and contain CSS rules (have { and }).
const pattern =
/"(\.(?:denhaag|utrecht)-[a-zA-Z][^"]{50,})"/g;
let match;
while ( ( match = pattern.exec( source ) ) !== null ) {
const raw = match[ 1 ];
// Validate it looks like CSS
if (
! raw.includes( '{' ) ||
! raw.includes( '}' )
) {
continue;
}
// Skip if already scoped
if ( raw.includes( PREFIX ) ) continue;
// Unescape JS string escapes to get real CSS
const css = raw
.replace( /\\n/g, '\n' )
.replace( /\\t/g, '\t' )
.replace( /\\\\/g, '\\' );
try {
const result = await processor.process(
css,
{ from: undefined }
);
const prefixedCss = result.css;
// Re-escape for JS string literal
const escaped = prefixedCss
.replace( /\\/g, '\\\\' )
.replace( /\n/g, '\\n' )
.replace( /\t/g, '\\t' );
if ( escaped !== raw ) {
replacements.push( {
original: `"${ raw }"`,
replacement: `"${ escaped }"`,
} );
}
} catch ( e ) {
// Skip strings PostCSS cannot parse
}
}
if ( replacements.length > 0 ) {
let newSource = source;
for ( const {
original,
replacement,
} of replacements ) {
newSource = newSource
.split( original )
.join( replacement );
}
assets[ filename ] = new compiler.webpack.sources.RawSource(
newSource
);
}
}
}
);
}
);
}
}
module.exports = {
...defaultConfig,
plugins: [ ...defaultConfig.plugins, new ScopeInjectedCssPlugin() ],
};