Created
October 16, 2017 09:30
-
-
Save oleq/1454e7ecc042af25291fbfa028e86f67 to your computer and use it in GitHub Desktop.
A PostCSS theme loader for CKEditor 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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