| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* eslint-env node */
- const path = require( 'path' );
- const fs = require( 'fs' );
- const webpack = require( 'webpack' );
- const { bundler } = require( '@ckeditor/ckeditor5-dev-utils' );
- const CKEditorWebpackPlugin = require( '@ckeditor/ckeditor5-dev-webpack-plugin' );
- // const BabiliPlugin = require( 'babili-webpack-plugin' );
- module.exports = function snippetAdapter( data ) {
- const snippetConfig = readSnippetConfig( data.snippetSource.js );
- const webpackConfig = getWebpackConfig( {
- entry: data.snippetSource.js,
- outputPath: path.join( data.outputPath, data.snippetPath ),
- language: snippetConfig.language
- } );
- return runWebpack( webpackConfig )
- .then( () => {
- return {
- html: generateSnippetHtml( {
- htmlPath: data.snippetSource.html,
- scriptPath: path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
- } )
- };
- } );
- };
- function getWebpackConfig( config ) {
- return {
- devtool: 'source-map',
- entry: config.entry,
- output: {
- path: config.outputPath,
- filename: 'snippet.js'
- },
- plugins: [
- new CKEditorWebpackPlugin( {
- languages: [ config.language || 'en' ]
- } ),
- // new BabiliPlugin( null, {
- // comments: false
- // } ),
- new webpack.BannerPlugin( {
- banner: bundler.getLicenseBanner(),
- raw: true
- } )
- ],
- // Configure the paths so building CKEditor 5 snippets work even if the script
- // is triggered from a directory outside ckeditor5 (e.g. multi-project case).
- resolve: {
- modules: getModuleResolvePaths()
- },
- resolveLoader: {
- modules: getModuleResolvePaths()
- },
- module: {
- rules: [
- {
- test: /\.svg$/,
- use: [ 'raw-loader' ]
- },
- {
- test: /\.scss$/,
- use: [
- 'style-loader',
- {
- loader: 'css-loader',
- options: {
- minimize: true
- }
- },
- 'sass-loader'
- ]
- }
- ]
- }
- };
- }
- function runWebpack( webpackConfig ) {
- return new Promise( ( resolve, reject ) => {
- webpack( webpackConfig, ( err, stats ) => {
- if ( err ) {
- reject( err );
- } else if ( stats.hasErrors() ) {
- reject( new Error( stats.toString() ) );
- } else {
- resolve();
- }
- } );
- } );
- }
- function generateSnippetHtml( data ) {
- let html = fs.readFileSync( data.htmlPath );
- html += `<script src="${ data.scriptPath }"></script>`;
- return html;
- }
- function getModuleResolvePaths() {
- return [
- path.resolve( __dirname, '..', '..', '..', 'node_modules' ),
- 'node_modules'
- ];
- }
- function readSnippetConfig( snippetSourcePath ) {
- const snippetSource = fs.readFileSync( snippetSourcePath ).toString();
- const configSourceMatch = snippetSource.match( /\n\/\* config ([\s\S]+?)\*\// );
- if ( !configSourceMatch ) {
- return {};
- }
- return JSON.parse( configSourceMatch[ 1 ] );
- }
|