Skip to content

Commit

Permalink
Use mini-css-extract-plugin@^0.6.0 and embed webpack-manifest-plugin …
Browse files Browse the repository at this point in the history
…into Encore
  • Loading branch information
Lyrkan committed Apr 14, 2019
1 parent ba08baf commit 9fe1632
Show file tree
Hide file tree
Showing 17 changed files with 516 additions and 63 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/webpack/webpack-manifest-plugin.js
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,34 @@ class Encore {
return this;
}

/**
* Configure the mini-css-extract-plugin.
*
* https://github.com/webpack-contrib/mini-css-extract-plugin#configuration
*
* ```
* Encore.configureMiniCssExtractPlugin(
* function(loaderConfig) {
* // change the loader's config
* // loaderConfig.reloadAll = true;
* },
* function(pluginConfig) {
* // change the plugin's config
* // pluginConfig.chunkFilename = '[id].css';
* }
* );
* ```
*
* @param {function} loaderOptionsCallback
* @param {function} pluginOptionsCallback
* @returns {Encore}
*/
configureMiniCssExtractPlugin(loaderOptionsCallback, pluginOptionsCallback = () => {}) {
webpackConfig.configureMiniCssExtractPlugin(loaderOptionsCallback, pluginOptionsCallback);

return this;
}

/**
* If enabled, the react preset is added to Babel.
*
Expand Down
15 changes: 15 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class WebpackConfig {
this.eslintLoaderOptionsCallback = () => {};
this.tsConfigurationCallback = () => {};
this.handlebarsConfigurationCallback = () => {};
this.miniCssExtractLoaderConfigurationCallback = () => {};
this.miniCssExtractPluginConfigurationCallback = () => {};
this.loaderConfigurationCallbacks = {
javascript: () => {},
css: () => {},
Expand Down Expand Up @@ -453,6 +455,19 @@ class WebpackConfig {
this.cssLoaderConfigurationCallback = callback;
}

configureMiniCssExtractPlugin(loaderOptionsCallback, pluginOptionsCallback = () => {}) {
if (typeof loaderOptionsCallback !== 'function') {
throw new Error('Argument 1 to configureMiniCssExtractPluginLoader() must be a callback function.');
}

if (typeof pluginOptionsCallback !== 'function') {
throw new Error('Argument 2 to configureMiniCssExtractPluginLoader() must be a callback function.');
}

this.miniCssExtractLoaderConfigurationCallback = loaderOptionsCallback;
this.miniCssExtractPluginConfigurationCallback = pluginOptionsCallback;
}

enableSingleRuntimeChunk() {
this.shouldUseSingleRuntimeChunk = true;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,7 @@ class ConfigGenerator {
buildPluginsConfig() {
const plugins = [];

if (this.webpackConfig.extractCss) {
miniCssExtractPluginUtil(plugins, this.webpackConfig);
}
miniCssExtractPluginUtil(plugins, this.webpackConfig);

// register the pure-style entries that should be deleted
deleteUnusedEntriesPluginUtil(plugins, this.webpackConfig);
Expand Down
14 changes: 13 additions & 1 deletion lib/loaders/css-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const applyOptionsCallback = require('../utils/apply-options-callback');

module.exports = {
/**
Expand All @@ -32,6 +33,17 @@ module.exports = {
}, ...loaders];
}

return [MiniCssExtractPlugin.loader, ...loaders];
// Default options
const options = {
hmr: webpackConfig.useHotModuleReplacementPlugin(),
};

return [{
loader: MiniCssExtractPlugin.loader,
options: applyOptionsCallback(
webpackConfig.miniCssExtractLoaderConfigurationCallback,
options
),
}, ...loaders];
}
};
2 changes: 1 addition & 1 deletion lib/plugins/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
const ManifestPlugin = require('webpack-manifest-plugin');
const ManifestPlugin = require('../webpack/webpack-manifest-plugin');
const PluginPriorities = require('./plugin-priorities');
const applyOptionsCallback = require('../utils/apply-options-callback');
const sharedEntryTmpName = require('../utils/sharedEntryTmpName');
Expand Down
13 changes: 12 additions & 1 deletion lib/plugins/mini-css-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
const WebpackConfig = require('../WebpackConfig'); //eslint-disable-line no-unused-vars
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const PluginPriorities = require('./plugin-priorities');
const applyOptionsCallback = require('../utils/apply-options-callback');

/**
* @param {Array} plugins
* @param {WebpackConfig} webpackConfig
* @return {void}
*/
module.exports = function(plugins, webpackConfig) {
// Don't add the plugin if CSS extraction is disabled
if (!webpackConfig.extractCss) {
return;
}

// Default filename can be overridden using Encore.configureFilenames({ css: '...' })
let filename = webpackConfig.useVersioning ? '[name].[contenthash:8].css' : '[name].css';
// the chunk filename should use [id], not [name]. But, due
Expand All @@ -45,7 +51,12 @@ module.exports = function(plugins, webpackConfig) {
};

plugins.push({
plugin: new MiniCssExtractPlugin(miniCssPluginOptions),
plugin: new MiniCssExtractPlugin(
applyOptionsCallback(
webpackConfig.miniCssExtractPluginConfigurationCallback,
miniCssPluginOptions
)
),
priority: PluginPriorities.MiniCssExtractPlugin
});
};
Loading

0 comments on commit 9fe1632

Please sign in to comment.