(Github Package Registry)
A new ๐ก home for your packages
with the same experience.
With a great difference:
But that makes them a little more difficult to work with...
Only stored in GPR (and private)
export const getBlahMessage = () => ({ message: 'blah' })
// src/index.js
Type definitions (TS/JSDocs) for EXTRA POINTS!
> npm init
> npm i -D rollup
> npm i -D @babel/core rollup-plugin-babel
rollup-plugin-terser rimraf
export default {
input: 'src/index.js',
output: {
file: `dist/blah.mjs`,
format: 'esm',
sourcemap: true,
},
plugins: [/*...*/]
}
// rollup.config.js
Outputs 1 file
export default [
{/* ... */},
{/* ... */},
]
// rollup.config.js
Outputs 2 files
import babel from 'rollup-plugin-babel'
import { terser } from 'rollup-plugin-terser'
import pkg from './package.json'
const terserOptions = {
compress: true,
mangle: true
}
const createConfig = ({ minify, format, ext = 'js' }) => ({
input: 'src/index.js',
output: {
name: pkg.name,
file: `dist/index${minify ? '.min' : ''}.${ext}`,
format,
sourcemap: true
},
plugins: [
babel({ exclude: 'node_modules/**' }), ...(minify ? [terser(terserOptions)] : [])
]
})
export default [
{ minify: false, format: 'esm', ext: 'mjs' },
{ minify: true, format: 'esm', ext: 'mjs' },
{ minify: false, format: 'esm' },
{ minify: true, format: 'esm' }
].map(createConfig)
{
...
"scripts": {
...
"build": "rimraf dist && rollup -c"
}
...
}
> npm run build
// pacakge.json
{
...
"files": [
"dist"
],
"main": "dist/index.js",
"module": "dist/index.mjs"
...
}
// pacakge.json
Like .gitignore, but the opposite and for the "artifacts" exposed. If empty, the whole project (src, dist...) will be included (except node_modules)
๐๐ฝ
๐
Learn more at Node Resolution Algorithmย docs
or see the npm-pkg-testย (private repo) for guidance
The module one is the one that webpack will use, in case it's present.
> git init
๐๐๐ฝ
> git add .
> git commit -m "initial commit :rocket:"
> git branch -M main
> git remote add origin git@...
> git push -u origin main
{
+ "name": "@lovetoknow/npm-pkg-test",
+ "repository": "git://github.com/lovetoknow/npm-pkg-test.git",
+ "publishConfig": {
+ "registry": "https://npm.pkg.github.com/"
+ },
- "private": true
}
ย Edit package.json
*
* Name of the organization in github (in lowecase!)
Docs: Configuring npm
registry=https://npm.pkg.github.com/lovetoknow
ย Add a .npmrc file
๐๐ฝ Oh, must be lowercase!
Docs: Configuring npm
You have 2 options:
Working exampleย (private link)
Option 1
...
- run: >
printf "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
...
This way, a PAT belonging to the CI will be appended to the file, no need to create a "personal" one!
> npm publish
Option 2
๐ Tip! Pre-publish hook, if you publish from the CLI
"prepublish": "npm run build"
> cd .. > mkdir install-test > npm init -y
(We set up a test project...)
> npm install @lovetoknow/npm-pkg-test
We forgot to tell npm to look for in GPR!
registry=https://npm.pkg.github.com/lovetoknow
// .npmrc
You need to be authenticated to install it!
You have 2 options:
๐๐ฝ
Docs: Creating a token
PAT Permissions:
repo
write:packages
read:packages
delete:packages (optional)
read:repo_hook
delete_repo
Docs: About Permissions
> npm install @lovetoknow/npm-pkg-test
The "robot" ๐ค needs to be authenticated too. Depending on your needs:
- Via npm CLI
- Via .npmrc with a PAT
//npm.pkg.github.com/:_authToken=PERSONAL_ACCESS_TOKEN
// .npmrc
- Or...
...
- uses: actions/setup-node@master
with:
registry-url: >
https://npm.pkg.github.com/
...
- run: npm publish
env:
NODE_AUTH_TOKEN: >
${{ secrets.GITHUB_TOKEN }}
Credit: Daniel Young
> git add . > git commit -m 'change ...' > git push > npm version <major | minor | patch> > npm publish
๐๐ฝmanually?
๐๐ฝmanually?
Alternative workflow:
name: Publish package
on:
push:
tags:
- v.*
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
with:
node-version: 12.x
registry-url: >
https://npm.pkg.github.com/
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: >
${{ secrets.GITHUB_TOKEN }}
Credit: Daniel Young
๐๐ฝ
> npm version
- Updates package.json and package-lock.json
- Creates git tags
- And a commit
npm versioning should follow semver
MAJOR version when you make incompatible API changes,
MINOR version when you add functionality in a backwards compatible manner, and
PATCH version when you make backwards compatible bug fixes.
> npm audit
> npm audit --registry https://registry.npmjs.org/
Credit: Carlo and Josh
- Tests! (and using the dist files in your tests, not the src)
- npm hooks (pretest, prepublish/postversion)
- semver!
- Publishing a changelog file
NPM PKG TEST โ ย (private link)
Thanks for your help!
Joshua Coady
Carlo D'Ambrosio
Daniel Young
Lluis de Lasaosa
Rakhi Neigi