Skip to content

Instantly share code, notes, and snippets.

@oleq
Created October 16, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleq/1454e7ecc042af25291fbfa028e86f67 to your computer and use it in GitHub Desktop.
Save oleq/1454e7ecc042af25291fbfa028e86f67 to your computer and use it in GitHub Desktop.
A PostCSS theme loader for CKEditor 5
const fs = require( 'fs' );
const path = require( 'path' );
const postcss = require( 'postcss' );
const postcssImport = require( 'postcss-import' );
const postcssCssnext = require( 'postcss-cssnext' );
module.exports = postcss.plugin( 'postcss-ck-theme-importer', ( options ) => {
const themeName = options.themePath.split( '/' ).slice( -1 );
console.log( `Using theme "${ themeName }".` );
return ( root, result ) => {
const fileName = path.basename( root.source.input.file );
const relativeFilePath = path.relative( __dirname, root.source.input.file );
const themeFile = path.resolve( __dirname, options.themePath, fileName );
if ( fs.existsSync( themeFile ) ) {
const relativeThemeFilePath = path.relative( __dirname, themeFile );
console.log( `Found a corresponding theme file for ${ relativeFilePath }: ${ relativeThemeFilePath }.` );
return new Promise( ( resolve, reject ) => {
fs.readFile( themeFile, 'utf8', ( err, data ) => {
const processor = postcss( {
plugins: [
postcssImport()
]
} );
return processor.process( data, { from: themeFile } )
.then( result => {
root.append( result.css );
root.append( '\n' );
resolve();
} );
} );
} );
} else {
console.log( `Theme file for ${ relativeFilePath } not found.` );
}
};
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment