Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial support for using mkdist #208

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ At the most basic level, your entrypoints are configured in your `package.json`:
There are some conventions in place of configuration that are worth noting:
* the file type is inferred from the file name if possible (e.g. `babel.es.js` will be in 'es' format)
* `main` defaults to CJS, `module` to ES, `browser` to UMD, and `bin` to CJS
* if you have a folder mapped using [subpath patterns](https://nodejs.org/api/packages.html#packages_subpath_patterns) and it matches a folder within your `src` folder, the files within will be copied across and lightly transpiled using [mkdist](https://github.com/unjsio/mkdist).

##### Example
```json5
{
"exports": {
".": {
// This will be compiled in CJS and matched to src/index.ts
"require": "./dist/index.js",
// This will be compiled in ES and matched to src/index.ts
"import": "./dist/index.es.js"
},
// src/templates will be lightly transpiled with mkdist and copied to dist/templates
"./templates/*": "./dist/templates/*",
// siroc will not touch this
"./package.json": "./package.json"
},
// This will be compiled in CJS and matched to src/index.ts
"main": "./dist/index.js",
// This will be compiled in ES and matched to src/index.ts
"module": "./dist/index.es.js",
// Types will be generated for src/index.ts
"types": "./dist/index.d.ts",
"bin": {
// This will be compiled in CJS and matched to src/cli/index.ts
"siroc": "bin/cli.js",
// This will be compiled in CJS and matched to src/cli/runtime.ts
"siroc-runner": "bin/runtime.js"
}
}
```

#### Build hooks

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
"jiti": "^1.6.2",
"mkdist": "^0.1.1",
"rollup": "^2.40.0",
"rollup-plugin-dts": "^2.0.1",
"rollup-plugin-esbuild": "2.6.1",
Expand Down
19 changes: 18 additions & 1 deletion src/core/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dirname, join } from 'upath'
import { dirname, join, relative } from 'upath'

import { bold, gray, green } from 'chalk'
import { remove, stat } from 'fs-extra'
import { mkdist } from 'mkdist'
import { rollup, watch, RollupError } from 'rollup'

import type { BuildOptions, Package } from '../package'
Expand Down Expand Up @@ -70,6 +71,22 @@ export const build = async (

logRollupConfig(pkg, rollupConfig)

await runInParallel(pkg.folderExports, folder => {
const { 1: distDir = folder } = folder.match(/^(.*[^*/])[*/]*$/) || []
const sourceFolder = pkg.resolveEntrypointFolder(distDir)
if (!sourceFolder) return

const srcDir = relative(pkg.options.rootDir, sourceFolder)
pkg.logger.info(
`Transpiling \`./${srcDir}\` to \`${distDir}\` with ${bold('mkdist')}.`
)
mkdist({
srcDir,
distDir: distDir.slice(2),
rootDir: pkg.options.rootDir,
})
})

if (shouldWatch) {
// Watch
const watcher = watch(rollupConfig)
Expand Down
35 changes: 33 additions & 2 deletions src/core/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
writeFile,
mkdirp,
chmod,
pathExistsSync,
} from 'fs-extra'
import type { RollupOptions } from 'rollup'
import sortPackageJson from 'sort-package-json'
Expand All @@ -30,7 +31,11 @@ import {
RequireProperties,
} from '../utils'
import type { PackageJson } from './types'
import { getEntrypointFilenames, getFlatValues } from './utils'
import {
getEntrypointFilenames,
getEntrypointPaths,
getFlatValues,
} from './utils'

export interface DefaultPackageOptions {
/**
Expand Down Expand Up @@ -396,7 +401,19 @@ export class Package {
`./${this.pkg.main}`,
`./${this.pkg.module}`,
`./${this.pkg.browser}`,
].includes(item) && !item.match(/[./]$/)
].includes(item) && !/[./*]$/.test(item)

if (!exports) return []
if (typeof exports === 'string') return [exports].filter(filterExports)
if (Array.isArray(exports)) return exports.filter(filterExports)

return getFlatValues(exports).filter(filterExports)
}

get folderExports(): string[] {
const { exports } = this.pkg

const filterExports = (item: string) => /[a-z]\/\*$/.test(item)

if (!exports) return []
if (typeof exports === 'string') return [exports].filter(filterExports)
Expand Down Expand Up @@ -445,6 +462,20 @@ export class Package {
return input
}

resolveEntrypointFolder(path = this.pkg.main) {
if (!path) return undefined

let input!: string
const paths = getEntrypointPaths(path)

paths.some(path => {
input = this.resolvePath('src', path)
return pathExistsSync(input)
})

return input
}

parsePerson(person: string) {
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down
15 changes: 14 additions & 1 deletion src/core/package/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { basename, sep } from 'upath'
import { sep } from 'upath'
import type { PackageJson } from './types'

const walkDownDirectory = (pathname: string) => {
const [first, ...rest] = pathname.split(sep)
return rest.join(sep)
}

export const getEntrypointPaths = (path: string) => {
if (path.startsWith('./')) path = path.slice(2)

const filenames = new Set<string>()
let cwd = path
do {
filenames.add(cwd)
cwd = walkDownDirectory(cwd)
} while (cwd)

return Array.from(filenames)
}

export const getEntrypointFilenames = (path: string) => {
if (path.startsWith('./')) path = path.slice(2)

Expand Down
60 changes: 56 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3661,6 +3661,13 @@ __metadata:
languageName: node
linkType: hard

"de-indent@npm:^1.0.2":
version: 1.0.2
resolution: "de-indent@npm:1.0.2"
checksum: a1933a4328d053d9b0db447668521a6d0360b509c115dc5340420fd645be556c00b82e491d6b862249981ed22fbf2016080b222ad23c25038aba72cb8e3120ea
languageName: node
linkType: hard

"debug@npm:4, debug@npm:4.3.1, debug@npm:^4.0.1, debug@npm:^4.1.0, debug@npm:^4.1.1, debug@npm:^4.2.0":
version: 4.3.1
resolution: "debug@npm:4.3.1"
Expand Down Expand Up @@ -4000,7 +4007,7 @@ __metadata:
languageName: node
linkType: hard

"esbuild@npm:^0.8.56":
"esbuild@npm:^0.8.32, esbuild@npm:^0.8.56":
version: 0.8.56
resolution: "esbuild@npm:0.8.56"
bin:
Expand Down Expand Up @@ -4655,7 +4662,7 @@ __metadata:
languageName: node
linkType: hard

"fs-extra@npm:^9.1.0":
"fs-extra@npm:^9.0.1, fs-extra@npm:^9.1.0":
version: 9.1.0
resolution: "fs-extra@npm:9.1.0"
dependencies:
Expand Down Expand Up @@ -4967,7 +4974,7 @@ __metadata:
languageName: node
linkType: hard

"globby@npm:11.0.2, globby@npm:^11.0.1":
"globby@npm:11.0.2, globby@npm:^11.0.1, globby@npm:^11.0.2":
version: 11.0.2
resolution: "globby@npm:11.0.2"
dependencies:
Expand Down Expand Up @@ -5158,6 +5165,15 @@ __metadata:
languageName: node
linkType: hard

"he@npm:^1.1.0":
version: 1.2.0
resolution: "he@npm:1.2.0"
bin:
he: bin/he
checksum: 212122003c20c8c17ac0c83a419b4c8e835411ff6ab9195d053ea6e4a0597cc005b5b8eabcbd57b0b0c0fe676f0049e09315845fff4e051198845491cbba260e
languageName: node
linkType: hard

"hosted-git-info@npm:^2.1.4":
version: 2.8.8
resolution: "hosted-git-info@npm:2.8.8"
Expand Down Expand Up @@ -6374,7 +6390,7 @@ __metadata:
languageName: node
linkType: hard

"jiti@npm:^1.6.2":
"jiti@npm:^1.2.0, jiti@npm:^1.6.2":
version: 1.6.2
resolution: "jiti@npm:1.6.2"
bin:
Expand Down Expand Up @@ -7146,13 +7162,38 @@ __metadata:
languageName: node
linkType: hard

"mkdist@npm:^0.1.1":
version: 0.1.1
resolution: "mkdist@npm:0.1.1"
dependencies:
defu: ^3.2.2
esbuild: ^0.8.32
fs-extra: ^9.0.1
globby: ^11.0.2
jiti: ^1.2.0
mri: ^1.1.6
upath: ^2.0.1
vue-template-compiler: ^2.6.12
bin:
mkdist: dist/cli.js
checksum: dae3891b9da0036bbdd7f2343ab8389e81374c06095eac9d7b1fe70d79586e51f0a42282f583605931ca5973e749c5982cd77a6583a4e1eeda32c098d83b0776
languageName: node
linkType: hard

"modify-values@npm:^1.0.0":
version: 1.0.1
resolution: "modify-values@npm:1.0.1"
checksum: 55165ae8b4ea2aafebe5027dd427d4a833d54606c81546f4d3c04943d99d194ac9481fa076719f326d243c475e2dfa5cf0219e68cffbbf9c44b24e46eb889779
languageName: node
linkType: hard

"mri@npm:^1.1.6":
version: 1.1.6
resolution: "mri@npm:1.1.6"
checksum: 87a09465063063da6d69d98ac75a6206a597c7533561c74e8aab5e6490ebe2a0e94c583fb6e02429e1f4291286a9549eb6a9699c83f5bad252c2c9b4eada97da
languageName: node
linkType: hard

"ms@npm:2.0.0":
version: 2.0.0
resolution: "ms@npm:2.0.0"
Expand Down Expand Up @@ -8880,6 +8921,7 @@ __metadata:
jest: ^26.6.3
jiti: ^1.6.2
lint-staged: ^10.5.4
mkdist: ^0.1.1
prettier: ^2.2.1
release-it: 14.4.1
rollup: ^2.40.0
Expand Down Expand Up @@ -10053,6 +10095,16 @@ typescript@^4.2.3:
languageName: node
linkType: hard

"vue-template-compiler@npm:^2.6.12":
version: 2.6.12
resolution: "vue-template-compiler@npm:2.6.12"
dependencies:
de-indent: ^1.0.2
he: ^1.1.0
checksum: 5b05dda398cc1df7ba244316ddfb8f6463dbaafb4d23a4ac7e1cb8914e813a464f4ac055e41ac6e60feeadd4cccdeec1111fac592f2bb4eeb320889846a18912
languageName: node
linkType: hard

"w3c-hr-time@npm:^1.0.2":
version: 1.0.2
resolution: "w3c-hr-time@npm:1.0.2"
Expand Down