import * as fs from "fs";import path from "path";import alias from "rollup-plugin-alias";import { plugin as analyze } from "rollup-plugin-analyzer";import commonjs from "rollup-plugin-commonjs";import globals from "rollup-plugin-node-globals";import nodeResolve from "rollup-plugin-node-resolve";import typescriptPlugin from "rollup-plugin-typescript2";import { createFilter } from "rollup-pluginutils";import replace from "rollup-plugin-replace";import typescript from "typescript";
const mockPath = path.resolve(__dirname, "js/mock_builtin.js");const tsconfig = path.resolve(__dirname, "tsconfig.json");const typescriptPath = path.resolve( __dirname, "third_party/node_modules/typescript/lib/typescript.js");const gnArgs = fs.readFileSync("gen/cli/gn_args.txt", "utf-8").trim();
const tsconfigOverride = { compilerOptions: { paths: { "*": ["*", path.resolve("*")] } }};
function strings( { include, exclude } = { include: undefined, exclude: undefined }) { if (!include) { throw new Error("include option must be passed"); }
const filter = createFilter(include, exclude);
return { name: "strings",
resolveId(importee) { if (importee.endsWith("!string")) { importee = importee.slice(0, importee.lastIndexOf("!string")); if (!importee.startsWith("gen/")) { return path.resolve(path.join(__dirname, importee)); } return path.resolve(path.join(process.cwd(), importee)); } },
transform(code, id) { if (filter(id)) { return { code: `export default ${JSON.stringify(code)};`, map: { mappings: "" } }; } } };}
const archNodeToDeno = { x64: "x64"};const osNodeToDeno = { win32: "win", darwin: "mac", linux: "linux"};
function resolveGenerated() { return { name: "resolve-msg-generated", resolveId(importee) { if (importee.startsWith("gen/cli/msg_generated")) { return path.resolve(`${importee}.ts`); } } };}
function generateDepFile({ outputFile, sourceFiles = [], configFiles = [] }) { let timestamp = new Date();
process.once("beforeExit", () => writeDepFile({ outputFile, sourceFiles, configFiles, timestamp }) );
return { name: "depfile", load(sourceFile) { if (/^[0-9a-f]{30}$/.test(sourceFile)) { return; } sourceFiles.push(sourceFile); timestamp = new Date(); } };}
function writeDepFile({ outputFile, sourceFiles, configFiles, timestamp }) { const buildDir = process.cwd(); const outputDir = path.dirname(outputFile);
sourceFiles.forEach(f => fs.accessSync(f)); let inputs = [...sourceFiles, ...configFiles, ...Object.keys(require.cache)]; inputs = Array.from(new Set(inputs.map(f => path.resolve(f)))); inputs = inputs.map(formatPath);
const depFile = path.join( outputDir, path.basename(outputFile, path.extname(outputFile)) + ".d" ); const outputs = [outputFile, depFile].map(formatPath);
const depFileContent = [ ...outputs.map(filename => `${filename}: ` + inputs.join(" ") + "\n\n"), ...inputs.map(filename => `${filename}:\n`) ].join("");
fs.writeFileSync(depFile, depFileContent); fs.utimesSync(depFile, timestamp, timestamp);
function formatPath(filename) { filename = path.relative(buildDir, filename); if (process.platform === "win32") { filename = filename.replace(/\\/g, "/"); } filename = filename.replace(/ /g, "\\ "); return filename; }}
export default function makeConfig(commandOptions) { return { output: { format: "iife", name: "denoMain", sourcemap: true, sourcemapExcludeSources: true },
plugins: [ replace({ ROLLUP_REPLACE_TS_VERSION: typescript.version, ROLLUP_REPLACE_ARCH: archNodeToDeno[process.arch], ROLLUP_REPLACE_OS: osNodeToDeno[process.platform], ROLLUP_REPLACE_GN_ARGS: gnArgs }),
alias({ fs: mockPath, path: mockPath, os: mockPath, crypto: mockPath, buffer: mockPath, module: mockPath }),
strings({ include: [ "*.d.ts", `${__dirname}/**/*.d.ts`, `${process.cwd()}/**/*.d.ts` ] }),
resolveGenerated(),
nodeResolve(),
commonjs({ namedExports: { [typescriptPath]: [ "createLanguageService", "formatDiagnostics", "formatDiagnosticsWithColorAndContext", "version", "Extension", "ModuleKind", "ScriptKind", "ScriptSnapshot", "ScriptTarget" ] } }),
typescriptPlugin({ tsconfig,
tsconfigOverride,
typescript,
include: ["*.ts", `${__dirname}/**/*.ts`, `${process.cwd()}/**/*.ts`],
exclude: [ "*.d.ts", `${__dirname}/**/*.d.ts`, `${process.cwd()}/**/*.d.ts` ] }),
analyze({ skipFormatted: true, onAnalysis({ bundleSize, bundleOrigSize, bundleReduction, moduleCount }) { if (!commandOptions.silent) { console.log( `Bundle size: ${Math.round((bundleSize / 1000000) * 100) / 100}Mb` ); console.log( `Original size: ${Math.round((bundleOrigSize / 1000000) * 100) / 100}Mb` ); console.log(`Reduction: ${bundleReduction}%`); console.log(`Module count: ${moduleCount}`); } } }),
globals(),
generateDepFile({ outputFile: commandOptions.o, configFiles: [commandOptions.c, tsconfig] }) ] };}