|
1 |
| -import { defineConfig, Options } from 'tsup' |
2 |
| -import fs from 'fs' |
3 |
| -import sh from 'shelljs' |
4 |
| -import type { ExecOptions } from 'shelljs' |
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import type { Options } from 'tsup' |
| 4 | +import { defineConfig } from 'tsup' |
5 | 5 |
|
6 |
| -function execAsync(cmd: string, opts: ExecOptions = {}) { |
7 |
| - return new Promise(function (resolve, reject) { |
8 |
| - // Execute the command, reject if we exit non-zero (i.e. error) |
9 |
| - sh.exec(cmd, opts, function (code, stdout, stderr) { |
10 |
| - if (code !== 0) return reject(new Error(stderr)) |
11 |
| - return resolve(stdout) |
12 |
| - }) |
13 |
| - }) |
| 6 | +async function writeCommonJSEntry() { |
| 7 | + await fs.writeFile( |
| 8 | + path.join('dist/cjs/', 'index.js'), |
| 9 | + `'use strict' |
| 10 | +if (process.env.NODE_ENV === 'production') { |
| 11 | + module.exports = require('./reselect.production.min.cjs') |
| 12 | +} else { |
| 13 | + module.exports = require('./reselect.development.cjs') |
| 14 | +}` |
| 15 | + ) |
14 | 16 | }
|
15 | 17 |
|
16 |
| -export default defineConfig(options => { |
17 |
| - const commonOptions: Partial<Options> = { |
| 18 | +export default defineConfig((options): Options[] => { |
| 19 | + const commonOptions: Options = { |
18 | 20 | entry: {
|
19 | 21 | reselect: 'src/index.ts'
|
20 | 22 | },
|
21 | 23 | sourcemap: true,
|
| 24 | + target: ['esnext'], |
| 25 | + clean: true, |
22 | 26 | ...options
|
23 | 27 | }
|
24 | 28 |
|
25 | 29 | return [
|
26 |
| - // Modern ESM |
27 | 30 | {
|
28 | 31 | ...commonOptions,
|
| 32 | + name: 'Modern ESM', |
| 33 | + target: ['esnext'], |
29 | 34 | format: ['esm'],
|
30 |
| - outExtension: () => ({ js: '.mjs' }), |
31 |
| - dts: true, |
32 |
| - clean: true |
| 35 | + outExtension: () => ({ js: '.mjs' }) |
33 | 36 | },
|
34 | 37 |
|
35 | 38 | // Support Webpack 4 by pointing `"module"` to a file with a `.js` extension
|
36 | 39 | // and optional chaining compiled away
|
37 | 40 | {
|
38 | 41 | ...commonOptions,
|
| 42 | + name: 'Legacy ESM, Webpack 4', |
39 | 43 | entry: {
|
40 | 44 | 'reselect.legacy-esm': 'src/index.ts'
|
41 | 45 | },
|
42 | 46 | format: ['esm'],
|
43 | 47 | outExtension: () => ({ js: '.js' }),
|
44 |
| - target: 'es2017' |
| 48 | + target: ['es2017'] |
45 | 49 | },
|
46 |
| - // Browser-ready ESM, production + minified |
| 50 | + |
| 51 | + // Meant to be served up via CDNs like `unpkg`. |
47 | 52 | {
|
48 | 53 | ...commonOptions,
|
| 54 | + name: 'Browser-ready ESM', |
49 | 55 | entry: {
|
50 | 56 | 'reselect.browser': 'src/index.ts'
|
51 | 57 | },
|
52 |
| - define: { |
53 |
| - 'process.env.NODE_ENV': JSON.stringify('production') |
| 58 | + platform: 'browser', |
| 59 | + env: { |
| 60 | + NODE_ENV: 'production' |
54 | 61 | },
|
55 | 62 | format: ['esm'],
|
56 | 63 | outExtension: () => ({ js: '.mjs' }),
|
57 | 64 | minify: true
|
58 | 65 | },
|
59 | 66 | {
|
60 | 67 | ...commonOptions,
|
61 |
| - format: 'cjs', |
| 68 | + name: 'CJS Development', |
| 69 | + entry: { |
| 70 | + 'reselect.development': 'src/index.ts' |
| 71 | + }, |
| 72 | + env: { |
| 73 | + NODE_ENV: 'development' |
| 74 | + }, |
| 75 | + format: ['cjs'], |
62 | 76 | outDir: './dist/cjs/',
|
63 | 77 | outExtension: () => ({ js: '.cjs' })
|
| 78 | + }, |
| 79 | + { |
| 80 | + ...commonOptions, |
| 81 | + name: 'CJS production', |
| 82 | + entry: { |
| 83 | + 'reselect.production.min': 'src/index.ts' |
| 84 | + }, |
| 85 | + env: { |
| 86 | + NODE_ENV: 'production' |
| 87 | + }, |
| 88 | + format: ['cjs'], |
| 89 | + outDir: './dist/cjs/', |
| 90 | + outExtension: () => ({ js: '.cjs' }), |
| 91 | + minify: true, |
| 92 | + onSuccess: async () => { |
| 93 | + await writeCommonJSEntry() |
| 94 | + } |
| 95 | + }, |
| 96 | + { |
| 97 | + ...commonOptions, |
| 98 | + name: 'CJS Type Definitions', |
| 99 | + format: ['cjs'], |
| 100 | + dts: { only: true } |
64 | 101 | }
|
65 | 102 | ]
|
66 | 103 | })
|
0 commit comments