snippetadapter.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* eslint-env node */
  6. const path = require( 'path' );
  7. const fs = require( 'fs' );
  8. const webpack = require( 'webpack' );
  9. const { bundler } = require( '@ckeditor/ckeditor5-dev-utils' );
  10. // const BabiliPlugin = require( 'babili-webpack-plugin' );
  11. module.exports = function snippetAdapter( data ) {
  12. const webpackConfig = getWebpackConfig( {
  13. entry: data.snippetSource.js,
  14. outputPath: path.join( data.outputPath, data.snippetPath )
  15. } );
  16. return runWebpack( webpackConfig )
  17. .then( () => {
  18. return {
  19. html: generateSnippetHtml( {
  20. htmlPath: data.snippetSource.html,
  21. scriptPath: path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
  22. } )
  23. };
  24. } );
  25. };
  26. function getWebpackConfig( config ) {
  27. return {
  28. devtool: 'source-map',
  29. entry: config.entry,
  30. output: {
  31. path: config.outputPath,
  32. filename: 'snippet.js'
  33. },
  34. plugins: [
  35. // new BabiliPlugin( null, {
  36. // comments: false
  37. // } ),
  38. new webpack.BannerPlugin( {
  39. banner: bundler.getLicenseBanner(),
  40. raw: true
  41. } )
  42. ],
  43. // Configure the paths so building CKEditor 5 snippets work even if the script
  44. // is triggered from a directory outside ckeditor5 (e.g. multi-project case).
  45. resolve: {
  46. modules: getModuleResolvePaths()
  47. },
  48. resolveLoader: {
  49. modules: getModuleResolvePaths()
  50. },
  51. module: {
  52. rules: [
  53. {
  54. test: /\.svg$/,
  55. use: [ 'raw-loader' ]
  56. },
  57. {
  58. test: /\.scss$/,
  59. use: [
  60. 'style-loader',
  61. {
  62. loader: 'css-loader',
  63. options: {
  64. minimize: true
  65. }
  66. },
  67. 'sass-loader'
  68. ]
  69. }
  70. ]
  71. }
  72. };
  73. }
  74. function runWebpack( webpackConfig ) {
  75. return new Promise( ( resolve, reject ) => {
  76. webpack( webpackConfig, ( err, stats ) => {
  77. if ( err ) {
  78. reject( err );
  79. } else if ( stats.hasErrors() ) {
  80. reject( new Error( stats.toString() ) );
  81. } else {
  82. resolve();
  83. }
  84. } );
  85. } );
  86. }
  87. function generateSnippetHtml( data ) {
  88. let html = fs.readFileSync( data.htmlPath );
  89. html += `<script src="${ data.scriptPath }"></script>`;
  90. return html;
  91. }
  92. function getModuleResolvePaths() {
  93. return [
  94. path.resolve( __dirname, '..', '..', '..', 'node_modules' ),
  95. 'node_modules'
  96. ];
  97. }