Skip to content

Commit 940cf91

Browse files
authored
Set up publishing to npm (#569)
1 parent 83b3f15 commit 940cf91

File tree

18 files changed

+2568
-607
lines changed

18 files changed

+2568
-607
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/dist
2+
/src/design-tokens/generated
3+
/storybook-static

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,27 @@ jobs:
2424
run: npm ci
2525
- name: Run Lint
2626
run: npm run check-lint
27+
28+
build:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- uses: actions/checkout@v1
32+
- name: Use Node.js 12
33+
uses: actions/setup-node@v1
34+
with:
35+
node-version: 12
36+
- name: Cache node modules
37+
uses: actions/cache@v1
38+
with:
39+
path: node_modules
40+
key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}
41+
restore-keys: |
42+
${{ runner.OS }}-build-${{ env.cache-name }}-
43+
${{ runner.OS }}-build-
44+
${{ runner.OS }}-
45+
- name: Install Dependencies
46+
run: npm ci
47+
- name: Run Preprocess
48+
run: npm run preprocess
49+
- name: Run Build
50+
run: npm run build

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist
2+
/storybook-static

.storybook/main.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ module.exports = {
4242
{
4343
loader: 'sass-loader',
4444
options: {
45-
// Dart Sass performs much better than Node Sass
45+
// Dart Sass is easier to install than Node Sass
4646
implementation: require('sass'),
4747
sassOptions: {
48-
// Import Theo design tokens as SCSS variables
49-
importer: [require('../.theo/sass-importer')],
48+
importer: [
49+
require('../glob-sass-importer'),
50+
// Import Theo design tokens as SCSS variables
51+
require('../.theo/sass-importer'),
52+
],
5053
},
5154
},
5255
},

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Publishing to npm
2+
3+
1. `git checkout master`
4+
2. `git pull`
5+
3. Make sure you have a clean working tree (`git status` should show no changes)
6+
4. `npm publish` - This will automatically install and compile everything, run linting, and publish

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ If you’re looking for the most stable version of our pattern library, check ou
1515
1. `npm ci`
1616
1. `npm run storybook`
1717

18+
## Building
19+
20+
- `npm run build` Builds CSS+JS for npm package
21+
- `npm run build-storybook` Creates a static storybook site build, for example for publishing the pattern library to Netlify
22+
1823
## Project Structure
1924

2025
```

glob-sass-importer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const glob = require('tiny-glob');
2+
const path = require('path');
3+
4+
/** @typedef {{file: string} | {contents: string} | null } ResolveResult */
5+
6+
/**
7+
* Custom glob importer
8+
* Needed to implement this because none of the sass glob importers on npm output @use
9+
* @param {string} url Raw path in source code
10+
* @param {string} currentPath Path to the current file
11+
* @param {(result: ResolveResult) => void} done Callback function for async resolvers
12+
* @returns {ResolveResult | void}
13+
*/
14+
const globImporter = (url, currentPath, done) => {
15+
// This is a really lazy check but it is easy and good enough
16+
const isGlob = url.includes('*');
17+
if (!isGlob) return null;
18+
glob(url, { cwd: path.dirname(currentPath) }).then((matchingPaths) => {
19+
// Generates a fake file that has the expanded @forward statements
20+
const contents = matchingPaths
21+
.map((filePath) => `@forward "${filePath}";`)
22+
.join('\n');
23+
done({ contents });
24+
});
25+
};
26+
27+
module.exports = globImporter;

gulpfile.js/tasks/build.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { src, dest, parallel } = require('gulp');
2+
3+
const sass = require('gulp-sass');
4+
const rename = require('gulp-rename');
5+
const postcss = require('gulp-postcss');
6+
const cssnano = require('cssnano');
7+
8+
const rollup = require('rollup');
9+
const path = require('path');
10+
const multiEntry = require('@rollup/plugin-multi-entry');
11+
const nodeResolve = require('@rollup/plugin-node-resolve');
12+
const { terser } = require('rollup-plugin-terser');
13+
14+
sass.compiler = require('sass');
15+
16+
const outDir = 'dist';
17+
18+
const buildSass = () => {
19+
return src('./src/index.scss')
20+
.pipe(
21+
sass({
22+
importer: [
23+
require('../../glob-sass-importer'),
24+
// Import Theo design tokens as SCSS variables
25+
require('../../.theo/sass-importer'),
26+
],
27+
}).on('error', sass.logError)
28+
)
29+
.pipe(rename({ basename: 'standalone' }))
30+
.pipe(dest(outDir))
31+
.pipe(postcss([cssnano()]))
32+
.pipe(rename({ extname: '.min.css' }))
33+
.pipe(dest(outDir));
34+
};
35+
36+
const buildJS = async () => {
37+
const pathName = 'cloudfour-patterns';
38+
const globalName = 'cloudfourPatterns';
39+
const bundle = await rollup.rollup({
40+
input: 'src/{objects,components}/**/*.js',
41+
plugins: [multiEntry(), nodeResolve()],
42+
});
43+
await Promise.all([
44+
bundle.write({ format: 'esm', file: path.join(outDir, `${pathName}.mjs`) }),
45+
bundle.write({
46+
format: 'umd',
47+
name: globalName,
48+
file: path.join(outDir, `${pathName}.js`),
49+
}),
50+
bundle.write({
51+
format: 'umd',
52+
name: globalName,
53+
file: path.join(outDir, `${pathName}.min.js`),
54+
plugins: [terser()],
55+
}),
56+
]);
57+
};
58+
59+
const build = parallel(buildSass, buildJS);
60+
61+
// Expose to Gulp
62+
module.exports = build;

gulpfile.js/tasks/theo-to-mdx.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { src, dest } = require('gulp');
22
const gulpTheo = require('gulp-theo');
33
// Requiring this file applies our Theo customizations to gulp-theo
44
require('../../.theo');
5+
const prettier = require('gulp-prettier');
56

67
// Gulp task
78
function theoToMdx() {
@@ -17,6 +18,7 @@ function theoToMdx() {
1718
},
1819
})
1920
)
21+
.pipe(prettier())
2022
.pipe(dest('src/design-tokens/generated'));
2123
}
2224

File renamed without changes.

0 commit comments

Comments
 (0)