snippetadapter.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2019, 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, styles } = require( '@ckeditor/ckeditor5-dev-utils' );
  10. const CKEditorWebpackPlugin = require( '@ckeditor/ckeditor5-dev-webpack-plugin' );
  11. const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
  12. const UglifyJsWebpackPlugin = require( 'uglifyjs-webpack-plugin' );
  13. const webpackProcesses = new Map();
  14. module.exports = function snippetAdapter( data ) {
  15. if ( !data.snippetSource.js ) {
  16. throw new Error( `Missing snippet source for "${ data.snippetPath }".` );
  17. }
  18. const snippetConfig = readSnippetConfig( data.snippetSource.js );
  19. const outputPath = path.join( data.outputPath, data.snippetPath );
  20. const webpackConfig = getWebpackConfig( {
  21. entry: data.snippetSource.js,
  22. outputPath,
  23. language: snippetConfig.language,
  24. production: data.options.production
  25. } );
  26. let promise;
  27. // See #530.
  28. if ( webpackProcesses.has( outputPath ) ) {
  29. promise = webpackProcesses.get( outputPath );
  30. } else {
  31. promise = runWebpack( webpackConfig );
  32. webpackProcesses.set( outputPath, promise );
  33. }
  34. return promise
  35. .then( () => {
  36. const wasCSSGenerated = fs.existsSync( path.join( outputPath, 'snippet.css' ) );
  37. const cssFiles = [
  38. path.join( data.basePath, 'assets', 'snippet-styles.css' )
  39. ];
  40. // CSS may not be generated by Webpack if a snippet's JS file didn't import any SCSS files.
  41. if ( wasCSSGenerated ) {
  42. cssFiles.unshift( path.join( data.relativeOutputPath, data.snippetPath, 'snippet.css' ) );
  43. }
  44. // If the snippet is a dependency of a parent snippet, append JS and CSS to HTML and save to disk.
  45. if ( data.isDependency ) {
  46. let htmlFile = fs.readFileSync( data.snippetSource.html ).toString();
  47. if ( wasCSSGenerated ) {
  48. htmlFile += '<link rel="stylesheet" href="snippet.css" type="text/css">';
  49. }
  50. htmlFile += '<script src="snippet.js"></script>';
  51. fs.writeFileSync( path.join( outputPath, 'snippet.html' ), htmlFile );
  52. }
  53. return {
  54. html: fs.readFileSync( data.snippetSource.html ),
  55. assets: {
  56. js: [
  57. // Load snippet helpers first.
  58. path.join( data.basePath, 'assets', 'snippet.js' ),
  59. // Then load the actual snippet code.
  60. path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
  61. ],
  62. css: cssFiles
  63. },
  64. dependencies: snippetConfig.dependencies
  65. };
  66. } );
  67. };
  68. function getWebpackConfig( config ) {
  69. return {
  70. mode: config.production ? 'production' : 'development',
  71. devtool: 'source-map',
  72. entry: config.entry,
  73. output: {
  74. path: config.outputPath,
  75. filename: 'snippet.js'
  76. },
  77. optimization: {
  78. minimizer: [
  79. new UglifyJsWebpackPlugin( {
  80. sourceMap: true,
  81. uglifyOptions: {
  82. output: {
  83. // Preserve license comments starting with an exclamation mark.
  84. comments: /^!/
  85. }
  86. }
  87. } )
  88. ]
  89. },
  90. plugins: [
  91. new MiniCssExtractPlugin( { filename: 'snippet.css' } ),
  92. new CKEditorWebpackPlugin( {
  93. language: config.language || 'en'
  94. } ),
  95. new webpack.BannerPlugin( {
  96. banner: bundler.getLicenseBanner(),
  97. raw: true
  98. } )
  99. ],
  100. // Configure the paths so building CKEditor 5 snippets work even if the script
  101. // is triggered from a directory outside ckeditor5 (e.g. multi-project case).
  102. resolve: {
  103. modules: getModuleResolvePaths()
  104. },
  105. resolveLoader: {
  106. modules: getModuleResolvePaths()
  107. },
  108. module: {
  109. rules: [
  110. {
  111. test: /\.svg$/,
  112. use: [ 'raw-loader' ]
  113. },
  114. {
  115. test: /\.css$/,
  116. use: [
  117. MiniCssExtractPlugin.loader,
  118. 'css-loader',
  119. {
  120. loader: 'postcss-loader',
  121. options: styles.getPostCssConfig( {
  122. themeImporter: {
  123. themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
  124. },
  125. minify: config.production
  126. } )
  127. }
  128. ]
  129. }
  130. ]
  131. }
  132. };
  133. }
  134. function runWebpack( webpackConfig ) {
  135. return new Promise( ( resolve, reject ) => {
  136. webpack( webpackConfig, ( err, stats ) => {
  137. if ( err ) {
  138. reject( err );
  139. } else if ( stats.hasErrors() ) {
  140. reject( new Error( stats.toString() ) );
  141. } else {
  142. resolve();
  143. }
  144. } );
  145. } );
  146. }
  147. function getModuleResolvePaths() {
  148. return [
  149. path.resolve( __dirname, '..', '..', 'node_modules' ),
  150. 'node_modules'
  151. ];
  152. }
  153. function readSnippetConfig( snippetSourcePath ) {
  154. const snippetSource = fs.readFileSync( snippetSourcePath ).toString();
  155. const configSourceMatch = snippetSource.match( /\n\/\* config ([\s\S]+?)\*\// );
  156. if ( !configSourceMatch ) {
  157. return {};
  158. }
  159. return JSON.parse( configSourceMatch[ 1 ] );
  160. }