snippetadapter.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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. let sassImportPath;
  21. if ( snippetConfig.sassImportPath ) {
  22. sassImportPath = path.join( path.dirname( data.snippetSource.js ), snippetConfig.sassImportPath );
  23. }
  24. const webpackConfig = getWebpackConfig( {
  25. entry: data.snippetSource.js,
  26. outputPath,
  27. language: snippetConfig.language,
  28. minify: data.options.production,
  29. sassImportPath
  30. } );
  31. let promise;
  32. // See #530.
  33. if ( webpackProcesses.has( outputPath ) ) {
  34. promise = webpackProcesses.get( outputPath );
  35. } else {
  36. promise = runWebpack( webpackConfig );
  37. webpackProcesses.set( outputPath, promise );
  38. }
  39. return promise
  40. .then( () => {
  41. const wasCSSGenerated = fs.existsSync( path.join( outputPath, 'snippet.css' ) );
  42. const cssFiles = [
  43. path.join( data.basePath, 'assets', 'snippet-styles.css' )
  44. ];
  45. // CSS may not be generated by Webpack if a snippet's JS file didn't import any SCSS files.
  46. if ( wasCSSGenerated ) {
  47. cssFiles.unshift( path.join( data.relativeOutputPath, data.snippetPath, 'snippet.css' ) );
  48. }
  49. return {
  50. html: fs.readFileSync( data.snippetSource.html ),
  51. assets: {
  52. js: [
  53. path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
  54. ],
  55. css: cssFiles
  56. }
  57. };
  58. } );
  59. };
  60. function getWebpackConfig( config ) {
  61. const plugins = [
  62. new ExtractTextPlugin( 'snippet.css' ),
  63. new CKEditorWebpackPlugin( {
  64. languages: [ config.language || 'en' ]
  65. } ),
  66. new webpack.BannerPlugin( {
  67. banner: bundler.getLicenseBanner(),
  68. raw: true
  69. } )
  70. ];
  71. if ( config.minify ) {
  72. plugins.push(
  73. new BabelMinifyPlugin(
  74. {
  75. // Temporary workaround for https://github.com/ckeditor/ckeditor5/issues/542.
  76. mangle: false
  77. },
  78. {
  79. comments: false
  80. }
  81. )
  82. );
  83. }
  84. return {
  85. devtool: 'source-map',
  86. entry: config.entry,
  87. output: {
  88. path: config.outputPath,
  89. filename: 'snippet.js'
  90. },
  91. plugins,
  92. // Configure the paths so building CKEditor 5 snippets work even if the script
  93. // is triggered from a directory outside ckeditor5 (e.g. multi-project case).
  94. resolve: {
  95. modules: getModuleResolvePaths()
  96. },
  97. resolveLoader: {
  98. modules: getModuleResolvePaths()
  99. },
  100. module: {
  101. rules: [
  102. {
  103. test: /\.svg$/,
  104. use: [ 'raw-loader' ]
  105. },
  106. {
  107. test: /\.scss$/,
  108. use: ExtractTextPlugin.extract( {
  109. fallback: 'style-loader',
  110. use: [
  111. {
  112. loader: 'css-loader',
  113. options: {
  114. minimize: config.minify
  115. }
  116. },
  117. {
  118. loader: 'sass-loader',
  119. options: {
  120. data: config.sassImportPath ? `@import '${ config.sassImportPath }';` : ''
  121. }
  122. }
  123. ]
  124. } )
  125. }
  126. ]
  127. }
  128. };
  129. }
  130. function runWebpack( webpackConfig ) {
  131. return new Promise( ( resolve, reject ) => {
  132. webpack( webpackConfig, ( err, stats ) => {
  133. if ( err ) {
  134. reject( err );
  135. } else if ( stats.hasErrors() ) {
  136. reject( new Error( stats.toString() ) );
  137. } else {
  138. resolve();
  139. }
  140. } );
  141. } );
  142. }
  143. function getModuleResolvePaths() {
  144. return [
  145. path.resolve( __dirname, '..', '..', '..', 'node_modules' ),
  146. 'node_modules'
  147. ];
  148. }
  149. function readSnippetConfig( snippetSourcePath ) {
  150. const snippetSource = fs.readFileSync( snippetSourcePath ).toString();
  151. const configSourceMatch = snippetSource.match( /\n\/\* config ([\s\S]+?)\*\// );
  152. if ( !configSourceMatch ) {
  153. return {};
  154. }
  155. return JSON.parse( configSourceMatch[ 1 ] );
  156. }