UNPKG

rollup-plugin-bucklescript

Version:
193 lines (161 loc) 5.23 kB
import { dirname, join } from 'path'; import { addExtension, createFilter } from 'rollup-pluginutils'; import { compileFile } from 'bsb-js'; import { readBsConfigSync } from 'read-bsconfig'; import fs from 'fs'; import nodeResolve from 'resolve'; var getPackageSpecs = function (packageSpecs) { var defaults = { moduleDir: 'es6', inSource: false, }; if (!packageSpecs) { return defaults; } if (Array.isArray(packageSpecs) && packageSpecs.length === 0) { return defaults; } var moduleSpec = Array.isArray(packageSpecs) ? packageSpecs[0] : packageSpecs; var moduleDir = typeof moduleSpec === 'string' ? moduleSpec : moduleSpec.module; var inSource = typeof moduleSpec === 'string' ? false : moduleSpec['in-source']; return { moduleDir: moduleDir, inSource: inSource }; }; var getBsConfigModuleOptions = function (buildDir) { var bsconfig = readBsConfigSync(buildDir); if (!bsconfig) { throw new Error(("bsconfig not found in " + buildDir)); } var bsSuffix = bsconfig.suffix; var suffix = typeof bsSuffix === 'string' ? bsSuffix : '.js'; var ref = getPackageSpecs(bsconfig['package-specs']); var moduleDir = ref.moduleDir; var inSource = ref.inSource; var options = { moduleDir: moduleDir, inSource: inSource, suffix: suffix }; return options; }; var jsFilePath = function (buildDir, moduleDir, resourcePath, inSource, suffix) { var outputDir = 'lib'; var fileNameRegex = /\.(ml|re)$/; var mlFileName = resourcePath.replace(buildDir, ''); var jsFileName = mlFileName.replace(fileNameRegex, suffix); if (inSource) { return join(buildDir, jsFileName); } return join(buildDir, outputDir, moduleDir, jsFileName); }; var isRelative = function (url) { var parts = url.split(/[\/\\]/).shift(); if (parts[0] === '.') { return true; } return false; }; var isStandardLibrary = function (id) { var runTimeLib = 'bs-platform/lib'; return id.substr(0, runTimeLib.length) === runTimeLib; }; var resolveStandardLibrary = function (id, importer) { return new Promise(function (resolve, reject) { nodeResolve(id, { basedir: dirname(importer) }, function (err, resolved) { if (!err) { if (resolved && fs.existsSync(resolved)) { resolve(fs.realpathSync(resolved)); } } else { reject(err); } }); }); }; var index = function (options) { if ( options === void 0 ) options = {}; var filter = createFilter( options.include || ['*.+(ml|re)', '**/*.+(ml|re)'], options.exclude || ['*.+(mli|rei)', '**/*.+(mli|rei)'] ); var buildDir = options.cwd || process.cwd(); var showWarnings = options.showWarnings !== undefined ? options.showWarnings : true; var bsconfig = getBsConfigModuleOptions(buildDir); var moduleDir = options.module || bsconfig.moduleDir; var bsSuffix = bsconfig.suffix; var inSourceBuild = options.inSource || bsconfig.inSource || false; var includeStandardLibrary = options.includeStandardLibrary !== undefined ? options.includeStandardLibrary : true; var warn = function (msg) { return console.warn(msg); }; return { name: 'bucklescript', resolveId: function (importee, importer) { if (isStandardLibrary(importee)) { if (includeStandardLibrary) { return resolveStandardLibrary(importee, importer); } else { warn("You have disabled \"includeStandardLibrary\" option.\nMake sure you know what you are doing.\nPlease make sure you check out the README\nhttps://github.com/shrynx/rollup-plugin-bucklescript#includeStandardLibrary"); } } if (!filter(importer)) { return null; } if (isRelative(importee)) { var buildpath = jsFilePath( buildDir, moduleDir, importer, inSourceBuild, bsSuffix ); var relativePath = join(buildpath, '..', importee); return addExtension(relativePath, bsSuffix); } return null; }, transform: function(code, id) { var this$1 = this; if (moduleDir !== 'es6') { throw new Error( "Please \"package-specs\" as \"[es6]\" in \"bsconfig.json\"\nor pass \" module: 'es6' \" in config options of bucklescript plugin\nhttps://github.com/shrynx/rollup-plugin-bucklescript#caveats" ); } if (!filter(id)) { return null; } var compiledFilePath = jsFilePath( buildDir, moduleDir, id, inSourceBuild, bsSuffix ); return compileFile( buildDir, moduleDir, compiledFilePath ).then(function (ref) { var src = ref.src; var warnings = ref.warnings; var errors = ref.errors; if (showWarnings) { warnings.forEach(function (message) { this$1.warn(message); }); } if (errors.length > 0) { throw new Error(errors[errors.length - 1]); } else { return { code: src, map: { mappings: '' }, }; } }); }, }; }; export default index; //# sourceMappingURL=rollup-plugin-bucklescript.es.js.map