@wordpress/build 0.6.2-next.v.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/lib/build.mjs +62 -6
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/lib/build.mjs
CHANGED
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { readFile, writeFile, copyFile, mkdir, unlink } from 'fs/promises';
|
|
7
7
|
import path from 'path';
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
8
9
|
import { parseArgs } from 'node:util';
|
|
9
10
|
import esbuild from 'esbuild';
|
|
10
11
|
import glob from 'fast-glob';
|
|
11
12
|
import chokidar from 'chokidar';
|
|
12
13
|
import browserslistToEsbuild from 'browserslist-to-esbuild';
|
|
13
|
-
import { sassPlugin
|
|
14
|
+
import { sassPlugin } from 'esbuild-sass-plugin';
|
|
14
15
|
import postcss from 'postcss';
|
|
16
|
+
import postcssModules from 'postcss-modules';
|
|
15
17
|
import autoprefixer from 'autoprefixer';
|
|
16
18
|
import rtlcss from 'rtlcss';
|
|
17
19
|
import cssnano from 'cssnano';
|
|
@@ -133,6 +135,61 @@ function getSassOptions( workingDir ) {
|
|
|
133
135
|
};
|
|
134
136
|
}
|
|
135
137
|
|
|
138
|
+
function compileInlineStyle( { cssModules = false, minify = true } = {} ) {
|
|
139
|
+
return async function styleType( cssText, _dirname, filePath ) {
|
|
140
|
+
// Always hash the untransformed code.
|
|
141
|
+
const hash = createHash( 'sha1' )
|
|
142
|
+
.update( cssText )
|
|
143
|
+
.digest( 'hex' )
|
|
144
|
+
.slice( 0, 10 );
|
|
145
|
+
|
|
146
|
+
let moduleExports = null;
|
|
147
|
+
|
|
148
|
+
// Transform the code: CSS modules and minification.
|
|
149
|
+
const plugins = [
|
|
150
|
+
cssModules &&
|
|
151
|
+
postcssModules( {
|
|
152
|
+
generateScopedName: '[contenthash]__[local]',
|
|
153
|
+
getJSON: ( _, json ) => {
|
|
154
|
+
moduleExports = json;
|
|
155
|
+
},
|
|
156
|
+
} ),
|
|
157
|
+
minify &&
|
|
158
|
+
cssnano( {
|
|
159
|
+
preset: [
|
|
160
|
+
'default',
|
|
161
|
+
{ discardComments: { removeAll: true } },
|
|
162
|
+
],
|
|
163
|
+
} ),
|
|
164
|
+
].filter( Boolean );
|
|
165
|
+
|
|
166
|
+
const { css } = await postcss( plugins ).process( cssText, {
|
|
167
|
+
from: filePath,
|
|
168
|
+
map: false,
|
|
169
|
+
} );
|
|
170
|
+
|
|
171
|
+
let cssModule = `if (typeof document !== 'undefined' && !document.head.querySelector("style[data-wp-hash='${ hash }']")) {
|
|
172
|
+
const style = document.createElement("style");
|
|
173
|
+
style.setAttribute("data-wp-hash", "${ hash }");
|
|
174
|
+
style.appendChild(document.createTextNode(${ JSON.stringify( css ) }));
|
|
175
|
+
document.head.appendChild(style);
|
|
176
|
+
}
|
|
177
|
+
`;
|
|
178
|
+
|
|
179
|
+
// The CSS modules transform produces an `exports` object with class name mappings.
|
|
180
|
+
if ( moduleExports ) {
|
|
181
|
+
const exportsString = JSON.stringify( moduleExports );
|
|
182
|
+
cssModule += `export default ${ exportsString };\n`;
|
|
183
|
+
}
|
|
184
|
+
return cssModule;
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function inlineStyle( contents ) {
|
|
189
|
+
// The `contents` is already a JS code created by `compileInlineStyle`.
|
|
190
|
+
return contents;
|
|
191
|
+
}
|
|
192
|
+
|
|
136
193
|
/**
|
|
137
194
|
* Create style bundling plugins with the given working directory.
|
|
138
195
|
*
|
|
@@ -146,10 +203,8 @@ function createStyleBundlingPlugins( workingDir ) {
|
|
|
146
203
|
sassPlugin( {
|
|
147
204
|
embedded: true,
|
|
148
205
|
filter: /\.module\.(css|scss)$/,
|
|
149
|
-
transform:
|
|
150
|
-
|
|
151
|
-
} ),
|
|
152
|
-
type: 'style',
|
|
206
|
+
transform: compileInlineStyle( { cssModules: true } ),
|
|
207
|
+
type: inlineStyle,
|
|
153
208
|
...sassOptions,
|
|
154
209
|
} ),
|
|
155
210
|
// Handle regular CSS/SCSS files
|
|
@@ -157,7 +212,8 @@ function createStyleBundlingPlugins( workingDir ) {
|
|
|
157
212
|
sassPlugin( {
|
|
158
213
|
embedded: true,
|
|
159
214
|
filter: /\.(css|scss)$/,
|
|
160
|
-
|
|
215
|
+
transform: compileInlineStyle(),
|
|
216
|
+
type: inlineStyle,
|
|
161
217
|
...sassOptions,
|
|
162
218
|
} ),
|
|
163
219
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/build",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Build tool for WordPress plugins.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"publishConfig": {
|
|
78
78
|
"access": "public"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "eee1cfb1472f11183e40fb77465a5f13145df7ad"
|
|
81
81
|
}
|