UNPKG

1.57 kBJavaScriptView Raw
1'use strict';
2const typescript = require('rollup-plugin-typescript2');
3const { terser } = require('rollup-plugin-terser');
4const commonjs = require('@rollup/plugin-commonjs');
5const json = require('@rollup/plugin-json');
6const path = require('path');
7const fs = require('fs');
8
9const BASE_PATH = process.cwd();
10
11const getConfigFile = name => {
12 const filePath = path.resolve(BASE_PATH, name);
13 if (fs.existsSync(path.resolve(BASE_PATH, name))) {
14 return filePath;
15 }
16
17 return require.resolve(`@stoplight/scripts/${name}`);
18};
19
20const plugins = () =>
21 [
22 typescript({
23 tsconfig: getConfigFile('tsconfig.build.json'),
24 useTsconfigDeclarationDir: true,
25 }),
26 process.env.MINIFY ? terser() : null,
27 ].filter(Boolean);
28
29const packageJson = JSON.parse(fs.readFileSync(path.resolve(BASE_PATH, 'package.json'), { encoding: 'utf-8' }));
30const dependencies = [
31 ...Object.keys(packageJson.dependencies || {}),
32 ...Object.keys(packageJson.peerDependencies || {}),
33];
34const external = module => dependencies.some(dep => module === dep || module.startsWith(`${dep}/`));
35
36module.exports = [
37 {
38 input: path.resolve(BASE_PATH, 'src/index.ts'),
39 output: {
40 entryFileNames: '[name].js',
41 dir: path.resolve(BASE_PATH, 'dist'),
42 format: 'cjs',
43 },
44 plugins: [json(), commonjs(), ...plugins()],
45 external
46 },
47 {
48 input: path.resolve(BASE_PATH, 'src/index.ts'),
49 output: {
50 entryFileNames: '[name].mjs',
51 dir: path.resolve(BASE_PATH, 'dist'),
52 format: 'esm',
53 },
54 plugins: plugins(),
55 external,
56 },
57];