|
1 |
| -import path from 'path' |
2 |
| -import ts from 'rollup-plugin-typescript2' |
3 |
| -// import replace from '@rollup/plugin-replace' |
4 |
| -import json from '@rollup/plugin-json' |
5 |
| -import commonjs from '@rollup/plugin-commonjs' |
6 |
| -import resolvePlugin from '@rollup/plugin-node-resolve' |
| 1 | +import path from "path"; |
| 2 | +import ts from "rollup-plugin-typescript2"; |
| 3 | +import json from "@rollup/plugin-json"; |
| 4 | +import commonjs from "@rollup/plugin-commonjs"; |
| 5 | +import resolvePlugin from "@rollup/plugin-node-resolve"; |
7 | 6 |
|
8 |
| -console.log(process.env.TARGET); |
| 7 | +if (!process.env.TARGET) { |
| 8 | + throw new Error("TARGET package must be specified via --environment flag"); |
| 9 | +} |
| 10 | + |
| 11 | +const packagesDir = path.resolve(__dirname, "packages"); |
| 12 | +const packageDir = path.resolve(packagesDir, process.env.TARGET); |
| 13 | +const resolve = (p) => path.resolve(packageDir, p); |
| 14 | +const pkg = require(resolve("package.json")); |
| 15 | +const packageOptions = pkg.buildOptions || {}; |
| 16 | +const name = packageOptions.filename || path.basename(packageDir); |
| 17 | + |
| 18 | +const outputConfig = { |
| 19 | + "esm-bundler": { |
| 20 | + file: resolve(`dist/${name}.esm-bundler.js`), |
| 21 | + format: "es", |
| 22 | + }, |
| 23 | + "esm-browser": { |
| 24 | + file: resolve(`dist/${name}.esm-browser.js`), |
| 25 | + format: "es", |
| 26 | + }, |
| 27 | + cjs: { |
| 28 | + file: resolve(`dist/${name}.cjs.js`), |
| 29 | + format: "cjs", |
| 30 | + }, |
| 31 | + global: { |
| 32 | + file: resolve(`dist/${name}.global.js`), |
| 33 | + format: "iife", |
| 34 | + }, |
| 35 | +}; |
| 36 | + |
| 37 | +const defaultFormats = ["esm-bundler", "cjs"]; |
| 38 | +const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(","); |
| 39 | +const packageFormats = |
| 40 | + inlineFormats || packageOptions.formats || defaultFormats; |
| 41 | + |
| 42 | +const packageConfigs = packageFormats.map((format) => |
| 43 | + createConfig(format, outputConfig[format]) |
| 44 | +); |
| 45 | + |
| 46 | +export default packageConfigs; |
| 47 | + |
| 48 | +function createConfig(format, output) { |
| 49 | + if (!output) { |
| 50 | + console.error(`invalid format: "${format}"`); |
| 51 | + return process.exit(1); |
| 52 | + } |
| 53 | + const isGlobalBuild = /global/.test(format); |
| 54 | + if (isGlobalBuild) { |
| 55 | + output.name = packageOptions.name; |
| 56 | + } |
| 57 | + |
| 58 | + // output.sourcemap = !!process.env.SOURCE_MAP |
| 59 | + output.sourcemap = true; |
9 | 60 |
|
10 |
| -if(!process.env.TARGET){ |
11 |
| - throw new Error('TARGET package must be specified via --environment flag') |
| 61 | + return { |
| 62 | + input: resolve(`src/index.ts`), |
| 63 | + output, |
| 64 | + plugins: [ |
| 65 | + json(), |
| 66 | + ts({ |
| 67 | + tsconfig: path.resolve(__dirname, "tsconfig.json"), |
| 68 | + }), |
| 69 | + resolvePlugin(), |
| 70 | + commonjs(), |
| 71 | + ], |
| 72 | + onwarn: (msg, warn) => { |
| 73 | + // 忽略Circular的提醒 |
| 74 | + if (!/Circular/.test(msg)) { |
| 75 | + warn(msg); |
| 76 | + } |
| 77 | + }, |
| 78 | + }; |
12 | 79 | }
|
0 commit comments