|
1 | 1 | // @ts-check |
2 | 2 | import { dirname } from 'path' |
3 | | -import { builtinModules } from 'module' |
| 3 | +import alias from '@rollup/plugin-alias' |
4 | 4 | import resolve from '@rollup/plugin-node-resolve' |
5 | 5 | import commonjs from '@rollup/plugin-commonjs' |
6 | | -import ts from 'rollup-plugin-ts' |
| 6 | +import typescript from 'rollup-plugin-ts' |
7 | 7 |
|
8 | | -// https://rollupjs.org/guide/en/#using-untranspiled-config-files |
9 | 8 | import pkg from './package.cjs' |
10 | 9 |
|
11 | | -/** |
12 | | - * A mini-plugin that resolves `node:` and `nodejs:` imports to their unprefixed equivalent. |
13 | | - * @type { import('rollup').PluginImpl } |
14 | | - */ |
15 | | -function nodeColon() { |
16 | | - return { |
17 | | - name: 'node-colon', |
18 | | - resolveId(id) { |
19 | | - for (const scheme of [ 'node:', 'nodejs:' ]) { |
20 | | - if (id.startsWith(scheme)) { |
21 | | - return { id: id.slice(scheme.length), external: true} |
22 | | - } |
23 | | - } |
24 | | - } |
25 | | - } |
| 10 | +/** @type {import('rollup').OutputOptions} */ |
| 11 | +const commonOutput = { |
| 12 | + sourcemap: false, |
| 13 | + generatedCode: 'es2015' |
26 | 14 | } |
27 | 15 |
|
28 | 16 | /** |
29 | | - * A mini-plugin that generates a package.json file next to the bundle. |
30 | | - * @type { import('rollup').PluginImpl<'module' | 'commonjs'> } |
| 17 | + * @param {'commonjs'|'module'} format |
| 18 | + * @returns {import('rollup').Plugin[]} |
31 | 19 | */ |
32 | | -function emitPkg(type) { |
33 | | - return { |
| 20 | +const plugins = format => ([ |
| 21 | + alias({ |
| 22 | + entries: [{ |
| 23 | + find: /^node(?:js)?:(.*)$/, |
| 24 | + replacement: '$1' |
| 25 | + }] |
| 26 | + }), |
| 27 | + resolve(), |
| 28 | + commonjs(), |
| 29 | + typescript({ |
| 30 | + tsconfig: cfg => ({ |
| 31 | + ...cfg, |
| 32 | + // No need to emit the declarations twice |
| 33 | + declaration: format === 'module', |
| 34 | + declarationDir: dirname(pkg.types) |
| 35 | + }) |
| 36 | + }), |
| 37 | + { |
| 38 | + // An inlined mini-plugin that generates a package.json |
| 39 | + // file next to the bundle with just the 'type' field set. |
34 | 40 | name: 'emit-pkg', |
35 | 41 | generateBundle() { |
36 | 42 | this.emitFile({ |
37 | 43 | type: 'asset', |
38 | 44 | fileName: 'package.json', |
39 | | - source: JSON.stringify({ type }, undefined, 2) |
| 45 | + source: JSON.stringify({ type: format }) |
40 | 46 | }) |
41 | 47 | } |
42 | 48 | } |
43 | | -} |
| 49 | +]) |
44 | 50 |
|
45 | | -/** @type {import('rollup').OutputOptions} */ |
46 | | -const commonOutput = { |
47 | | - sourcemap: true, |
48 | | - generatedCode: 'es2015' |
49 | | -} |
50 | | - |
51 | | -/** @type {import('rollup').RollupOptions} */ |
52 | | -const config = { |
53 | | - input: 'src/index.ts', |
54 | | - output: [ |
55 | | - { |
| 51 | +/** @type {import('rollup').RollupOptions[]} */ |
| 52 | +const configs = [ |
| 53 | + { |
| 54 | + input: 'src/index.ts', |
| 55 | + output: { |
56 | 56 | ...commonOutput, |
57 | 57 | format: 'commonjs', |
58 | 58 | file: pkg.main, |
59 | | - exports: 'auto', |
60 | | - sourcemap: true, |
61 | | - plugins: [ |
62 | | - emitPkg('commonjs') |
63 | | - ] |
| 59 | + exports: 'default', // externals is exported as default |
| 60 | + interop: 'default', // How to import external modules? |
64 | 61 | }, |
65 | | - { |
| 62 | + plugins: plugins('commonjs') |
| 63 | + }, |
| 64 | + { |
| 65 | + input: 'src/index.ts', |
| 66 | + output: { |
66 | 67 | ...commonOutput, |
67 | 68 | format: 'module', |
68 | | - file: pkg.module, |
69 | | - sourcemap: true, |
70 | | - plugins: [ |
71 | | - emitPkg('module') |
72 | | - ] |
| 69 | + file: pkg.module |
73 | 70 | }, |
74 | | - ], |
75 | | - plugins: [ |
76 | | - nodeColon(), |
77 | | - resolve(), |
78 | | - commonjs(), |
79 | | - ts({ |
80 | | - tsconfig: cfg => ({ |
81 | | - ...cfg, |
82 | | - declarationDir: dirname(pkg.types) |
83 | | - }) |
84 | | - }) |
85 | | - ], |
86 | | - external: builtinModules.concat(Object.keys(pkg.dependencies)) |
87 | | -} |
| 71 | + plugins: plugins('module'), |
| 72 | + // Leave find-up out of the esm build |
| 73 | + external: 'find-up' |
| 74 | + } |
| 75 | +] |
88 | 76 |
|
89 | | -export default config |
| 77 | +export default configs |
0 commit comments