Как настроить веб-пакет для моих блоков Гутенберга, чтобы извлечь несколько файлов CSS и связать их в соответствии с именем таблиц стилей.
Зак Гордон использовал для этого плагин Extract Text с Webpack 3, и это сработало как шарм. Но с webpack 4 мне пришлось переключиться на плагин mini-css-extract-plugin, в котором я больше не могу заставить это работать.
См. Мои текущие настройки ниже, чтобы вы могли видеть, что я пытаюсь сделать.
Это папка моего проекта:
Plugin folder
|-- [src]
| |-- [block1]
| | |-- block1.js
| | |-- style.scss
| | `-- editor.scss
| |-- [block2]
| | |-- block2.js
| | |-- style.scss
| | `-- editor.scss
| `-- index.js
`-- [build]
|-- index.js
|-- style.build.css
`-- editor.build.css
В block1.js / block2.js:
import './style.scss'
import './editor.scss'
В index.js:
import './block1'
import './block2'
В webpack.config.js:
const defaultConfig = require("./node_modules/@wordpress/scripts/config/webpack.config");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
...defaultConfig,
optimization: {
...defaultConfig.optimization,
splitChunks: {
cacheGroups: {
style: {
name: 'style',
test: /style\.s?css$/,
chunks: 'all',
enforce: true,
},
editor: {
name: 'editor',
test: /editor\.s?css$/,
chunks: 'all',
enforce: true,
},
},
},
},
plugins: [
...defaultConfig.plugins,
new MiniCssExtractPlugin({
filename: 'blocks.[name].build.css'
}),
],
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
{
test: /\.s?css$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader'
],
},
]
},
};
Ожидаемый результат:
[build]
|-- blocks.editor.build.css
|-- index.js
|-- blocks.style.build.css
Токовый выход:
[build]
|-- blocks.editor.build.css
|-- editor.js
|-- index.js
|-- blocks.style.build.css
|-- style.js
`-- (...and indentical map files)
Текущая установка выкладывает два лишних js-файла, которые мне не нужны (style.js / editor.js), но большая проблема заключается в том, что блок также не загружается в Wordpress. Он загружается, когда я не использую splitChunks, но тогда все CSS объединены в один файл ... а мне нужны два.
Сравнение:
index.js без splitChunks:
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/index.js");
/******/ })
index.js с splitChunks:
/******/ // add entry module to deferred list
/******/ deferredModules.push(["./src/index.js","editor","style"]);
/******/ // run deferred modules when ready
/******/ return checkDeferredModules();
/******/ })