jiek 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/bin/jiek.js +13 -0
  4. package/dist/cli.cjs +5041 -0
  5. package/dist/cli.d.cts +112 -0
  6. package/dist/cli.d.ts +112 -0
  7. package/dist/cli.js +5010 -0
  8. package/dist/cli.min.cjs +19 -0
  9. package/dist/cli.min.js +19 -0
  10. package/dist/index.cjs +5 -0
  11. package/dist/index.d.cts +73 -0
  12. package/dist/index.d.ts +73 -0
  13. package/dist/index.js +3 -0
  14. package/dist/index.min.cjs +1 -0
  15. package/dist/index.min.js +1 -0
  16. package/dist/rollup/index.cjs +4688 -0
  17. package/dist/rollup/index.d.cts +53 -0
  18. package/dist/rollup/index.d.ts +53 -0
  19. package/dist/rollup/index.js +4673 -0
  20. package/dist/rollup/index.min.cjs +19 -0
  21. package/dist/rollup/index.min.js +19 -0
  22. package/package.json +89 -4
  23. package/src/cli.ts +9 -0
  24. package/src/commands/base.ts +8 -0
  25. package/src/commands/build.ts +158 -0
  26. package/src/commands/init.ts +373 -0
  27. package/src/commands/publish.ts +170 -0
  28. package/src/index.ts +8 -0
  29. package/src/inner.ts +11 -0
  30. package/src/merge-package-json.ts +75 -0
  31. package/src/rollup/base.ts +72 -0
  32. package/src/rollup/index.ts +422 -0
  33. package/src/rollup/plugins/globals.ts +34 -0
  34. package/src/rollup/plugins/progress.ts +26 -0
  35. package/src/rollup/plugins/skip.ts +23 -0
  36. package/src/rollup/utils/commonOptions.ts +9 -0
  37. package/src/rollup/utils/externalResolver.ts +21 -0
  38. package/src/rollup/utils/globalResolver.ts +13 -0
  39. package/src/rollup/utils/withMinify.ts +18 -0
  40. package/src/utils/filterSupport.ts +84 -0
  41. package/src/utils/getExports.ts +104 -0
  42. package/src/utils/getRoot.ts +16 -0
  43. package/src/utils/getWD.ts +31 -0
  44. package/src/utils/loadConfig.ts +93 -0
  45. package/src/utils/tsRegister.ts +26 -0
  46. package/index.js +0 -1
@@ -0,0 +1,104 @@
1
+ import { relative, resolve } from 'node:path'
2
+
3
+ import {
4
+ DEFAULT_SKIP_VALUES,
5
+ entrypoints2Exports,
6
+ type Entrypoints2ExportsOptions,
7
+ filterLeafs,
8
+ type RecursiveRecord,
9
+ resolveEntrypoints
10
+ } from '@jiek/pkger/entrypoints'
11
+ import type { Config } from 'jiek'
12
+ import { isMatch } from 'micromatch'
13
+
14
+ const intersection = <T>(a: Set<T>, b: Set<T>) => new Set([...a].filter(i => b.has(i)))
15
+
16
+ export function getExports({
17
+ entrypoints,
18
+ pkgIsModule,
19
+ entries,
20
+ config,
21
+ dir,
22
+ noFilter,
23
+ isPublish
24
+ }: {
25
+ entrypoints: string | string[] | Record<string, unknown>
26
+ pkgIsModule: boolean
27
+ entries?: string[]
28
+ config?: Config
29
+ dir?: string
30
+ noFilter?: boolean
31
+ isPublish?: boolean
32
+ }) {
33
+ const dirResolve = (...paths: string[]) => resolve(dir ?? process.cwd(), ...paths)
34
+ const dirRelative = (path: string) => relative(dir ?? process.cwd(), path)
35
+ const {
36
+ build = {},
37
+ publish: {
38
+ withSuffix = false,
39
+ withSource = true
40
+ } = {}
41
+ } = config ?? {}
42
+ const {
43
+ crossModuleConvertor = true
44
+ } = build
45
+ const jsOutdir = `./${
46
+ dirRelative(dirResolve(
47
+ (
48
+ typeof build?.output?.dir === 'object'
49
+ // the outdir only affect js output in this function
50
+ ? build.output.dir.js
51
+ : build?.output?.dir
52
+ ) ?? 'dist'
53
+ ))
54
+ }`
55
+ const [, resolvedEntrypoints] = resolveEntrypoints(entrypoints)
56
+ if (entries) {
57
+ Object
58
+ .entries(resolvedEntrypoints)
59
+ .forEach(([key]) => {
60
+ if (!entries.some(e => isMatch(key, e, { matchBase: true }))) {
61
+ delete resolvedEntrypoints[key]
62
+ }
63
+ })
64
+ }
65
+ const filteredResolvedEntrypoints = noFilter ? resolvedEntrypoints : filterLeafs(
66
+ resolvedEntrypoints as RecursiveRecord<string>,
67
+ {
68
+ skipValue: [
69
+ // ignore values that filename starts with `.jk-noentry`
70
+ /(^|\/)\.jk-noentry/,
71
+ ...DEFAULT_SKIP_VALUES
72
+ ]
73
+ }
74
+ )
75
+ const crossModuleWithConditional: Entrypoints2ExportsOptions['withConditional'] = crossModuleConvertor
76
+ ? {
77
+ import: opts =>
78
+ !pkgIsModule && intersection(
79
+ new Set(opts.conditionals),
80
+ new Set(['import', 'module'])
81
+ ).size === 0
82
+ ? opts.dist.replace(/\.js$/, '.mjs')
83
+ : false,
84
+ require: opts =>
85
+ pkgIsModule && intersection(
86
+ new Set(opts.conditionals),
87
+ new Set(['require', 'node'])
88
+ ).size === 0
89
+ ? opts.dist.replace(/\.js$/, '.cjs')
90
+ : false
91
+ }
92
+ : {}
93
+ return [
94
+ filteredResolvedEntrypoints,
95
+ entrypoints2Exports(filteredResolvedEntrypoints, {
96
+ outdir: jsOutdir,
97
+ withSuffix: isPublish ? withSuffix : undefined,
98
+ withSource: isPublish ? withSource : undefined,
99
+ withConditional: {
100
+ ...crossModuleWithConditional
101
+ }
102
+ })
103
+ ] as const
104
+ }
@@ -0,0 +1,16 @@
1
+ import path from 'node:path'
2
+
3
+ import { program } from 'commander'
4
+
5
+ let root: string
6
+ export function getRoot() {
7
+ if (root) return root
8
+
9
+ const rootOption = program.getOptionValue('root')
10
+ root = rootOption
11
+ ? path.isAbsolute(rootOption)
12
+ ? rootOption
13
+ : path.resolve(process.cwd(), rootOption)
14
+ : undefined
15
+ return root
16
+ }
@@ -0,0 +1,31 @@
1
+ import { getWorkspaceDir, isWorkspaceDir } from '@jiek/utils/getWorkspaceDir'
2
+
3
+ import { type } from './filterSupport'
4
+ import { getRoot } from './getRoot'
5
+
6
+ let wd: string
7
+ let notWorkspace = false
8
+
9
+ export function getWD() {
10
+ if (wd) return { wd, notWorkspace }
11
+
12
+ const root = getRoot()
13
+ if (root !== undefined) {
14
+ const isWorkspace = isWorkspaceDir(root, type)
15
+ notWorkspace = !isWorkspace
16
+ wd = root
17
+ return { wd, notWorkspace }
18
+ }
19
+ try {
20
+ wd = getWorkspaceDir(type)
21
+ } catch (e) {
22
+ // @ts-ignore
23
+ if ('message' in e && e.message === 'workspace root not found') {
24
+ wd = root
25
+ notWorkspace = true
26
+ } else {
27
+ throw e
28
+ }
29
+ }
30
+ return { wd, notWorkspace }
31
+ }
@@ -0,0 +1,93 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+
4
+ import { program } from 'commander'
5
+ import type { Config } from 'jiek'
6
+ import { load } from 'js-yaml'
7
+
8
+ import { getWD } from './getWD'
9
+ import { tsRegisterName } from './tsRegister'
10
+
11
+ let configName = 'jiek.config'
12
+
13
+ function getConfigPath(root: string, dir?: string) {
14
+ const isSupportTsLoader = !!tsRegisterName
15
+ function configWithExtIsExist(ext: string) {
16
+ const filenames = [
17
+ path.resolve(process.cwd(), `${configName}.${ext}`),
18
+ path.resolve(process.cwd(), `.${configName}.${ext}`),
19
+ path.resolve(root, `${configName}.${ext}`),
20
+ path.resolve(root, `.${configName}.${ext}`)
21
+ ]
22
+ if (dir) {
23
+ filenames.unshift(...[
24
+ path.resolve(dir, `${configName}.${ext}`),
25
+ path.resolve(dir, `.${configName}.${ext}`)
26
+ ])
27
+ }
28
+ for (const filename of filenames) {
29
+ if (
30
+ fs.existsSync(filename)
31
+ && fs.lstatSync(filename)
32
+ .isFile()
33
+ ) {
34
+ return filename
35
+ }
36
+ }
37
+ return
38
+ }
39
+ configName = configWithExtIsExist('js') ?? configName
40
+ configName = configWithExtIsExist('json') ?? configName
41
+ configName = configWithExtIsExist('yaml') ?? configName
42
+ if (isSupportTsLoader) {
43
+ configName = configWithExtIsExist('ts') ?? configName
44
+ }
45
+ return path.resolve(root, configName)
46
+ }
47
+
48
+ export function loadConfig(dir?: string): Config {
49
+ const { wd: root } = getWD()
50
+
51
+ let configPath = program.getOptionValue('configPath')
52
+
53
+ if (!configPath) {
54
+ configPath = getConfigPath(root, dir)
55
+ } else {
56
+ if (!fs.existsSync(configPath)) {
57
+ throw new Error(`config file not found: ${configPath}`)
58
+ }
59
+ if (!path.isAbsolute(configPath)) {
60
+ configPath = path.resolve(root, configPath)
61
+ }
62
+ }
63
+ const ext = path.extname(configPath)
64
+
65
+ let module: any
66
+ switch (ext) {
67
+ case '.js':
68
+ module = require(configPath)
69
+ break
70
+ case '.json':
71
+ return require(configPath)
72
+ case '.yaml':
73
+ return load(fs.readFileSync(configPath, 'utf-8')) as Config
74
+ case '.ts':
75
+ if (tsRegisterName) {
76
+ require(tsRegisterName)
77
+ module = require(configPath)
78
+ break
79
+ }
80
+ throw new Error(
81
+ 'ts config file is not supported without ts register, '
82
+ + 'please install esbuild-register or set JIEK_TS_REGISTER env for custom ts register'
83
+ )
84
+ case '.config':
85
+ module = {}
86
+ break
87
+ default:
88
+ throw new Error(`unsupported config file type: ${ext}`)
89
+ }
90
+ if (!module) throw new Error('config file is empty')
91
+
92
+ return module.default ?? module
93
+ }
@@ -0,0 +1,26 @@
1
+ import { createRequire } from 'node:module'
2
+
3
+ const require = createRequire(import.meta.url)
4
+
5
+ function packageIsExist(name: string) {
6
+ try {
7
+ require.resolve(name)
8
+ return true
9
+ } catch (e) {
10
+ return false
11
+ }
12
+ }
13
+
14
+ export let tsRegisterName: string | undefined
15
+ const registers = [
16
+ process.env.JIEK_TS_REGISTER,
17
+ 'esbuild-register',
18
+ '@swc-node/register',
19
+ 'ts-node/register'
20
+ ].filter(Boolean) as string[]
21
+ for (const register of registers) {
22
+ if (packageIsExist(register)) {
23
+ tsRegisterName = register
24
+ break
25
+ }
26
+ }
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = () => console.log('hello')