Webpack ã使ã£ã¦ SCSS ãã³ã³ãã¤ã«ãã¦ããããã¸ã§ã¯ãã§ä¹ ã ã« build ãããä¸è¨ã®ãããªã¨ã©ã¼ãçºçãã
$ npx webpack --config ./webpack.config.js --mode production ⦠webpack-fix-style-only-entries: removing js from style only module: js/css.js (node:53965) [DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE] DeprecationWarning: Chunk.hasEntryModule: Use new ChunkGraph API (Use `node --trace-deprecation ...` to show where the warning was created) (node:53965) [DEP_WEBPACK_CHUNK_ENTRY_MODULE] DeprecationWarning: Chunk.entryModule: Use new ChunkGraph API (node:53965) [DEP_WEBPACK_MODULE_INDEX] DeprecationWarning: Module.index: Use new ModuleGraph API ... ERROR in [entry] [initial] js/main.js Maximum call stack size exceeded RangeError: Maximum call stack size exceeded at ModuleGraph.getModuleGraphForModule (/path/to/node_modules/webpack/lib/ModuleGraph.js:842:32) ⦠webpack 5.93.0 compiled with 1 error in 3219 ms
ãªãã§ãå·¥è¤â¦
version
- webpack
^5.93.0
webpack.config
åè
// plugins const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries'); const CompressionPlugin = require('compression-webpack-plugin'); // rules const tsRules = require('./ts.rules'); const cssRules = require('./css.rules'); module.exports = (env, argv) => { const IS_PRODUCTION = argv.mode === 'production'; return { mode: IS_PRODUCTION ? 'production' : 'development', devtool: !IS_PRODUCTION ? 'eval-source-map' : false, // https://webpack.js.org/configuration/devtool/ entry: { main: `${DIR}/src/main.ts`, css: `${DIR}/scss/styles.scss`, }, output: { path: OUTPUT_DIR, filename: 'js/[name].js', }, plugins: [ new FixStyleOnlyEntriesPlugin(), // /js/css.js ãåºåããªã IS_PRODUCTION && new CompressionPlugin({ test: /\.(css|scss|js)$/, algorithm: 'gzip', filename: '[path][base].gz', }), // gzip å production mode æã®ã¿ new MiniCssExtractPlugin({ filename: 'css/styles.css' }), // `${OUTPUT_DIR}/css/styles.css` ã¨ãã¦åºå ].filter(Boolean), resolve: { extensions: ['.ts', '.js'] }, module: { rules: [ tsRules(), cssRules() ] }, }; };
webpack-fix-style-only-entries ãã©ã°ã¤ã³ãåå ã ã£ã
CSS ã webpack 㧠build ããéã«ç©ºã® css.js
ãã¡ã¤ã«ãä½ããã¦ãã¾ãã®ãé²ãããã®ãã©ã°ã¤ã³ã production
mode ã§ãã«ãããéã®ã¿ã¨ã©ã¼ã«ãªã£ã¦ããã£ã®ãåå ã ã£ã
â ï¸ The current package version is not compatible with webpack 5. There is a fork here that is compatible: https://github.com/webdiscus/webpack-remove-empty-scripts
cf. GitHub fqborges/webpack-fix-style-only-entries
Webpack 5 ã«é©åãã¦ãªãã®ã§ãFork ãã¦ä½ããã webpack-remove-empty-scripts ã使ã£ã¦ããã¨ã®ãã¨ã
â» webpack-remove-empty-scripts
㯠webpack 5 ã«é©åãã¦ãã¦ããããwebpack 4 ã«ã¯é©åãã¦ç¡ã 4 ã®å ´åã¯å¼ãç¶ã webpack-fix-style-only-entries
ã使ãã®ãè¯ããã
â ï¸ webpack-fix-style-only-entries ã webpack-remove-empty-scripts ã«ç½®ãæãã (webpack 5)
åç´ã«ãã©ã°ã¤ã³ãç½®ãæããã ãã§ãåãç¨ã«åä½ãã
$ npm uninstall webpack-fix-style-only-entries
$ npm i -D webpack-remove-empty-scripts
webpack.config
ãä¿®æ£ãã
// plugins const MiniCssExtractPlugin = require('mini-css-extract-plugin'); - const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries'); + const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts'); const CompressionPlugin = require('compression-webpack-plugin'); ⦠module.exports = (env, argv) => { const IS_PRODUCTION = argv.mode === 'production'; return { mode: IS_PRODUCTION ? 'production' : 'development', ... }, plugins: [ - new FixStyleOnlyEntriesPlugin(), // /js/css.js ãåºåããªã + new RemoveEmptyScriptsPlugin(), IS_PRODUCTION && new CompressionPlugin({
ãã©ã°ã¤ã³ãå¤æ´ã㦠production mode ã§ã®ãã«ã (npx webpack --config ./webpack.config.js --mode production
) ãã¨ã©ã¼ã«ãªããå®äºããã° OK
ç¡äºåä½ããããã«ãªã£ã¦ããã£ã
ããã à´¦àµà´¦à´¿á¢- Ì«-á¢â
[åè]
- GitHub - fqborges/webpack-fix-style-only-entries: Webpack plugin to solve the problem of having a style only entry (css/sass/less) generating an extra js file.
- GitHub - webdiscus/webpack-remove-empty-scripts: Webpack plugin to remove empty scripts generated by usage only a style without JS in entry.
- webpack 5でCSSを別ファイルに書き出し&minifyをする方法 | ブログ | UiDev