Skip to content

Commit 429197c

Browse files
author
James Nash
committed
feat(build-api): new, cleaner build-api
The build-api has been altered (and the internal build scripts have been updated accordingly). It now only exposes paths and filenames that get distributed by the @buildit/gravity-ui-sass NPM package. BREAKING CHANGE: The build API is no longer the package's main entry point. You need to `require('@buildit/gravity-ui-sass/build-api')` now. The available keys have also changed. re #189
1 parent da57502 commit 429197c

File tree

9 files changed

+306
-138
lines changed

9 files changed

+306
-138
lines changed

build-api.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* Provides programatic access to CSS, asset and SASS file paths
3+
* that are published by the gravity-ui-sass NPM package.
4+
*
5+
* This is so that consumers of this package can access those files
6+
* in their own build processes (e.g. to copy them to their build dir)
7+
* without needing to know the internal directory structure of this
8+
* package or having to hard-code file paths in their own build
9+
* scripts.
10+
*
11+
* @public
12+
*/
13+
14+
// Note, since this file is included in the published NPM package, it
15+
// must not export paths or filenames that are excluded from the published
16+
// package. Refer to the 'files' section in package.json to see exactly
17+
// what gets published and what does not.
18+
// Any path or file data that is required for development and builds but
19+
// is excluded from the published NPM package should be added to
20+
// gulp/paths.js instead of this file.
21+
22+
const path = require('path');
23+
const pkgManifest = require('./package.json');
24+
const bldConsts = require('./build-consts.js');
25+
26+
const pkgDir = __dirname;
27+
28+
29+
// Resolves the given path segments relative to the UI lib dist dir
30+
function distPath(...pathSegements) {
31+
return path.resolve(pkgDir, bldConsts.distDirname, bldConsts.uiLibDirname, ...pathSegements);
32+
}
33+
34+
// CSS
35+
const distCssFileBasename = 'gravity';
36+
const distCssFilename = `${distCssFileBasename}.css`;
37+
38+
const distCssDebugFileBasename = 'debug';
39+
const distCssDebugFilename = `${distCssDebugFileBasename}.css`;
40+
41+
// SVG symbols
42+
const distSvgSymbolsFilename = `${bldConsts.svgSymbolsBasename}.svg`;
43+
const distSvgSymbolsInfoFilename = `${bldConsts.svgSymbolsBasename}.json`;
44+
45+
// JS
46+
const distJsFileBasename = distCssFileBasename;
47+
const distJsFilename = `${distJsFileBasename}.js`;
48+
49+
50+
51+
// Resolves the given path segments relative to the SASS src dir
52+
function srcSassPath(...pathSegements) {
53+
return path.resolve(pkgDir, bldConsts.srcDirname, bldConsts.uiLibDirname, 'sass', ...pathSegements);
54+
}
55+
56+
// SASS
57+
const srcSassMainFilename = 'index.scss';
58+
59+
const srcSassDebugFilename = `${distCssDebugFileBasename}.scss`;
60+
61+
62+
63+
module.exports = {
64+
/**
65+
* The Gravity UI SASS version.
66+
*
67+
* @public
68+
*/
69+
version: pkgManifest.version,
70+
71+
// ==== Pre-compiled output: ====
72+
73+
/**
74+
* Takes a sequence of path segments relative to the UI library's
75+
* distributables directory and returns the absolute path.
76+
*
77+
* @param {...string} pathSegements One or more path segments
78+
* relative to the UI library's distributables directory.
79+
*
80+
* @return {string} Absolute file path to the specified
81+
* distributable directory or file.
82+
*
83+
* @public
84+
*/
85+
distPath,
86+
87+
/**
88+
* The filename of Gravity's pre-compiled CSS.
89+
*
90+
* @public
91+
*/
92+
distCssFilename,
93+
94+
/**
95+
* The filename of Gravity's pre-compiled debug CSS.
96+
*
97+
* @public
98+
*/
99+
distCssDebugFilename,
100+
101+
/**
102+
* The filename of Gravity's SVG symbols file.
103+
*
104+
* @public
105+
*/
106+
distSvgSymbolsFilename,
107+
108+
/**
109+
* The filename of Gravity's SVG symbols metadata JSON file.
110+
*
111+
* @public
112+
*/
113+
distSvgSymbolsInfoFilename,
114+
115+
/**
116+
* The filename of Gravity's client-side JS file.
117+
*
118+
* @public
119+
*/
120+
distJsFilename,
121+
122+
123+
124+
// ==== Published source files: ====
125+
126+
/**
127+
* Takes a sequence of path segments relative to the UI library's
128+
* SASS source directory and returns the absolute path.
129+
*
130+
* @param {...string} pathSegements One or more path segments
131+
* relative to the UI library's SASS source directory.
132+
*
133+
* @return {string} Absolute file path to the specified source
134+
* directory or file.
135+
*
136+
* @public
137+
*/
138+
srcSassPath,
139+
140+
/**
141+
* The filename of Gravity's main SASS source file.
142+
*
143+
* @public
144+
*/
145+
srcSassMainFilename,
146+
147+
/**
148+
* The filename of Gravity's debug SASS source file.
149+
*
150+
* @public
151+
*/
152+
srcSassDebugFilename,
153+
};

build-consts.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Shared constants used by public build-api and private build scripts.
3+
*
4+
* Although this is shipped as part of the NPM package, it is considered an
5+
* internal ("private") API and is therefore not monitored for breaking changes.
6+
*
7+
* External consumers MUST NOT include this module.
8+
*
9+
* @private
10+
*/
11+
12+
module.exports = {
13+
/**
14+
* Name of root source directory.
15+
*/
16+
srcDirname: 'src',
17+
18+
/**
19+
* Name of root distributables directory.
20+
*/
21+
distDirname: 'dist',
22+
23+
/**
24+
* Name of sub-directories containing UI library assets.
25+
*/
26+
uiLibDirname: 'ui-lib',
27+
28+
/**
29+
* Name of sub-directories containing pattern library assets.
30+
*/
31+
patternLibDirname: 'styleguide',
32+
33+
34+
/**
35+
* Base filename used by various SVG symbols files.
36+
*/
37+
svgSymbolsBasename: 'symbols',
38+
};

eyeglass-exports.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { srcSassDir } = require('./index');
1+
const { srcSassPath } = require('./build-api.js');
22

33
module.exports = function(eyeglass, sass) {
44
return {
5-
sassDir: srcSassDir,
5+
sassDir: srcSassPath(),
66
};
77
};

gulp/paths.js

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,87 @@
11
/**
22
* Export full set of file paths used by the build.
33
*
4-
* Exports everything from index.js, which only exposes paths and files
4+
* Exports everything from build-api.js, which only exposes paths and files
55
* that are included in the published NPM package, and adds additional
66
* paths and files that are only in the source repo but required for
77
* builds.
88
*/
99
const path = require('path');
10-
const pkgPaths = require('../index.js');
10+
const patternLabConfig = require('../patternlab-config.json');
11+
const bldConsts = require('../build-consts.js');
12+
13+
const gulpDir = __dirname;
14+
15+
// Resolves the given path segments relative to the package root dir
16+
function pkgRootPath(...pathSegements) {
17+
return path.resolve(gulpDir, '..', ...pathSegements);
18+
}
19+
20+
21+
// ==== UI library ====
22+
23+
function srcUiLibPath(...pathSegements) {
24+
return pkgRootPath(bldConsts.srcDirname, bldConsts.uiLibDirname, ...pathSegements)
25+
}
26+
27+
28+
// ==== Pattern library ====
29+
30+
// Resolves the given path segments relative to Pattern Lab's patterns source dir
31+
function srcPatternsPath(...pathSegments) {
32+
return pkgRootPath(patternLabConfig.paths.source.patterns, ...pathSegments);
33+
}
34+
35+
// Resolves the given path segments relative to the SASS src dir
36+
function distPatternLibPath(...pathSegements) {
37+
return pkgRootPath(bldConsts.distDirname, bldConsts.patternLibDirname, ...pathSegements);
38+
}
1139

12-
const srcImgDir = path.join(pkgPaths.srcUiLibDir, 'images');
13-
const srcSymbolsDir = path.join(pkgPaths.srcUiLibDir, 'svg-symbols');
14-
const srcJsDir = path.join(pkgPaths.srcUiLibDir, 'js');
1540

1641
module.exports = {
17-
srcImgDir,
18-
srcSymbolsDir,
19-
srcJsDir,
42+
/**
43+
* Takes a sequence of path segments relative to the package's
44+
* root directory and returns the absolute path.
45+
*
46+
* @param {...string} pathSegements One or more path segments
47+
* relative to the package's root directory.
48+
*
49+
* @return {string} Absolute file path to the specified package
50+
* directory or file.
51+
*/
52+
pkgRootPath,
53+
54+
/**
55+
* Takes a sequence of path segments relative to the pattern
56+
* library's source patterns directory and returns the absolute path.
57+
*
58+
* @param {...string} pathSegements One or more path segments
59+
* relative to the pattern library's source patterns directory.
60+
*
61+
* @return {string} Absolute file path to the specified source
62+
* directory or file.
63+
*/
64+
srcPatternsPath,
65+
66+
/**
67+
* Takes a sequence of path segments relative to the pattern
68+
* library's distributables directory and returns the absolute path.
69+
*
70+
* @param {...string} pathSegements One or more path segments
71+
* relative to the pattern library's distributables directory.
72+
*
73+
* @return {string} Absolute file path to the specified distributable
74+
* directory or file.
75+
*/
76+
distPatternLibPath,
77+
78+
/**
79+
* The absolute path to SVG symbols source directory.
80+
*/
81+
srcSvgSymbolsPath: (...pathSegments) => srcUiLibPath('svg-symbols', ...pathSegments),
2082

21-
...pkgPaths
83+
/**
84+
* The absolute path to the JS source directory.
85+
*/
86+
srcJsPath: (...pathSegments) => srcUiLibPath('js', ...pathSegments),
2287
}

gulp/patternlab.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const eyeglass = require('eyeglass');
1414
const rename = require("gulp-rename");
1515
const path = require('path');
1616
const argv = require('minimist')(process.argv.slice(2));
17+
18+
const uiLibPaths = require('../build-api.js');
1719
const pkgPaths = require('./paths.js');
1820

1921
const taskNamePrefix = 'patternlab:';
@@ -22,9 +24,8 @@ const taskNamePrefix = 'patternlab:';
2224
const config = require('../patternlab-config.json');
2325
const patternlab = require('@pattern-lab/core')(config);
2426

25-
function paths() {
26-
return config.paths;
27-
}
27+
const plPaths = config.paths;
28+
2829

2930
/******************************************************
3031
* PRE-BUILD TASKS
@@ -35,29 +36,29 @@ function paths() {
3536
// Exported array of glob patterns that clean tasks can use to
3637
// delete any files generated at build-time
3738
const generatedFileGlobs = [
38-
path.join(__dirname, '..', 'dependencyGraph.json'),
39-
pkgPaths.normalizePath(paths().source.root, '_meta', '*.mustache')
39+
pkgPaths.pkgRootPath('dependencyGraph.json'),
40+
pkgPaths.pkgRootPath(plPaths.source.meta, '*.mustache')
4041
];
4142

42-
const generatedPatternsDir = pkgPaths.normalizePath(paths().source.patterns, '_generated');
43-
generatedFileGlobs.push(path.join(generatedPatternsDir, '*'));
44-
generatedFileGlobs.push('!' + path.join(generatedPatternsDir, 'README.md')); // Prevent README.md from being deleted
43+
const generatedPatternsDirname = '_generated';
44+
generatedFileGlobs.push(pkgPaths.srcPatternsPath(generatedPatternsDirname, '*'));
45+
generatedFileGlobs.push(`!${pkgPaths.srcPatternsPath(generatedPatternsDirname, 'README.md')}`); // Prevent README.md from being deleted
4546

4647
function preSvgSymbolsTask () {
47-
return gulp.src(pkgPaths.bldSvgSymbolsFilePath)
48+
return gulp.src(uiLibPaths.distPath(uiLibPaths.distSvgSymbolsFilename))
4849
.pipe(rename('symbols.njk'))
49-
.pipe(gulp.dest(generatedPatternsDir));
50+
.pipe(gulp.dest(pkgPaths.srcPatternsPath(generatedPatternsDirname)));
5051
};
5152
preSvgSymbolsTask.displayName = taskNamePrefix + 'pre:symbols';
5253
preSvgSymbolsTask.description = 'Copies Gravity\'s symbols.svg file to the patterns folder.';
5354

5455

5556
const generatedSymbolInfoFilename = '00-svg-symbols.json';
56-
const generatedSymbolInfoDir = pkgPaths.normalizePath(paths().source.patterns, '00-particles', '05-logos-and-icons');
57+
const generatedSymbolInfoDir = pkgPaths.srcPatternsPath('00-particles', '05-logos-and-icons');
5758
generatedFileGlobs.push(path.join(generatedSymbolInfoDir, generatedSymbolInfoFilename));
5859

5960
function preSvgSymbolsInfoTask () {
60-
return gulp.src(pkgPaths.bldSvgSymbolsInfoFilePath)
61+
return gulp.src(uiLibPaths.distPath(uiLibPaths.distSvgSymbolsInfoFilename))
6162
.pipe(rename(generatedSymbolInfoFilename))
6263
.pipe(gulp.dest(generatedSymbolInfoDir));
6364
};
@@ -72,9 +73,9 @@ const preBuildTask = gulp.parallel(preSvgSymbolsTask, preSvgSymbolsInfoTask);
7273
******************************************************/
7374

7475
function plSassTask() {
75-
return gulp.src(pkgPaths.normalizePath(paths().source.root, 'sass', 'pattern-scaffolding.scss'))
76+
return gulp.src(pkgPaths.pkgRootPath(plPaths.source.root, 'sass', 'pattern-scaffolding.scss'))
7677
.pipe(sass(eyeglass(sass.sync().on('error', sass.logError))))
77-
.pipe(gulp.dest(pkgPaths.normalizePath(paths().public.css)))
78+
.pipe(gulp.dest(pkgPaths.pkgRootPath(plPaths.public.css)))
7879
};
7980
plSassTask.displayName = taskNamePrefix + 'sass';
8081
plSassTask.description = 'Compiles pattern library CSS files from source SASS.';
@@ -158,7 +159,7 @@ plBuildTask.description = 'Compiles the patterns and frontend, outputting to con
158159

159160
function plWatchSassTask() {
160161
gulp.watch(
161-
pkgPaths.normalizePath(paths().source.root, 'sass', '**', '*.scss'),
162+
pkgPaths.pkgRootPath(plPaths.source.root, 'sass', '**', '*.scss'),
162163
gulp.series(plSassTask, patternlab.server.refreshCSS)
163164
);
164165
}

0 commit comments

Comments
 (0)