piratepoet/webpack.config.js

444 lines
12 KiB
JavaScript
Raw Normal View History

const webpack = require('webpack');
const webpackManifestPlugin = require('webpack-manifest-plugin');
const webpackCleanPlugin = require('clean-webpack-plugin');
const webpackTerserPlugin = require('terser-webpack-plugin');
const webpackCopyPlugin = require('copy-webpack-plugin');
const path = require('path');
const globalPrefix = 'MailPoetLib';
const PRODUCTION_ENV = process.env.NODE_ENV === 'production';
const manifestSeed = {};
// Base config
const baseConfig = {
mode: PRODUCTION_ENV ? 'production' : 'development',
devtool: PRODUCTION_ENV ? undefined : 'eval-source-map',
cache: true,
context: __dirname,
2018-06-13 13:33:27 +01:00
watchOptions: {
aggregateTimeout: 300,
poll: true
},
optimization: {
minimizer: [
new webpackTerserPlugin({
terserOptions: {
// preserve identifier names for easier debugging & support
mangle: false,
},
2019-02-05 15:50:05 +03:00
parallel: false,
}),
],
},
output: {
path: path.join(__dirname, 'assets/dist/js'),
filename: (PRODUCTION_ENV) ? '[name].[hash:8].js' : '[name].js',
chunkFilename: (PRODUCTION_ENV) ? '[name].[hash:8].chunk.js' : '[name].chunk.js',
jsonpFunction: 'mailpoetJsonp'
},
resolve: {
2018-06-13 13:33:27 +01:00
modules: [
'node_modules',
'assets/js/src',
],
alias: {
'handlebars': 'handlebars/dist/handlebars.js',
'backbone.marionette': 'backbone.marionette/lib/backbone.marionette',
'backbone.supermodel$': 'backbone.supermodel/build/backbone.supermodel.js',
2016-01-04 17:02:46 +02:00
'sticky-kit': 'vendor/jquery.sticky-kit.js',
2015-08-26 18:24:43 +03:00
'interact$': 'interact.js/interact.js',
'spectrum$': 'spectrum-colorpicker/spectrum.js',
'wp-js-hooks': path.resolve(__dirname, 'assets/js/src/hooks.js'),
'blob$': 'blob-tmp/Blob.js',
2015-11-13 14:45:25 +02:00
'papaparse': 'papaparse/papaparse.min.js',
'html2canvas': 'html2canvas/dist/html2canvas.js',
'asyncqueue': 'vendor/jquery.asyncqueue.js',
'intro.js': 'intro.js/intro.js',
2015-08-26 18:24:43 +03:00
},
},
node: {
fs: 'empty'
},
plugins: [
new webpackCleanPlugin([
'./assets/dist/js/*',
]),
],
module: {
2018-06-13 13:33:27 +01:00
rules: [
{
2019-02-20 11:20:33 +01:00
test: /\.jsx?$/,
exclude: /(node_modules|src\/vendor)/,
2018-06-13 10:57:40 +01:00
loader: 'babel-loader',
},
{
test: /form_editor\.js$/,
loader: 'expose-loader?WysijaForm',
},
{
include: require.resolve('codemirror'),
loader: 'expose-loader?CodeMirror',
},
{
include: require.resolve('backbone'),
loader: 'expose-loader?Backbone',
},
{
include: require.resolve('underscore'),
loader: 'expose-loader?_',
},
2018-06-06 15:13:39 +01:00
{
include: require.resolve('react-tooltip'),
loader: 'expose-loader?' + globalPrefix + '.ReactTooltip',
},
{
include: require.resolve('react'),
loader: 'expose-loader?' + globalPrefix + '.React',
},
{
include: require.resolve('react-dom'),
loader: 'expose-loader?' + globalPrefix + '.ReactDOM',
},
{
2018-11-14 15:03:28 +01:00
include: require.resolve('react-router-dom'),
2018-06-13 13:33:27 +01:00
use: 'expose-loader?' + globalPrefix + '.ReactRouter',
},
{
include: require.resolve('react-string-replace'),
loader: 'expose-loader?' + globalPrefix + '.ReactStringReplace',
},
{
include: path.resolve(__dirname, 'assets/js/src/hooks.js'),
use: 'expose-loader?' + globalPrefix + '.Hooks',
},
{
test: /listing.jsx/i,
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.Listing',
'babel-loader'
],
},
{
test: /form.jsx/i,
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.Form',
'babel-loader'
]
},
{
2019-06-21 15:33:03 +01:00
include: path.resolve(__dirname, 'assets/js/src/newsletters/listings/utils.jsx'),
2018-06-13 13:33:27 +01:00
use: [
2019-06-21 15:33:03 +01:00
'expose-loader?' + globalPrefix + '.NewslettersListingsUtils',
2018-06-13 13:33:27 +01:00
'babel-loader',
]
},
2019-06-21 16:53:06 +01:00
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/listings/statistics.jsx'),
use: [
'expose-loader?' + globalPrefix + '.NewslettersListingsStatistics',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/listings/tabs.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.NewslettersListingsTabs',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/listings/heading.jsx'),
use: [
'expose-loader?' + globalPrefix + '.NewslettersListingsHeading',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/announcements/feature_announcement.jsx'),
use: [
'expose-loader?' + globalPrefix + '.FeatureAnnouncement',
'babel-loader',
]
},
2018-01-31 18:08:43 -05:00
{
include: path.resolve(__dirname, 'assets/js/src/form/fields/selection.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.FormFieldSelection',
'babel-loader',
]
2018-01-31 18:08:43 -05:00
},
{
include: path.resolve(__dirname, 'assets/js/src/form/fields/text.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.FormFieldText',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/scheduling/common.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.NewsletterSchedulingCommonOptions',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/badges/stats.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.StatsBadge',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/breadcrumb.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.NewsletterCreationBreadcrumb',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/types/automatic_emails/events_list.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.AutomaticEmailEventsList',
'babel-loader',
]
},
2019-05-15 15:09:14 +02:00
{
include: path.resolve(__dirname, 'assets/js/src/help-tooltip.jsx'),
use: [
'expose-loader?' + globalPrefix + '.HelpTooltip',
'babel-loader',
]
},
{
include: path.resolve(__dirname, 'assets/js/src/newsletters/types/automatic_emails/breadcrumb.jsx'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.AutomaticEmailsBreadcrumb',
'babel-loader',
]
},
{
include: /Blob.js$/,
loader: 'exports-loader?window.Blob',
},
{
test: /backbone.supermodel/,
loader: 'exports-loader?Backbone.SuperModel',
},
2015-08-26 18:24:43 +03:00
{
include: require.resolve('handlebars'),
loader: 'expose-loader?Handlebars',
},
{
include: /html2canvas.js$/,
loader: 'expose-loader?html2canvas',
},
{
include: require.resolve('velocity-animate'),
loader: 'imports-loader?jQuery=jquery',
},
{
include: require.resolve('classnames'),
2018-06-13 13:33:27 +01:00
use: [
'expose-loader?' + globalPrefix + '.ClassNames',
'babel-loader',
]
},
]
}
};
// Admin config
const adminConfig = {
name: 'admin',
entry: {
vendor: 'webpack_vendor_index.jsx',
mailpoet: 'webpack_mailpoet_index.jsx',
admin_vendor: [
'react',
'react-dom',
require.resolve('react-router-dom'),
'react-string-replace',
'prop-types',
'classnames',
2019-05-15 15:09:14 +02:00
'help-tooltip.jsx',
'form/form.jsx',
'listing/listing.jsx',
'newsletters/badges/stats.jsx',
'newsletters/breadcrumb.jsx',
'newsletters/listings/tabs.jsx',
2019-06-21 15:33:03 +01:00
'newsletters/listings/utils.jsx',
2019-06-21 16:53:06 +01:00
'newsletters/listings/statistics.jsx',
'newsletters/listings/heading.jsx',
'announcements/feature_announcement.jsx',
'announcements/free_plan_announcement.jsx',
'newsletters/types/automatic_emails/events_list.jsx',
'newsletters/types/automatic_emails/breadcrumb.jsx',
'newsletters/types/welcome/scheduling.jsx',
],
admin: 'webpack_admin_index.jsx',
form_editor: 'form_editor/webpack_index.jsx',
newsletter_editor: 'newsletter_editor/webpack_index.jsx',
},
plugins: [
...baseConfig.plugins,
new webpackCopyPlugin([
{
from: 'node_modules/tinymce/skins/lightgray',
to: 'skins/lightgray'
},
])
],
optimization: {
runtimeChunk: {
name: 'vendor',
},
splitChunks: {
cacheGroups: {
chunks: 'all',
default: false,
vendors: false,
vendor: {
name: 'vendor',
chunks: (chunk) => chunk.name === 'vendor',
priority: 1,
enforce: true,
},
admin_vendor_chunk: {
name: 'admin_vendor_chunk',
test: (module, chunks) => {
// add all modules from 'admin_vendor' entrypoint
if (chunks.some((chunk) => chunk.name === 'admin_vendor')) {
return true;
}
// add admin/form_editor/newsletter_editor shared modules
const filteredChunks = chunks.filter((chunk) => {
return ['admin', 'form_editor', 'newsletter_editor'].includes(chunk.name);
});
return filteredChunks.length > 1;
},
enforce: true,
chunks: (chunk) => ['admin_vendor', 'admin', 'form_editor', 'newsletter_editor'].includes(chunk.name),
priority: 0,
},
}
}
},
externals: {
'jquery': 'jQuery',
}
};
// Public config
const publicConfig = {
name: 'public',
entry: {
public: 'webpack_public_index.jsx',
},
plugins: [
...baseConfig.plugins,
// replace MailPoet definition with a smaller version for public
new webpack.NormalModuleReplacementPlugin(
/mailpoet\.js/,
'./mailpoet_public.js'
),
],
externals: {
'jquery': 'jQuery'
}
};
// Migrator config
const migratorConfig = {
New: Display the number of data to migrate Several fixes following the code review: - For styles we use Stylus. (http://stylus-lang.com/) In Stylus styles curly brackets and terminating semicolons are unnecessary. Indentation is sufficient to denote blocks of styles You can also use variables to avoid duplication (e.g. mentioning the same color multiple times) Vendor prefixes are not necessary. We use the Nib mixin for Stylus to handle those (http://tj.github.io/nib/) => DONE - `admin.js` bundle is included on all admin pages, so we need to ensure that migration fires only on the migration page and nowhere else. You can even create a separate bundle only for migration files, as they won't be necessary on other admin pages. => DONE - MP2MigratorAPI => MP2Migrator endpoint would be just as fine => DONE - For storing migration files, you can use the `Env::$temp_path` path => DONE - `proposeMigration()` the method name disagrees with the comment. Comment suggests it tests if migration can be or should be performed Method name suggests that it proposes doing the migration (to the user?) - not very clear => DONE: the new name is isMigrationNeeded() And not only does it test, it may also update the `mailpoet_migration_complete` option, which is confusing and is an unexpected side-effect. => DONE The migration class itself `MP2Migrator` shouldn't even care about _GET, _REQUEST or _POST arguments. It should only work with what is passed to it. => DONE - In views, please make sure all human-friendly texts use WP's gettext functions for translations (e.g. __('text')) => DONE
2017-04-18 19:15:12 +02:00
name: 'mp2migrator',
entry: {
mp2migrator: [
'mp2migrator.js'
]
},
externals: {
'jquery': 'jQuery',
'mailpoet': 'MailPoet'
New: Display the number of data to migrate Several fixes following the code review: - For styles we use Stylus. (http://stylus-lang.com/) In Stylus styles curly brackets and terminating semicolons are unnecessary. Indentation is sufficient to denote blocks of styles You can also use variables to avoid duplication (e.g. mentioning the same color multiple times) Vendor prefixes are not necessary. We use the Nib mixin for Stylus to handle those (http://tj.github.io/nib/) => DONE - `admin.js` bundle is included on all admin pages, so we need to ensure that migration fires only on the migration page and nowhere else. You can even create a separate bundle only for migration files, as they won't be necessary on other admin pages. => DONE - MP2MigratorAPI => MP2Migrator endpoint would be just as fine => DONE - For storing migration files, you can use the `Env::$temp_path` path => DONE - `proposeMigration()` the method name disagrees with the comment. Comment suggests it tests if migration can be or should be performed Method name suggests that it proposes doing the migration (to the user?) - not very clear => DONE: the new name is isMigrationNeeded() And not only does it test, it may also update the `mailpoet_migration_complete` option, which is confusing and is an unexpected side-effect. => DONE The migration class itself `MP2Migrator` shouldn't even care about _GET, _REQUEST or _POST arguments. It should only work with what is passed to it. => DONE - In views, please make sure all human-friendly texts use WP's gettext functions for translations (e.g. __('text')) => DONE
2017-04-18 19:15:12 +02:00
}
};
// Test config
const testConfig = {
name: 'test',
entry: {
vendor: 'webpack_vendor_index.jsx',
2015-08-26 18:24:43 +03:00
testNewsletterEditor: [
'webpack_mailpoet_index.jsx',
'newsletter_editor/webpack_index.jsx',
2015-08-26 18:24:43 +03:00
'components/config.spec.js',
'components/content.spec.js',
'components/heading.spec.js',
'components/history.spec.js',
2015-08-26 18:24:43 +03:00
'components/save.spec.js',
'components/sidebar.spec.js',
'components/styles.spec.js',
'components/communication.spec.js',
2015-08-26 18:24:43 +03:00
'blocks/automatedLatestContentLayout.spec.js',
2015-08-26 18:24:43 +03:00
'blocks/button.spec.js',
'blocks/container.spec.js',
'blocks/divider.spec.js',
'blocks/footer.spec.js',
'blocks/header.spec.js',
'blocks/image.spec.js',
'blocks/posts.spec.js',
'blocks/products.spec.js',
2015-08-26 18:24:43 +03:00
'blocks/social.spec.js',
'blocks/spacer.spec.js',
'blocks/text.spec.js',
],
},
output: {
2018-06-13 13:33:27 +01:00
path: path.join(__dirname, 'tests/javascript/testBundles'),
filename: '[name].js',
},
plugins: [
...baseConfig.plugins,
// replace MailPoet definition with a smaller version for public
new webpack.NormalModuleReplacementPlugin(
/mailpoet\.js/,
'./mailpoet_tests.js'
),
],
resolve: {
2018-06-13 13:33:27 +01:00
modules: [
'node_modules',
'assets/js/src',
'tests/javascript/newsletter_editor'
2015-08-26 18:24:43 +03:00
],
alias: {
2016-01-04 17:02:46 +02:00
'sticky-kit': 'vendor/jquery.sticky-kit.js',
2015-08-26 18:24:43 +03:00
'backbone.marionette': 'backbone.marionette/lib/backbone.marionette',
'backbone.supermodel$': 'backbone.supermodel/build/backbone.supermodel.js',
'blob$': 'blob-tmp/Blob.js',
'wp-js-hooks': path.resolve(__dirname, 'assets/js/src/hooks.js'),
2015-08-26 18:24:43 +03:00
},
},
externals: {
'jquery': 'jQuery',
'interact': 'interact',
'spectrum': 'spectrum',
}
};
module.exports = [adminConfig, publicConfig, migratorConfig, testConfig].map((config) => {
if (config.name !== 'test') {
config.plugins = config.plugins || [];
config.plugins.push(
new webpackManifestPlugin({
// create single manifest file for all Webpack configs
seed: manifestSeed,
})
);
}
return Object.assign({}, baseConfig, config);
});