-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
4,128 additions
and
1,707 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,52 @@ | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import autoExternal from 'rollup-plugin-auto-external'; | ||
import esbuild from 'rollup-plugin-esbuild'; | ||
import json from '@rollup/plugin-json'; | ||
import nodeResolve from '@rollup/plugin-node-resolve'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import findNodeModules from 'find-node-modules'; | ||
import builtinModules from 'builtin-modules'; | ||
import pkgJson from './package.json'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
let CHECKED_DEPS = []; | ||
|
||
export default function odpRollupConfig(options) { | ||
const extensions = ['.ts', '.js', '.mjs']; | ||
const resolveOptions = { | ||
mainFields: ['jsnext:main', 'es2017', 'es2015', 'module', 'main'], | ||
mainFields: ['jsnext:main', 'es2020', 'es2018', 'es2017', 'es2015', 'module', 'main'], | ||
preferBuiltins: true, | ||
extensions, | ||
modulesOnly: true, | ||
browser: true | ||
extensions: ['.ts', '.js', '.mjs', '.node'], | ||
modulesOnly: false, | ||
browser: false | ||
}; | ||
const plugins = [ | ||
json({ | ||
exclude: 'node_modules/**', | ||
preferConst: true, | ||
compact: true, | ||
namedExports: true, | ||
indent: ' ' | ||
json(), | ||
autoExternal({ | ||
builtins: true, | ||
peerDependencies: true, | ||
dependencies: true | ||
}), | ||
nodeResolve(resolveOptions), | ||
typescript({ | ||
useTsconfigDeclarationDir: false, | ||
tsconfigOverride: { | ||
compilerOptions: { module: 'esnext' } | ||
} | ||
}), | ||
commonjs(), | ||
terser() | ||
esbuild({ | ||
minify: true, | ||
target: 'esnext' | ||
}) | ||
]; | ||
|
||
function getContent(dir) { | ||
return JSON.parse(fs.readFileSync(dir, 'utf8')); | ||
} | ||
|
||
function getDependencies(pkg) { | ||
return [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]; | ||
} | ||
|
||
function getAllDependencies(dep) { | ||
let result = []; | ||
findNodeModules() | ||
.map((dir) => path.resolve(process.cwd(), dir)) | ||
.forEach((dir) => { | ||
const depPkg = path.join(dir, dep, 'package.json'); | ||
if (fs.existsSync(depPkg)) { | ||
const content = getContent(depPkg); | ||
const deps = getDependencies(content); | ||
deps.forEach((d) => { | ||
if (!CHECKED_DEPS.includes(d)) { | ||
CHECKED_DEPS = [...CHECKED_DEPS, d]; | ||
result = [...result, ...getAllDependencies(d)]; | ||
} | ||
}); | ||
result = [...result, ...deps]; | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
function filterDependencies(pkg, deps) { | ||
const { devDependencies } = pkg; | ||
let result = [...deps]; | ||
if (devDependencies) { | ||
result = result.filter((d) => !Object.keys(devDependencies).includes(d)); | ||
} | ||
return result.filter((dep, index, array) => array.indexOf(dep) === index); | ||
} | ||
|
||
function getRecursiveDependencies(pkg) { | ||
const deps = getDependencies(pkg); | ||
let result = [...deps]; | ||
deps.forEach((dep) => { | ||
CHECKED_DEPS = [...CHECKED_DEPS, dep]; | ||
result = [...result, ...getAllDependencies(dep)]; | ||
}); | ||
return filterDependencies(pkg, result); | ||
} | ||
|
||
const external = [...getRecursiveDependencies(pkgJson), ...builtinModules]; | ||
const outputConfig = { sourcemap: false, globals: {} }; | ||
const output = { | ||
const external = [...builtinModules]; | ||
const outputMain = { | ||
input: 'src/index.ts', | ||
treeshake: true, | ||
output: [ | ||
{ ...outputConfig, file: pkgJson.main, format: 'cjs' }, | ||
{ ...outputConfig, file: pkgJson.module, format: 'es' } | ||
], | ||
output: [{ sourcemap: false, file: pkgJson.main, format: 'cjs' }], | ||
plugins, | ||
external, | ||
...options | ||
}; | ||
const outputModule = { | ||
...outputMain, | ||
output: [{ sourcemap: false, file: pkgJson.module, format: 'esm' }] | ||
}; | ||
return [ | ||
output, | ||
outputMain, | ||
outputModule, | ||
{ | ||
...output, | ||
...outputMain, | ||
input: 'cli/cli.ts', | ||
output: [{ ...outputConfig, file: 'bin/cli.js', format: 'cjs', banner: '#!/usr/bin/env node' }] | ||
output: [{ sourcemap: false, file: 'bin/cli.js', format: 'cjs', banner: '#!/usr/bin/env node' }] | ||
} | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.