|
| 1 | +const fs = require('fs'); |
| 2 | +const { DEFAULT_EXTENSIONS } = require('@babel/core'); |
| 3 | +const babel = require('@rollup/plugin-babel').babel; |
| 4 | +const { dts } = require('rolldown-plugin-dts'); |
| 5 | +const pkg = require('../package.json'); |
| 6 | + |
| 7 | +const env = process.env.NODE_ENV; |
| 8 | +const version = process.env.npm_package_version; |
| 9 | +const license = fs |
| 10 | + .readFileSync('./src/license_header', 'utf8') |
| 11 | + .replace(/VERSION/gi, version); |
| 12 | + |
| 13 | +const experimental = { attachDebugInfo: 'none' }; |
| 14 | + |
| 15 | +const commonOutputConfig = { |
| 16 | + name: 'DOMPurify', |
| 17 | + sourcemap: true, |
| 18 | + banner: license, |
| 19 | + exports: 'default', |
| 20 | + sourcemapExcludeSources: true, |
| 21 | +}; |
| 22 | + |
| 23 | +// The source is ESM, so it has always executed in strict mode, and Rollup put |
| 24 | +// `'use strict'` inside the format wrapper of every non-ESM bundle to keep it |
| 25 | +// that way. Rolldown's `transform.target` equivalent here is `output.strict`, |
| 26 | +// but that hoists the directive *above* the UMD wrapper, making the whole |
| 27 | +// script strict and leaking that to anything concatenated after it. `intro` |
| 28 | +const useStrict = { intro: "'use strict';" }; |
| 29 | + |
| 30 | +const bundleOutputs = { |
| 31 | + umd: { |
| 32 | + ...commonOutputConfig, |
| 33 | + ...useStrict, |
| 34 | + file: pkg.browser, |
| 35 | + format: 'umd', |
| 36 | + }, |
| 37 | + 'umd-min': { |
| 38 | + ...commonOutputConfig, |
| 39 | + ...useStrict, |
| 40 | + file: pkg.production, |
| 41 | + format: 'umd', |
| 42 | + // Oxc's minifier defaults to `esnext` and would re-introduce `||=` / `?.` |
| 43 | + // into code Babel already down-levelled for the `.babelrc` browsers. |
| 44 | + minify: { compress: { target: 'es2015' } }, |
| 45 | + }, |
| 46 | + es: { ...commonOutputConfig, file: pkg.module, format: 'es' }, |
| 47 | + cjs: { ...commonOutputConfig, ...useStrict, file: pkg.main, format: 'cjs' }, |
| 48 | +}; |
| 49 | + |
| 50 | +const target = process.env.DOMPURIFY_BUILD_TARGET; |
| 51 | + |
| 52 | +if (target && !bundleOutputs[target]) { |
| 53 | + throw new Error(`Unknown DOMPURIFY_BUILD_TARGET: ${target}`); |
| 54 | +} |
| 55 | + |
| 56 | +const bundle = { |
| 57 | + input: 'src/purify.ts', |
| 58 | + external: [], |
| 59 | + experimental, |
| 60 | + plugins: [ |
| 61 | + babel({ |
| 62 | + babelHelpers: 'bundled', |
| 63 | + exclude: ['**/node_modules/**'], |
| 64 | + extensions: [...DEFAULT_EXTENSIONS, '.ts'], |
| 65 | + presets: ['@babel/preset-typescript'], |
| 66 | + }), |
| 67 | + ], |
| 68 | + transform: { |
| 69 | + define: { |
| 70 | + 'process.env.NODE_ENV': JSON.stringify(env) ?? 'undefined', |
| 71 | + VERSION: `'${version}'`, |
| 72 | + }, |
| 73 | + }, |
| 74 | + output: target ? [bundleOutputs[target]] : Object.values(bundleOutputs), |
| 75 | +}; |
| 76 | + |
| 77 | +const declarations = (file, plugins = []) => ({ |
| 78 | + input: './dist/types/purify.d.ts', |
| 79 | + external: [/^[^.\/]/], |
| 80 | + experimental, |
| 81 | + output: [{ file, format: 'es', banner: license }], |
| 82 | + plugins: [...plugins, dts({ dtsInput: true })], |
| 83 | +}); |
| 84 | + |
| 85 | +module.exports = target |
| 86 | + ? [bundle] |
| 87 | + : [ |
| 88 | + bundle, |
| 89 | + // ESM type declarations |
| 90 | + declarations(pkg.module.replace(/\.mjs$/, '.d.mts')), |
| 91 | + // CJS type declarations with named export stripping |
| 92 | + declarations(pkg.main.replace(/\.js$/, '.d.ts')), |
| 93 | + ]; |
0 commit comments