8
0

snippetadapter.js 3.1 KB

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