Skip to content

Commit 9f58b90

Browse files
authored
Merge pull request #430 from cr0ybot/feature/block-transform-sass-assets
Transform Sass asset file extensions in block.json
2 parents 4c4ba03 + 6573d42 commit 9f58b90

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

.changeset/hot-worms-compete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"10up-toolkit": patch
3+
---
4+
5+
Fix: transform file extension for .sass and .scss assets inside block.json files

packages/toolkit/utils/__tests__/blocks.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,51 @@ describe('transformBlockJson', () => {
162162
),
163163
);
164164
});
165+
166+
it('transforms sass and scss to css', () => {
167+
expect(
168+
transformBlockJson(
169+
JSON.stringify({
170+
style: 'file:./style.sass',
171+
editorStyle: 'file:./editor.scss',
172+
viewStyle: 'file:./view.sass',
173+
version: '12345678',
174+
}),
175+
absoluteteFileName,
176+
),
177+
).toEqual(
178+
JSON.stringify(
179+
{
180+
style: 'file:./style.css',
181+
editorStyle: 'file:./editor.css',
182+
viewStyle: 'file:./view.css',
183+
version: '12345678',
184+
},
185+
null,
186+
2,
187+
),
188+
);
189+
expect(
190+
transformBlockJson(
191+
JSON.stringify({
192+
style: ['file:./style.sass', 'file:./style.scss'],
193+
editorStyle: ['file:./editor.sass', 'file:./editor.scss'],
194+
viewStyle: ['file:./view.sass', 'file:./view.scss'],
195+
version: '12345678',
196+
}),
197+
absoluteteFileName,
198+
),
199+
).toEqual(
200+
JSON.stringify(
201+
{
202+
style: ['file:./style.css', 'file:./style.css'],
203+
editorStyle: ['file:./editor.css', 'file:./editor.css'],
204+
viewStyle: ['file:./view.css', 'file:./view.css'],
205+
version: '12345678',
206+
},
207+
null,
208+
2,
209+
),
210+
);
211+
});
165212
});

packages/toolkit/utils/blocks.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ const path = require('path');
22
const { getFileContentHash } = require('./file');
33

44
const JS_ASSET_KEYS = ['script', 'editorScript', 'viewScript', 'viewScriptModule', 'scriptModule'];
5+
const CSS_ASSET_KEYS = ['style', 'editorStyle', 'viewStyle'];
56

67
/**
78
* Transform the asset path from `.ts or .tsx` to `.js`
89
*
9-
* When a block.json file has a script or style property that points to a `.ts or .tsx` file,
10+
* When a block.json file has a script property that points to a `.ts or .tsx` file,
1011
* this function will transform the path to point to the `.js` file instead.
1112
*
1213
* @param {string|Array<string>} asset - The asset path to transform
@@ -27,6 +28,30 @@ function transformTSAsset(asset) {
2728
return Array.isArray(asset) ? asset.map(replaceExtension) : replaceExtension(asset);
2829
}
2930

31+
/**
32+
* Transform the asset path from `.sass or .scss` to `.css`
33+
*
34+
* When a block.json file has a style property that points to a `.sass or .scss` file,
35+
* this function will transform the path to point to the `.css` file instead.
36+
*
37+
* @param {string|Array<string>} asset - The asset path to transform
38+
* @returns {string|Array<string>}
39+
*/
40+
function transformSassAsset(asset) {
41+
function replaceExtension(filePath) {
42+
const isFilePath = filePath.startsWith('file:');
43+
if (!isFilePath) {
44+
return filePath;
45+
}
46+
47+
// replace the `.sass or .scss` extension with `.css`
48+
const cssPath = filePath.replace(/\.s[ac]ss$/, '.css');
49+
return cssPath;
50+
}
51+
52+
return Array.isArray(asset) ? asset.map(replaceExtension) : replaceExtension(asset);
53+
}
54+
3055
const transformBlockJson = (content, absoluteFilename) => {
3156
const rawMetadata = content.toString();
3257
if (rawMetadata === '') {
@@ -70,6 +95,12 @@ const transformBlockJson = (content, absoluteFilename) => {
7095
}
7196
});
7297

98+
CSS_ASSET_KEYS.forEach((key) => {
99+
if (metadata[key]) {
100+
newMetadata[key] = transformSassAsset(metadata[key]);
101+
}
102+
});
103+
73104
return JSON.stringify(newMetadata, null, 2);
74105
};
75106

0 commit comments

Comments
 (0)