snippetadapter.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. module: {
  44. rules: [
  45. {
  46. test: /\.svg$/,
  47. use: [ 'raw-loader' ]
  48. },
  49. {
  50. test: /\.scss$/,
  51. use: [
  52. 'style-loader',
  53. {
  54. loader: 'css-loader',
  55. options: {
  56. minimize: true
  57. }
  58. },
  59. 'sass-loader'
  60. ]
  61. }
  62. ]
  63. }
  64. };
  65. }
  66. function runWebpack( webpackConfig ) {
  67. return new Promise( ( resolve, reject ) => {
  68. webpack( webpackConfig, ( err, stats ) => {
  69. if ( err ) {
  70. reject( err );
  71. } else if ( stats.hasErrors() ) {
  72. reject( new Error( stats.toString() ) );
  73. } else {
  74. resolve();
  75. }
  76. } );
  77. } );
  78. }
  79. function generateSnippetHtml( data ) {
  80. let html = fs.readFileSync( data.htmlPath );
  81. html += `<script src="${ data.scriptPath }"></script>`;
  82. return html;
  83. }