themeimporter.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* eslint-env node */
  7. const fs = require( 'fs' );
  8. const path = require( 'path' );
  9. const postcss = require( 'postcss' );
  10. const postCssImport = require( 'postcss-import' );
  11. module.exports = postcss.plugin( 'postcss-ckeditor-theme-importer', options => {
  12. const themeName = options.themePath.split( '/' ).slice( -1 );
  13. console.log( `Using theme "${ themeName }".` );
  14. return root => {
  15. const fileName = path.basename( root.source.input.file );
  16. const packageName = getPackageName( root.source.input.file );
  17. const relativeFilePath = path.relative( __dirname, root.source.input.file );
  18. const themeFile = path.resolve( __dirname, options.themePath, 'theme', packageName, fileName );
  19. if ( fs.existsSync( themeFile ) ) {
  20. console.log( `Found a corresponding theme file for ${ relativeFilePath }. Including.` );
  21. return new Promise( resolve => {
  22. fs.readFile( themeFile, 'utf8', ( err, data ) => {
  23. const processor = postcss( {
  24. plugins: [
  25. postCssImport()
  26. ]
  27. } );
  28. return processor.process( data, { from: themeFile } )
  29. .then( result => {
  30. root.append( result.css );
  31. root.append( '\n' );
  32. resolve();
  33. } );
  34. } );
  35. } );
  36. } else {
  37. console.log( `Theme file for ${ relativeFilePath } not found in theme "${ themeName }".` );
  38. }
  39. };
  40. } );
  41. function getPackageName( path ) {
  42. const match = path.match( /ckeditor5-[^/]+/ );
  43. if ( match ) {
  44. return match[ 0 ];
  45. } else {
  46. return null;
  47. }
  48. }
  49. // function CKThemeLogger() {
  50. // return ( root ) => {
  51. // root.prepend( `/* ${ root.source.input.file } */` );
  52. // };
  53. // }
  54. // const CKRaiseSpecificity = postcss.plugin( 'postcss-ck-specificity', ( opts ) => {
  55. // opts = opts || {};
  56. // return ( root, result ) => {
  57. // root.walkRules( rule => {
  58. // if ( rule.selector.match( /^\.ck-/g ) ) {
  59. // rule.selector = 'body ' + rule.selector;
  60. // }
  61. // } );
  62. // };
  63. // } );