universal-webpack
Version:
Isomorphic Webpack
163 lines (129 loc) • 7.51 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
import util from 'util';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import chunks_plugin from './chunksPlugin.js';
import { clone, starts_with } from './helpers.js';
import { find_loader, get_style_rules, normalize_configuration_rule_loaders } from './loaders.js';
export default function client_configuration(webpack_configuration, settings) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var configuration = clone(webpack_configuration);
configuration.plugins = configuration.plugins || [];
configuration.plugins.push( // Add chunk filename info plugin
//
// Writes client-side build chunks filename info
// for later use inside server-side rendering code
// (`<script src=.../>` and `<link rel="style" href=.../>` tags)
//
// Cloning Webpack configuration here
// because `webpack-dev-server` seems to alter it
// by changing the already predefined `.output.path`.
//
new chunks_plugin(clone(configuration), {
silent: settings.silent,
chunk_info_filename: settings.chunk_info_filename
})); // CSS bundle filename (if specified)
var css_bundle = options.css_bundle || options.cssBundle;
var css_bundle_filename = '[name]-[contenthash].css';
if (typeof css_bundle === 'string') {
css_bundle_filename = css_bundle;
} // If it's a client-side development webpack build,
// and CSS bundle extraction is enabled,
// then extract all CSS styles into a file.
// (without removing them from the code)
if (options.development && css_bundle) {
// Extract styles into a file
// (without removing them from the code in this case).
//
// It copies contents of each `require("style.css")`
// into one big CSS file on disk
// which will be later read on the server-side
// and inserted into `<head><style></style></head>`,
// so that in development mode there's no
// "flash of unstyled content" on page reload.
//
var extract_css_plugin = create_extract_css_plugin(css_bundle_filename); // Normalize `modules.rules` loaders.
normalize_configuration_rule_loaders(configuration); // Find all rules using `style-loader`
// and replace `style-loader` with `mini-css-extract-plugin` loader.
var _iterator = _createForOfIteratorHelper(get_style_rules(configuration)),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var rule = _step.value;
var style_loader = find_loader(rule, 'style-loader');
var before_style_loader = rule.use.slice(0, rule.use.indexOf(style_loader));
var after_style_loader = rule.use.slice(rule.use.indexOf(style_loader) + 1);
if (before_style_loader.length > 0) {
throw new Error('No loaders can preceed `style-loader` in a Webpack module rule.', util.inspect(rule));
}
rule.use = generate_extract_css_loaders(after_style_loader, options.development);
} // Add `mini-css-extract-plugin` to the list of plugins.
// It will extract all CSS into a file
// (without removing it from the code in this case)
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
configuration.plugins.push(extract_css_plugin);
} // Use `mini-css-extract-plugin`
// to extract all CSS into a separate file
// (in production)
if (options.development === false && css_bundle !== false) {
// Extract styles into a file
// (removing them from the code in this case).
//
// It moves contents of each `require("style.css")`
// into one big CSS file on disk
// which will be later read on the server-side
// and inserted into `<head><style></style></head>`.
//
var _extract_css_plugin = create_extract_css_plugin(css_bundle_filename); // Normalize `modules.rules` loaders.
normalize_configuration_rule_loaders(configuration); // Find module loaders with `style-loader`,
// and set those module loaders to `mini-css-extract-plugin` loader
var _iterator2 = _createForOfIteratorHelper(get_style_rules(configuration)),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _rule = _step2.value;
var _style_loader = find_loader(_rule, 'style-loader'); // const style_loader_and_before = rule.use.slice(0, rule.use.indexOf(style_loader) + 1)
var _before_style_loader = _rule.use.slice(0, _rule.use.indexOf(_style_loader));
var _after_style_loader = _rule.use.slice(_rule.use.indexOf(_style_loader) + 1);
if (_before_style_loader.length > 0) {
throw new Error('No loaders can preceed `style-loader` in a Webpack module rule.', util.inspect(_rule));
}
_rule.use = generate_extract_css_loaders(_after_style_loader, options.development);
} // Add `mini-css-extract-plugin` to the list of plugins.
// It will extract all CSS into a file
// (removing it from the code in this case)
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
configuration.plugins.push(_extract_css_plugin);
} // Done
return configuration;
}
/**
* Creates an instance of `mini-css-extract-plugin` for extracting styles in a file.
*/
function create_extract_css_plugin(css_bundle_filename) {
return new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: css_bundle_filename
});
}
/**
* Generates rule.use loaders for extracting styles in a file.
* For `mini-css-extract-plugin`.
*/
function generate_extract_css_loaders(after_style_loader, development) {
return [{
loader: MiniCssExtractPlugin.loader
}].concat(_toConsumableArray(after_style_loader));
}
//# sourceMappingURL=clientConfiguration.js.map