From 6f1a749b94691642d50472113d58e770268384fd Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 6 Mar 2021 12:30:45 +0000 Subject: [PATCH 1/6] feat: add initial support for using mkdist --- package.json | 1 + src/core/build/index.ts | 17 ++++++++++- src/core/package/index.ts | 35 +++++++++++++++++++++-- src/core/package/utils.ts | 15 +++++++++- yarn.lock | 60 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 120 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 1020bb6..d77dfd9 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,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", diff --git a/src/core/build/index.ts b/src/core/build/index.ts index 7e00b78..6b30b19 100644 --- a/src/core/build/index.ts +++ b/src/core/build/index.ts @@ -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' @@ -70,6 +71,20 @@ 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(`Copying ./${srcDir} => ${distDir} with \`mkdist\`.`) + mkdist({ + srcDir, + distDir: distDir.slice(2), + rootDir: pkg.options.rootDir, + }) + }) + if (shouldWatch) { // Watch const watcher = watch(rollupConfig) diff --git a/src/core/package/index.ts b/src/core/package/index.ts index 1498ec0..2c13ca2 100644 --- a/src/core/package/index.ts +++ b/src/core/package/index.ts @@ -10,6 +10,7 @@ import { writeFile, mkdirp, chmod, + pathExistsSync, } from 'fs-extra' import type { RollupOptions } from 'rollup' import sortPackageJson from 'sort-package-json' @@ -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 { /** @@ -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) @@ -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 */ diff --git a/src/core/package/utils.ts b/src/core/package/utils.ts index 0cf9ff9..4ed1e99 100644 --- a/src/core/package/utils.ts +++ b/src/core/package/utils.ts @@ -1,4 +1,4 @@ -import { basename, sep } from 'upath' +import { sep } from 'upath' import type { PackageJson } from './types' const walkDownDirectory = (pathname: string) => { @@ -6,6 +6,19 @@ const walkDownDirectory = (pathname: string) => { return rest.join(sep) } +export const getEntrypointPaths = (path: string) => { + if (path.startsWith('./')) path = path.slice(2) + + const filenames = new Set() + 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) diff --git a/yarn.lock b/yarn.lock index e9bda1d..01f2716 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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: @@ -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: @@ -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: @@ -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" @@ -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: @@ -7146,6 +7162,24 @@ __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" @@ -7153,6 +7187,13 @@ __metadata: 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" @@ -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 @@ -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" From 9d1664c0136c7166df9950308ac201d3e3e5e701 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Mar 2021 12:33:13 +0000 Subject: [PATCH 2/6] chore: add exports to `package.json` --- package.json | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 1020bb6..6bd9b23 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,15 @@ ], "repository": "nuxt-contrib/siroc", "license": "MIT", - "sideEffects": false, - "main": "dist/index.js", - "module": "dist/index.es.js", - "types": "dist/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.es.js" + } + }, + "main": "./dist/index.js", + "module": "./dist/index.es.js", + "types": "./dist/index.d.ts", "bin": { "siroc": "bin/cli.js", "siroc-runner": "bin/runtime.js" From f5ed32d22954fe95f77d14469e13ebecb4599706 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Mar 2021 12:48:02 +0000 Subject: [PATCH 3/6] chore!: add explicit exports BREAKING CHANGE: this currently includes all published files but changes to `exports` can potentially cause import issues --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6bd9b23..e961542 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ ".": { "require": "./dist/index.js", "import": "./dist/index.es.js" - } + }, + "./package.json": "./package.json" }, "main": "./dist/index.js", "module": "./dist/index.es.js", From 6579dc51e642666dd5e51d6c47acdc16d9305cc7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Mar 2021 13:12:21 +0000 Subject: [PATCH 4/6] docs: add info on mkdist --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index e88a5ad..0d80359 100644 --- a/README.md +++ b/README.md @@ -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 +```json +{ + "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 From fecbb84b47b8f06a11d2269b77906db661df37c5 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Mar 2021 13:21:15 +0000 Subject: [PATCH 5/6] docs: use json5 for better code syntax highlighting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d80359..f74c922 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ There are some conventions in place of configuration that are worth noting: * 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 -```json +```json5 { "exports": { ".": { From 3cf1e92e7f125b83d4fe032e0caf37b657d78eb7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 8 Mar 2021 13:23:13 +0000 Subject: [PATCH 6/6] fix: update transpile message --- src/core/build/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/build/index.ts b/src/core/build/index.ts index 6b30b19..5210c73 100644 --- a/src/core/build/index.ts +++ b/src/core/build/index.ts @@ -77,7 +77,9 @@ export const build = async ( if (!sourceFolder) return const srcDir = relative(pkg.options.rootDir, sourceFolder) - pkg.logger.info(`Copying ./${srcDir} => ${distDir} with \`mkdist\`.`) + pkg.logger.info( + `Transpiling \`./${srcDir}\` to \`${distDir}\` with ${bold('mkdist')}.` + ) mkdist({ srcDir, distDir: distDir.slice(2),