UNPKG

rollup-plugin-google-apps-script

Version:
152 lines (142 loc) 5.31 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var fs = require('fs'); var path = require('path'); var pc = require('picocolors'); var gasEntryGenerator = require('gas-entry-generator'); var rollupPluginutils = require('rollup-pluginutils'); const getRelativePath = (filePath) => { return [".", path.relative(process.cwd(), filePath)].join(path.sep); }; const generateEntry = (code, prams) => gasEntryGenerator.generate(code, prams); const manifest = "appsscript.json"; const loadManifest = (sourceFile) => { return fs.readFileSync(sourceFile, { encoding: "utf8", }); }; const rollupPluginGasCopyManifest = (configuratedOptions) => { return { name: "rollup-plugin-gas-copy-manifest", generateBundle() { if (!configuratedOptions.manifest.copy) { return; } const sourceFile = path.join(configuratedOptions.manifest.srcDir, manifest); this.info(pc.gray("Copy the manifest from: ") + pc.green(getRelativePath(sourceFile))); if (!fs.existsSync(sourceFile)) { this.error("Manifest file is not exist: " + sourceFile); } this.emitFile({ type: "asset", name: "manifest", fileName: manifest, source: loadManifest(sourceFile), }); }, }; }; const generateChunckHeader = (code, id) => { const filename = path.basename(id); const title = ` !*** ${filename} ***!`; const ast = Array(filename.length).fill("*"); const header = "/*!****" + ast.join("") + "****!*\\"; const footer = "\\*!****" + ast.join("") + "****!*/"; return [header, title, footer, code].join("\n"); }; const rollupPluginGasEntryPoint = (configuratedOptions) => { const entryPointFunctions = []; const filter = rollupPluginutils.createFilter(configuratedOptions.include); return { name: "rollup-plugin-gas-entry-point", outputOptions(options) { options.format = "umd"; // cjs return options; }, transform(code, id) { if (id.slice(-5).toLowerCase() === ".json") return; if (!filter(id)) { this.info(pc.gray("Excluded target: ") + pc.yellow(pc.bold(getRelativePath(id)))); if (configuratedOptions.moduleHeaderComment) { return generateChunckHeader(code, id); } else { return; } } this.info(pc.gray("Generated entry points for: ") + pc.green(getRelativePath(id))); const gasCode = generateEntry(code, configuratedOptions.gasEntryOptions); if (gasCode.entryPointFunctions) { const codes = String(gasCode.entryPointFunctions).replace(/{\n}/g, "{};"); codes.split("\n").forEach((functionCode) => { if (functionCode) { this.debug(" -> " + functionCode); entryPointFunctions.push(`${functionCode}`); } }); } if (configuratedOptions.moduleHeaderComment) { return generateChunckHeader(code, id); } else { return; } }, banner() { return ["var global = this;", ...entryPointFunctions].join("\n"); }, }; }; const getPluginSettings = (defaultOptions, inputOptions) => { return Object.assign({}, defaultOptions, inputOptions); }; const getPluginSetting = (options) => { const defaultOptions = { include: ["**/*"], moduleHeaderComment: false, manifest: { copy: false, srcDir: process.cwd(), }, gasEntryOptions: { comment: false, // autoGlobalExports: false, // exportsIdentifierName: "exports", // globalIdentifierName: "global", }, verbose: false, }; if (!options) { return defaultOptions; } if (options.manifest && options.manifest.srcDir) { options.manifest.srcDir = path.join(process.cwd(), options.manifest.srcDir); } const configuredOptions = getPluginSettings(defaultOptions, options); configuredOptions.manifest = getPluginSettings(defaultOptions.manifest, options.manifest); configuredOptions.gasEntryOptions = getPluginSettings(defaultOptions.gasEntryOptions, options.gasEntryOptions); return configuredOptions; }; var name = "rollup-plugin-google-apps-script"; const rollupPluginGas = (options) => { const configuratedOptions = getPluginSetting(options); const { outputOptions, transform, banner } = rollupPluginGasEntryPoint(configuratedOptions); const { generateBundle } = rollupPluginGasCopyManifest(configuratedOptions); return { name, onLog() { if (!configuratedOptions.verbose) { return false; } }, outputOptions, transform, banner, generateBundle, }; }; exports.default = rollupPluginGas; module.exports = Object.assign(exports.default, exports); //# sourceMappingURL=index.js.map