snippetadapter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 CKEditorWebpackPlugin = require( '@ckeditor/ckeditor5-dev-webpack-plugin' );
  11. // const BabiliPlugin = require( 'babili-webpack-plugin' );
  12. module.exports = function snippetAdapter( data ) {
  13. const snippetConfig = readSnippetConfig( data.snippetSource.js );
  14. const webpackConfig = getWebpackConfig( {
  15. entry: data.snippetSource.js,
  16. outputPath: path.join( data.outputPath, data.snippetPath ),
  17. language: snippetConfig.language
  18. } );
  19. return runWebpack( webpackConfig )
  20. .then( () => {
  21. return {
  22. html: generateSnippetHtml( {
  23. htmlPath: data.snippetSource.html,
  24. scriptPath: path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
  25. } )
  26. };
  27. } );
  28. };
  29. function getWebpackConfig( config ) {
  30. return {
  31. devtool: 'source-map',
  32. entry: config.entry,
  33. output: {
  34. path: config.outputPath,
  35. filename: 'snippet.js'
  36. },
  37. plugins: [
  38. new CKEditorWebpackPlugin( {
  39. languages: [ config.language || 'en' ]
  40. } ),
  41. // new BabiliPlugin( null, {
  42. // comments: false
  43. // } ),
  44. new webpack.BannerPlugin( {
  45. banner: bundler.getLicenseBanner(),
  46. raw: true
  47. } )
  48. ],
  49. // Configure the paths so building CKEditor 5 snippets work even if the script
  50. // is triggered from a directory outside ckeditor5 (e.g. multi-project case).
  51. resolve: {
  52. modules: getModuleResolvePaths()
  53. },
  54. resolveLoader: {
  55. modules: getModuleResolvePaths()
  56. },
  57. module: {
  58. rules: [
  59. {
  60. test: /\.svg$/,
  61. use: [ 'raw-loader' ]
  62. },
  63. {
  64. test: /\.scss$/,
  65. use: [
  66. 'style-loader',
  67. {
  68. loader: 'css-loader',
  69. options: {
  70. minimize: true
  71. }
  72. },
  73. 'sass-loader'
  74. ]
  75. }
  76. ]
  77. }
  78. };
  79. }
  80. function runWebpack( webpackConfig ) {
  81. return new Promise( ( resolve, reject ) => {
  82. webpack( webpackConfig, ( err, stats ) => {
  83. if ( err ) {
  84. reject( err );
  85. } else if ( stats.hasErrors() ) {
  86. reject( new Error( stats.toString() ) );
  87. } else {
  88. resolve();
  89. }
  90. } );
  91. } );
  92. }
  93. function generateSnippetHtml( data ) {
  94. let html = fs.readFileSync( data.htmlPath );
  95. html += `<script src="${ data.scriptPath }"></script>`;
  96. return html;
  97. }
  98. function getModuleResolvePaths() {
  99. return [
  100. path.resolve( __dirname, '..', '..', '..', 'node_modules' ),
  101. 'node_modules'
  102. ];
  103. }
  104. function readSnippetConfig( snippetSourcePath ) {
  105. const snippetSource = fs.readFileSync( snippetSourcePath ).toString();
  106. const configSourceMatch = snippetSource.match( /\n\/\* config ([\s\S]+?)\*\// );
  107. if ( !configSourceMatch ) {
  108. return {};
  109. }
  110. return JSON.parse( configSourceMatch[ 1 ] );
  111. }