我的汇总配置如下所示:
import {babel, RollupBabelInputPluginOptions} from '@rollup/plugin-babel'
import {nodeResolve} from '@rollup/plugin-node-resolve'
import nodeExternals from 'rollup-plugin-node-externals'
import json from '@rollup/plugin-json'
import commonjs from '@rollup/plugin-commonjs'
import findUp from 'find-up'
import {readFileSync} from 'fs'
import packagePlugin from '@mpen/rollup-plugin-package'
import cleanPlugin from '@mpen/rollup-plugin-clean'
import runPlugin from '@mpen/rollup-plugin-run'
import execPlugin, {RollupPluginExecutableOptions} from '@mpen/rollup-plugin-executable'
// import renameNodeModules from 'rollup-plugin-rename-node-modules'
import type {RollupOptions, WatcherOptions} from 'rollup'
// see also: https://github.com/rollup/rollup-starter-app
interface RollupPresetTsappOptions {
tsconfig?: string
babelOptions?: RollupBabelInputPluginOptions
watch?: WatcherOptions
nodeOptions?: RollupPluginExecutableOptions
}
type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T; // https://stackoverflow.com/a/58110124/65387
function truthy<T>(value: T): value is Truthy<T> {
return Boolean(value);
}
export default function rollupPresetTsapp(opts: RollupPresetTsappOptions = {}): RollupOptions {
const tsconfigFile = findUp.sync(opts.tsconfig ?? 'tsconfig.json')
if(!tsconfigFile) throw new Error('tsconfig.json file not found')
const tsconfig = JSON.parse(readFileSync(tsconfigFile, 'utf8'))
const extensions = ['.ts', '.mjs', '.js', '.json', '.node'];
const isWatch = process.env.ROLLUP_WATCH === 'true'
return {
input: tsconfig.files,
plugins: [
!isWatch && cleanPlugin(),
nodeExternals({
builtins: true,
deps: isWatch,
devDeps: isWatch,
peerDeps: true,
optDeps: true,
}),
nodeResolve({
extensions,
preferBuiltins: true,
}),
commonjs({
include: 'node_modules/**',
}),
json({preferConst: true}),
babel({
exclude: 'node_modules/**',
extensions,
comments: false,
babelHelpers: 'bundled', // https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers
root: `${__dirname}/..`,
// babelrcRoots: ['.', __dirname], // https://babeljs.io/docs/en/options#babelrcroots
...opts.babelOptions,
}),
execPlugin({
maxOldSpaceSize: 4*1024,
enableSourceMaps: true,
...opts.nodeOptions,
}),
// renameNodeModules('external'),
!isWatch && packagePlugin(),
isWatch && runPlugin(),
].filter(truthy),
watch: {
buildDelay: 200,
clearScreen: false,
...opts.watch,
},
preserveSymlinks: true, // https://www.npmjs.com/package/@rollup/plugin-commonjs#usage-with-symlinks
preserveModules: false, // outputs multiple files; https://stackoverflow.com/questions/66219812/force-include-node-modules-in-package https://github.com/rollup/rollup/issues/3684
output: {
// banner: `#!/usr/bin/env -S node --max-old-space-size=${opts.memory ?? 8192} --enable-source-maps`,
dir: 'dist',
format: 'cjs',
sourcemap: true,
exports: 'named',
},
}
}
然而,在编译我的项目时,我得到:
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
parse5 (imported by parse5?commonjs-external)
mariadb (imported by mariadb?commonjs-external)
highlight.js (imported by highlight.js?commonjs-external)
parse5-htmlparser2-tree-adapter (imported by parse5-htmlparser2-tree-adapter?commonjs-external)
bl (imported by bl?commonjs-external)
log-symbols (imported by log-symbols?commonjs-external)
cli-cursor (imported by cli-cursor?commonjs-external)
cli-spinners (imported by cli-spinners?commonjs-external)
strip-ansi (imported by strip-ansi?commonjs-external)
wcwidth (imported by wcwidth?commonjs-external)
is-interactive (imported by is-interactive?commonjs-external)
fs-constants (imported by fs-constants?commonjs-external)
inherits (imported by inherits?commonjs-external)
readable-stream (imported by readable-stream?commonjs-external)
end-of-stream (imported by end-of-stream?commonjs-external)
supports-color (imported by supports-color?commonjs-external)
ansi-styles (imported by ansi-styles?commonjs-external)
emoji-regex (imported by emoji-regex?commonjs-external)
is-fullwidth-code-point (imported by is-fullwidth-code-point?commonjs-external)
我不想把这些都当作是外部的,但它们并没有被编译,即使我有
nodeResolve
为什么不呢?
看起来它们都不是我的项目的直接依赖项,而是间接依赖项(我的依赖项的依赖项)。