|
| 1 | +const path = require("path"); |
| 2 | +const babel = require("@rollup/plugin-babel").default; |
| 3 | +const copy = require("rollup-plugin-copy"); |
| 4 | +const extensions = require("rollup-plugin-extensions"); |
| 5 | +const prettier = require("rollup-plugin-prettier"); |
| 6 | +const replace = require("@rollup/plugin-replace"); |
| 7 | +const { terser } = require("rollup-plugin-terser"); |
| 8 | +const typescript = require("@rollup/plugin-typescript"); |
| 9 | +const { |
| 10 | + createBanner, |
| 11 | + getBuildDirectories, |
| 12 | + PRETTY, |
| 13 | +} = require("../../rollup.utils"); |
| 14 | +const { name, version } = require("./package.json"); |
| 15 | + |
| 16 | +module.exports = function rollup() { |
| 17 | + const { ROOT_DIR, SOURCE_DIR, OUTPUT_DIR } = getBuildDirectories( |
| 18 | + name, |
| 19 | + // We don't live in a folder matching our package name |
| 20 | + "router" |
| 21 | + ); |
| 22 | + |
| 23 | + // JS modules for bundlers |
| 24 | + const modules = [ |
| 25 | + { |
| 26 | + input: `${SOURCE_DIR}/index.ts`, |
| 27 | + output: { |
| 28 | + file: `${OUTPUT_DIR}/index.js`, |
| 29 | + format: "esm", |
| 30 | + sourcemap: !PRETTY, |
| 31 | + banner: createBanner("@remix-run/router", version), |
| 32 | + }, |
| 33 | + plugins: [ |
| 34 | + extensions({ extensions: [".ts"] }), |
| 35 | + babel({ |
| 36 | + babelHelpers: "bundled", |
| 37 | + exclude: /node_modules/, |
| 38 | + presets: [ |
| 39 | + ["@babel/preset-env", { loose: true }], |
| 40 | + "@babel/preset-typescript", |
| 41 | + ], |
| 42 | + plugins: ["babel-plugin-dev-expression"], |
| 43 | + extensions: [".ts"], |
| 44 | + }), |
| 45 | + typescript({ |
| 46 | + tsconfig: path.join(__dirname, "tsconfig.json"), |
| 47 | + exclude: ["__tests__"], |
| 48 | + noEmitOnError: true, |
| 49 | + }), |
| 50 | + copy({ |
| 51 | + targets: [ |
| 52 | + { src: path.join(ROOT_DIR, "LICENSE.md"), dest: SOURCE_DIR }, |
| 53 | + ], |
| 54 | + verbose: true, |
| 55 | + }), |
| 56 | + ].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 57 | + }, |
| 58 | + ]; |
| 59 | + |
| 60 | + // JS modules for <script type=module> |
| 61 | + const webModules = [ |
| 62 | + { |
| 63 | + input: `${SOURCE_DIR}/index.ts`, |
| 64 | + output: { |
| 65 | + file: `${OUTPUT_DIR}/router.development.js`, |
| 66 | + format: "esm", |
| 67 | + sourcemap: !PRETTY, |
| 68 | + banner: createBanner("@remix-run/router", version), |
| 69 | + }, |
| 70 | + plugins: [ |
| 71 | + extensions({ extensions: [".ts"] }), |
| 72 | + babel({ |
| 73 | + babelHelpers: "bundled", |
| 74 | + exclude: /node_modules/, |
| 75 | + presets: ["@babel/preset-modules", "@babel/preset-typescript"], |
| 76 | + plugins: ["babel-plugin-dev-expression"], |
| 77 | + extensions: [".ts"], |
| 78 | + }), |
| 79 | + replace({ |
| 80 | + preventAssignment: true, |
| 81 | + values: { "process.env.NODE_ENV": JSON.stringify("development") }, |
| 82 | + }), |
| 83 | + ].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 84 | + }, |
| 85 | + { |
| 86 | + input: `${SOURCE_DIR}/index.ts`, |
| 87 | + output: { |
| 88 | + file: `${OUTPUT_DIR}/router.production.min.js`, |
| 89 | + format: "esm", |
| 90 | + sourcemap: !PRETTY, |
| 91 | + banner: createBanner("@remix-run/router", version), |
| 92 | + }, |
| 93 | + plugins: [ |
| 94 | + extensions({ extensions: [".ts"] }), |
| 95 | + babel({ |
| 96 | + babelHelpers: "bundled", |
| 97 | + exclude: /node_modules/, |
| 98 | + presets: [ |
| 99 | + [ |
| 100 | + "@babel/preset-modules", |
| 101 | + { |
| 102 | + // Don't spoof `.name` for Arrow Functions, which breaks when minified anyway. |
| 103 | + loose: true, |
| 104 | + }, |
| 105 | + ], |
| 106 | + "@babel/preset-typescript", |
| 107 | + ], |
| 108 | + plugins: ["babel-plugin-dev-expression"], |
| 109 | + extensions: [".ts"], |
| 110 | + }), |
| 111 | + replace({ |
| 112 | + preventAssignment: true, |
| 113 | + values: { "process.env.NODE_ENV": JSON.stringify("production") }, |
| 114 | + }), |
| 115 | + // compiler(), |
| 116 | + terser({ ecma: 8, safari10: true }), |
| 117 | + ].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 118 | + }, |
| 119 | + ]; |
| 120 | + |
| 121 | + // UMD modules for <script> tags and CommonJS (node) |
| 122 | + const globals = [ |
| 123 | + { |
| 124 | + input: `${SOURCE_DIR}/index.ts`, |
| 125 | + output: { |
| 126 | + file: `${OUTPUT_DIR}/umd/router.development.js`, |
| 127 | + format: "umd", |
| 128 | + sourcemap: !PRETTY, |
| 129 | + banner: createBanner("@remix-run/router", version), |
| 130 | + name: "Router", |
| 131 | + }, |
| 132 | + plugins: [ |
| 133 | + extensions({ extensions: [".ts"] }), |
| 134 | + babel({ |
| 135 | + babelHelpers: "bundled", |
| 136 | + exclude: /node_modules/, |
| 137 | + presets: [ |
| 138 | + ["@babel/preset-env", { loose: true }], |
| 139 | + "@babel/preset-typescript", |
| 140 | + ], |
| 141 | + plugins: ["babel-plugin-dev-expression"], |
| 142 | + extensions: [".ts"], |
| 143 | + }), |
| 144 | + replace({ |
| 145 | + preventAssignment: true, |
| 146 | + values: { "process.env.NODE_ENV": JSON.stringify("development") }, |
| 147 | + }), |
| 148 | + ].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 149 | + }, |
| 150 | + { |
| 151 | + input: `${SOURCE_DIR}/index.ts`, |
| 152 | + output: { |
| 153 | + file: `${OUTPUT_DIR}/umd/router.production.min.js`, |
| 154 | + format: "umd", |
| 155 | + sourcemap: !PRETTY, |
| 156 | + banner: createBanner("@remix-run/router", version), |
| 157 | + name: "Router", |
| 158 | + }, |
| 159 | + plugins: [ |
| 160 | + extensions({ extensions: [".ts"] }), |
| 161 | + babel({ |
| 162 | + babelHelpers: "bundled", |
| 163 | + exclude: /node_modules/, |
| 164 | + presets: [ |
| 165 | + ["@babel/preset-env", { loose: true }], |
| 166 | + "@babel/preset-typescript", |
| 167 | + ], |
| 168 | + plugins: ["babel-plugin-dev-expression"], |
| 169 | + extensions: [".ts"], |
| 170 | + }), |
| 171 | + replace({ |
| 172 | + preventAssignment: true, |
| 173 | + values: { "process.env.NODE_ENV": JSON.stringify("production") }, |
| 174 | + }), |
| 175 | + // compiler(), |
| 176 | + terser(), |
| 177 | + ].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 178 | + }, |
| 179 | + ]; |
| 180 | + |
| 181 | + // Node entry points |
| 182 | + const node = [ |
| 183 | + { |
| 184 | + input: `${SOURCE_DIR}/node-main.js`, |
| 185 | + output: { |
| 186 | + file: `${OUTPUT_DIR}/main.js`, |
| 187 | + format: "cjs", |
| 188 | + banner: createBanner("@remix-run/router", version), |
| 189 | + }, |
| 190 | + plugins: [].concat(PRETTY ? prettier({ parser: "babel" }) : []), |
| 191 | + }, |
| 192 | + ]; |
| 193 | + |
| 194 | + return [...modules, ...webModules, ...globals, ...node]; |
| 195 | +}; |
0 commit comments