Skip to content

Commit a1da042

Browse files
committed
feat(tools): shared sass-transforming function
1 parent 7fd6531 commit a1da042

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed

.changeset/tasty-dragons-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": minor
3+
---
4+
5+
export `transformSass` helper from esbuild.js

docs/_plugins/bundle.cjs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-env node */
22
const esbuild = require('esbuild');
3-
const Sass = require('sass');
43

54
const { join } = require('path');
65
const { readdir } = require('fs/promises');
@@ -9,14 +8,6 @@ const { litCssPlugin } = require('esbuild-plugin-lit-css');
98
module.exports = {
109
async bundle() {
1110
const cwd = process.cwd();
12-
const transform = (data, { filePath }) => Sass.renderSync({
13-
data,
14-
file: filePath,
15-
includePaths: [
16-
join(cwd, 'node_modules'),
17-
],
18-
}).css.toString();
19-
2011
function pfeEnvPlugin() {
2112
return {
2213
name: 'pfe-env',
@@ -37,6 +28,7 @@ export const core = ${JSON.stringify(await readdir(join(cwd, 'core')))};
3728
}
3829

3930
const { packageVersion } = await import('@patternfly/pfe-tools/esbuild-plugins/package-version.js');
31+
const { transformSass } = await import('@patternfly/pfe-tools/esbuild.js');
4032

4133
await esbuild.build({
4234
entryPoints: ['docs/demo/bundle.ts'],
@@ -60,7 +52,7 @@ export const core = ${JSON.stringify(await readdir(join(cwd, 'core')))};
6052

6153
plugins: [
6254
// import scss files as LitElement CSSResult objects
63-
litCssPlugin({ filter: /.scss$/, transform }),
55+
litCssPlugin({ filter: /.scss$/, transform: transformSass }),
6456
pfeEnvPlugin({ cwd }),
6557
// replace `{{version}}` with each package's version
6658
packageVersion(),

tools/pfe-tools/dev-server.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import type { Plugin } from '@web/dev-server-core';
22
import type { DevServerConfig } from '@web/dev-server';
33
import type { InjectSetting } from '@web/dev-server-import-maps/dist/importMapsPlugin';
4-
import type { Meta as LitCSSModuleMeta } from '@pwrs/lit-css';
54

65
import { readdir } from 'fs/promises';
7-
import { join, dirname } from 'path';
6+
import { join } from 'path';
87
import { fileURLToPath } from 'url';
98

10-
import Sass from 'sass';
119
import litcssRollup from 'rollup-plugin-lit-css';
1210

1311
import { fromRollup } from '@web/dev-server-rollup';
1412
import { esbuildPlugin } from '@web/dev-server-esbuild';
1513
import { importMapsPlugin } from '@web/dev-server-import-maps';
14+
import { transformSass } from './esbuild.js';
1615

1716
export interface PfeDevServerConfigOptions {
1817
/** Extra dev server plugins */
@@ -27,9 +26,6 @@ const litcss = fromRollup(litcssRollup);
2726

2827
const rootDir = fileURLToPath(new URL('../..', import.meta.url));
2928

30-
// allow for imports in scss from node_modules
31-
const NODE_MODULES = join(rootDir, 'node_modules');
32-
3329
function appendLines(body: string, ...lines: string[]): string {
3430
return [body, ...lines].join('\n');
3531
}
@@ -69,15 +65,6 @@ function scssMimeType(): Plugin {
6965
};
7066
}
7167

72-
/** Transform `.scss` sources on-the-fly */
73-
function transform(source: string, { filePath }: LitCSSModuleMeta): string {
74-
const result = Sass.compileString(source, {
75-
loadPaths: [dirname(filePath), NODE_MODULES],
76-
});
77-
// TODO: forward sourcemaps by returning an object
78-
return result.css;
79-
}
80-
8168
/**
8269
* Creates a default config for PFE's dev server.
8370
*/
@@ -125,7 +112,7 @@ export function pfeDevServerConfig(options?: PfeDevServerConfigOptions): DevServ
125112
// so that we can list the elements by name
126113
bindNodeDataToBrowser({ cwd }),
127114
// load .scss files as lit CSSResult modules
128-
litcss({ include: ['**/*.scss'], transform }),
115+
litcss({ include: ['**/*.scss'], transform: transformSass }),
129116
],
130117
};
131118
}

tools/pfe-tools/esbuild.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from '@web/dev-server-core';
2-
import type { LitCSSOptions } from 'esbuild-plugin-lit-css';
2+
import type { Meta as LitCSSModuleMeta } from '@pwrs/lit-css';
33

44
import esbuild from 'esbuild';
55
import glob from 'glob';
@@ -11,15 +11,16 @@ import { litCssPlugin } from 'esbuild-plugin-lit-css';
1111

1212
import { nodeExternalsPlugin } from 'esbuild-node-externals';
1313
import { readdirSync } from 'fs';
14-
import { resolve, join } from 'path';
15-
16-
type Transformer = LitCSSOptions['transform'];
14+
import { resolve, join, dirname } from 'path';
15+
import { fileURLToPath } from 'url';
1716

1817
export interface PfeEsbuildOptions {
1918
/** Extra esbuild plugins */
2019
plugins?: Plugin[];
20+
/** Whether to bundle the components and all their dependencies */
21+
bundle?: boolean;
2122
/** repository root (default: process.cwd()) */
22-
cwd?: string
23+
cwd?: string;
2324
/** exclude these directories (under the workspace) from the build */
2425
entryPointFilesExcludes?: string[];
2526
/** packages to include */
@@ -28,10 +29,24 @@ export interface PfeEsbuildOptions {
2829
workspace?: string;
2930
/** production bundles are minified */
3031
mode?: 'development'|'production';
32+
/** file to bundle to */
33+
outfile?: string;
3134
/** Packages to treat as external, i.e. not to bundle */
3235
external: string[];
3336
}
3437

38+
/** lit-css transform plugin to process `.scss` files on-the-fly */
39+
export function transformSass(source: string, { filePath }: LitCSSModuleMeta): string {
40+
const result = Sass.compileString(source, {
41+
loadPaths: [dirname(filePath), NODE_MODULES],
42+
});
43+
// TODO: forward sourcemaps by returning an object
44+
return result.css;
45+
}
46+
47+
/** abs-path to root node_modules */
48+
const NODE_MODULES = fileURLToPath(new URL('../../node_modules', import.meta.url));
49+
3550
/**
3651
* Exclude SASS-only packages because there are no ts sources there
3752
*/
@@ -79,36 +94,41 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
7994
const packagePath = packageDirs.flatMap(dir =>
8095
glob.sync(`${workspace}/${dir}/package.json`, { absolute: true, cwd }));
8196

82-
const transform: Transformer = (data, { filePath }) => Sass.renderSync({
83-
data,
84-
file: filePath,
85-
includePaths: [
86-
join(options?.cwd ?? process.cwd(), 'node_modules'),
87-
],
88-
}).css.toString();
89-
9097
try {
9198
const result = await esbuild.build({
9299
entryPoints,
93100
entryNames,
94101
absWorkingDir: cwd,
95-
outdir: workspace,
96102
format: 'esm',
97103
allowOverwrite: true,
98-
bundle: true,
99-
external: ['@patternfly*', 'lit', 'tslib', ...options?.external ?? []],
100104
treeShaking: true,
101105
legalComments: 'linked',
102106
watch: Boolean(process.env.WATCH) || false,
103107
logLevel: 'info',
104108
sourcemap: true,
109+
bundle: options?.bundle ?? true,
105110

106111
minify: mode === 'production',
107112
minifyWhitespace: mode === 'production',
108113

114+
...options?.outfile ? {
115+
outfile: options.outfile
116+
} : {
117+
outdir: workspace,
118+
},
119+
120+
external: [
121+
...options?.bundle ? [] : [
122+
'@patternfly*',
123+
'lit',
124+
'tslib',
125+
],
126+
...options?.external ?? []
127+
],
128+
109129
plugins: [
110130
// import scss files as LitElement CSSResult objects
111-
litCssPlugin({ filter: /.scss$/, transform }),
131+
litCssPlugin({ filter: /.scss$/, transform: transformSass }),
112132
// replace `{{version}}` with each package's version
113133
packageVersion(),
114134
// ignore sub components bundling like "pfe-progress-steps-item"
@@ -118,6 +138,7 @@ export async function pfeBuild(options?: PfeEsbuildOptions) {
118138
],
119139
});
120140
result.stop?.();
141+
return result.outputFiles?.map(x => x.path) ?? [];
121142
} catch (error) {
122143
console.log(error);
123144
process.exit(1);

0 commit comments

Comments
 (0)