PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] How to integrate Tinymce with Symfony encore?

 January 21, 2022     symfony, tinymce, webpack-encore, yarnpkg     No comments   

Issue

I have a Symfony4 project using flex and encore. I would like to add tinymce.

So I add tinymce project:

$ yarn add tinymce

I edited my app.js file:

require('../css/app.scss');

// Import TinyMCE
import tinymce from 'tinymce/tinymce';

// A theme is also required
import 'tinymce/themes/modern/theme';

// Any plugins you want to use has to be imported
import 'tinymce/plugins/paste';
import 'tinymce/plugins/link';

// Initialize the app
tinymce.init({
    selector: 'textarea',

    plugins: ['paste', 'link']
});

I compiled:

$ yarn run encore dev

Compilation is successfull:

Running webpack ...

 DONE  Compiled successfully in 17600ms                                                                                                                                                                                                             

 I  8 files written to public\build
Done in 20.23s.

My textareas are replaced by a blank page.

I found the solution in documentation and it works fine when I copy the node_modules/tinymce/skins directory to /public/build/skins. But I still have to do it after each yarn compilation

Is there a way to automatically copy this node_modules/tinymce/skins directory to /public/build/skins? IS it possible to update the webpack.config.js to do it?

PS: I read some recommandations with the webpack-loader, but I don't understand what I have to do.


Solution

OPTION1: RECOMMENDED: the buit-in copyFiles function

Use Encore's built-in copyFiles function.

var Encore = require('@symfony/webpack-encore');

//...

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')

    // copy tinymce's skin files
    .copyFiles({
        from: 'node_modules/tinymce/skins',
        to: 'skins/[path]/[name].[ext]'
    })

Encore's API reference.

OPTION2 : The copy webpack plugin

I added the copy webpack plugin

yarn add copy-webpack-plugin --dev

I edited my webpack.config.js to only add 4 lines:

var Encore = require('@symfony/webpack-encore');

//DECLARATION OF THE NEW PLUGIN
var CopyWebpackPlugin = require('copy-webpack-plugin');

Encore
// the project directory where all compiled assets will be stored
    .setOutputPath('public/build/')

    // the public path used by the web server to access the previous directory
    .setPublicPath('/build')

    // will create public/build/admin/app.js and public/build/admin/app.css
    .addEntry('admin', './assets/js/app.js')

    //Some project lines
    //...
    //...
    
    //I call the plugin with its new syntax (since 3.11)
    .addPlugin(new CopyWebpackPlugin({
        patterns: [
            // Copy the skins from tinymce to the build/skins directory
            { from: 'node_modules/tinymce/skins', to: 'skins' },
        ],
    }))

    //Some project lines
    //...
    //...
;

// export the final configuration
module.exports = Encore.getWebpackConfig();


Answered By - Alexandre Tranchant
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing