Skip to content

Commit

Permalink
fix: in dev mode imports SCSS in JS when in the same file is inlined …
Browse files Browse the repository at this point in the history
…another SCSS file via `?inline`, #102
  • Loading branch information
webdiscus committed Aug 9, 2024
1 parent 0f6ebdf commit 2cb4dca
Show file tree
Hide file tree
Showing 22 changed files with 305 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 3.17.3 (2024-08-09)

- fix: in dev mode imports SCSS in JS when in the same file is inlined another SCSS file via `?inline` query, #102

## 3.17.2 (2024-08-08)

- fix: error when `integrity` option is enabled but no template defined in entry, #107
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-bundler-webpack-plugin",
"version": "3.17.2",
"version": "3.17.3",
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
"keywords": [
"html",
Expand Down
7 changes: 4 additions & 3 deletions src/Plugin/AssetCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,9 @@ class AssetCompiler {
const fileManifest = {
render: () => cssContent,
filename: assetFile,
identifier: `${pluginName}.${chunk.id}`,
hash,
identifier: `${pluginName}.${chunk.id}.inline.css`,
// the validity of the hash does not matter because it will be injected in the HTML
hash: hash + 'inline',
};

result.push(fileManifest);
Expand Down Expand Up @@ -1219,7 +1220,7 @@ class AssetCompiler {
const fileManifest = {
render: () => cssContent,
filename: assetFile,
identifier: `${pluginName}.${chunk.id}`,
identifier: `${pluginName}.${chunk.id}.file.css`,
hash,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.import1 {
color: red;
}.import2 {
color: green;
}
16 changes: 16 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-dev/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<link href="index.css" rel="stylesheet">
<style>.import3 {
color: blue;
}</style>
</head>
<body>
<p class="import1">This text should be red (import1.scss)</p>
<p class="import2">This text should be green (import2.scss)</p>
<p class="import3">This text should be blue (import3.scss)</p>
<script src="index.js"></script>
</body>
</html>
131 changes: 131 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-dev/expected/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({

/***/ "./src/import1.scss":
/*!**************************!*\
!*** ./src/import1.scss ***!
\**************************/
/***/ (() => {

/* extracted by HTMLBundler CSSLoader */

/***/ }),

/***/ "./src/import2.scss":
/*!**************************!*\
!*** ./src/import2.scss ***!
\**************************/
/***/ (() => {

/* extracted by HTMLBundler CSSLoader */

/***/ }),

/***/ "./src/import3.scss?inline":
/*!*********************************!*\
!*** ./src/import3.scss?inline ***!
\*********************************/
/***/ (() => {

/* extracted by HTMLBundler CSSLoader */

/***/ })

/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
"use strict";
/*!**********************!*\
!*** ./src/index.js ***!
\**********************/
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _import1_scss__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./import1.scss */ "./src/import1.scss");
/* harmony import */ var _import1_scss__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_import1_scss__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _import2_scss__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./import2.scss */ "./src/import2.scss");
/* harmony import */ var _import2_scss__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_import2_scss__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _import3_scss_inline__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./import3.scss?inline */ "./src/import3.scss?inline");
/* harmony import */ var _import3_scss_inline__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_import3_scss_inline__WEBPACK_IMPORTED_MODULE_2__);
// These two imports are not included in the bundled css when running `webpack --mode development`.
// However, they are included in the bundled css when running `webpack --mode production`.



// This css is always properly inlined in the HTML file.
// Interestingly, if you comment this line out, then it fixes the issue with thea above imports
// (they will be properly included in the bundled css in development mode).


console.log("Hello World!");

})();

/******/ })()
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import2 {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import3 {
color: blue;
}
12 changes: 12 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-dev/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
</head>
<body>
<p class="import1">This text should be red (import1.scss)</p>
<p class="import2">This text should be green (import2.scss)</p>
<p class="import3">This text should be blue (import3.scss)</p>
<script src="index.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-dev/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// These two imports are not included in the bundled css when running `webpack --mode development`.
// However, they are included in the bundled css when running `webpack --mode production`.
import "./import1.scss";
import "./import2.scss";

// This css is always properly inlined in the HTML file.
// Interestingly, if you comment this line out, then it fixes the issue with thea above imports
// (they will be properly included in the bundled css in development mode).
import "./import3.scss?inline";

console.log("Hello World!");
31 changes: 31 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-dev/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require('path');
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');

module.exports = {
// test importing SCSS in JS in dev mode (in prod mode is OK) when in the same file is another SCSS file inlined via `?inline` query, #102
mode: 'development',

output: {
path: path.join(__dirname, 'dist/'),
clean: true,
publicPath: '',
},

plugins: [
new HtmlBundlerPlugin({
entry: {
index: './src/index.html',
},
verbose: true,
}),
],

module: {
rules: [
{
test: /\.(s?css)$/,
use: ['css-loader', 'sass-loader'],
},
],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.import1{color:red}.import2{color:green}
14 changes: 14 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-prod/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<link href="index.css" rel="stylesheet">
<style>.import3{color:blue}</style>
</head>
<body>
<p class="import1">This text should be red (import1.scss)</p>
<p class="import2">This text should be green (import2.scss)</p>
<p class="import3">This text should be blue (import3.scss)</p>
<script src="index.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(()=>{"use strict";console.log("Hello World!")})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import2 {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.import3 {
color: blue;
}
12 changes: 12 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-prod/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
</head>
<body>
<p class="import1">This text should be red (import1.scss)</p>
<p class="import2">This text should be green (import2.scss)</p>
<p class="import3">This text should be blue (import3.scss)</p>
<script src="index.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-prod/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// These two imports are not included in the bundled css when running `webpack --mode development`.
// However, they are included in the bundled css when running `webpack --mode production`.
import "./import1.scss";
import "./import2.scss";

// This css is always properly inlined in the HTML file.
// Interestingly, if you comment this line out, then it fixes the issue with thea above imports
// (they will be properly included in the bundled css in development mode).
import "./import3.scss?inline";

console.log("Hello World!");
30 changes: 30 additions & 0 deletions test/cases/js-import-scss-and-inline-scss-prod/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require('path');
const HtmlBundlerPlugin = require('@test/html-bundler-webpack-plugin');

module.exports = {
mode: 'production',

output: {
path: path.join(__dirname, 'dist/'),
clean: true,
publicPath: '',
},

plugins: [
new HtmlBundlerPlugin({
entry: {
index: './src/index.html',
},
verbose: true,
}),
],

module: {
rules: [
{
test: /\.(s?css)$/,
use: ['css-loader', 'sass-loader'],
},
],
},
};
3 changes: 3 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ describe('import styles in JavaScript', () => {

test('inline CSS', () => compareFiles('js-import-css-inline-css'));
test('inline CSS, source map', () => compareFiles('js-import-css-inline-css-sourceMap'));

test('import SCSS in JS and inline SCSS, prod', () => compareFiles('js-import-scss-and-inline-scss-prod'));
test('import SCSS in JS and inline SCSS, dev', () => compareFiles('js-import-scss-and-inline-scss-dev'));

test('inline images in CSS', () => compareFiles('js-import-css-inline-img-in-css'));
test('inline images in inlined CSS', () => compareFiles('js-import-css-inline-img-in-inlined-css'));
Expand Down

0 comments on commit 2cb4dca

Please sign in to comment.