bs-commonjs-generator.js 978 B

12345678910111213141516171819202122232425262728293031
  1. /*!
  2. * Bootstrap Grunt task for the CommonJS module generation
  3. * https://getbootstrap.com/
  4. * Copyright 2014-2019 Twitter, Inc.
  5. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  6. */
  7. 'use strict';
  8. var fs = require('fs');
  9. var path = require('path');
  10. var COMMONJS_BANNER = '// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.\n';
  11. module.exports = function generateCommonJSModule(grunt, srcFiles, destFilepath) {
  12. var destDir = path.dirname(destFilepath);
  13. function srcPathToDestRequire(srcFilepath) {
  14. var requirePath = path.posix.relative(destDir, srcFilepath);
  15. return 'require(\'' + requirePath + '\')';
  16. }
  17. var moduleOutputJs = COMMONJS_BANNER + srcFiles.map(srcPathToDestRequire).join('\n');
  18. try {
  19. fs.writeFileSync(destFilepath, moduleOutputJs);
  20. } catch (err) {
  21. grunt.fail.warn(err);
  22. }
  23. grunt.log.writeln('File ' + destFilepath.cyan + ' created.');
  24. };