-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add function support for locals (loader) (#985)
- Loading branch information
Showing
9 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/cases/custom-loader-with-functional-exports/app/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { cnA, cnB } from "./style.css"; | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(cnA(), cnB()); |
14 changes: 14 additions & 0 deletions
14
test/cases/custom-loader-with-functional-exports/app/mockLoader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default function loader() { | ||
const callback = this.async(); | ||
|
||
callback( | ||
null, | ||
`export default [ | ||
[module.id, ".class-name-a {background: red;}", ""], | ||
[module.id, ".class-name-b {background: blue;}", ""], | ||
]; | ||
export var cnA = () => "class-name-a"; | ||
export var cnB = () => "class-name-b";` | ||
); | ||
} |
7 changes: 7 additions & 0 deletions
7
test/cases/custom-loader-with-functional-exports/app/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.class-name-a { | ||
background: red; | ||
} | ||
|
||
.class-name-b { | ||
background: blue; | ||
} |
2 changes: 2 additions & 0 deletions
2
test/cases/custom-loader-with-functional-exports/expected/main.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.class-name-a {background: red;} | ||
.class-name-b {background: blue;} |
87 changes: 87 additions & 0 deletions
87
test/cases/custom-loader-with-functional-exports/expected/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******/ (() => { // webpackBootstrap | ||
/******/ "use strict"; | ||
/******/ var __webpack_modules__ = ([ | ||
/* 0 */, | ||
/* 1 */ | ||
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { | ||
|
||
__webpack_require__.r(__webpack_exports__); | ||
/* harmony export */ __webpack_require__.d(__webpack_exports__, { | ||
/* harmony export */ "cnA": () => (/* binding */ cnA), | ||
/* harmony export */ "cnB": () => (/* binding */ cnB) | ||
/* harmony export */ }); | ||
// extracted by mini-css-extract-plugin | ||
var cnA = () => "class-name-a"; | ||
var cnB = () => "class-name-b"; | ||
|
||
/***/ }) | ||
/******/ ]); | ||
/************************************************************************/ | ||
/******/ // 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/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 isolated against other modules in the chunk. | ||
(() => { | ||
__webpack_require__.r(__webpack_exports__); | ||
/* harmony import */ var _style_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); | ||
|
||
|
||
// eslint-disable-next-line no-console | ||
console.log((0,_style_css__WEBPACK_IMPORTED_MODULE_0__.cnA)(), (0,_style_css__WEBPACK_IMPORTED_MODULE_0__.cnB)()); | ||
|
||
})(); | ||
|
||
/******/ })() | ||
; |
21 changes: 21 additions & 0 deletions
21
test/cases/custom-loader-with-functional-exports/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import path from "path"; | ||
|
||
import Self from "../../../src"; | ||
|
||
module.exports = { | ||
entry: "./index.js", | ||
context: path.resolve(__dirname, "app"), | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [Self.loader, "./mockLoader"], | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
new Self({ | ||
filename: "[name].css", | ||
}), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { stringifyLocal } from "../src/utils"; | ||
|
||
describe("stringifyLocal", () => { | ||
it(`primitive`, async () => { | ||
const testObj = "classA"; | ||
|
||
expect(stringifyLocal(testObj)).toBe('"classA"'); | ||
}); | ||
|
||
it(`arrow function`, async () => { | ||
const testFn = () => "classA"; | ||
|
||
expect(stringifyLocal(testFn)).toBe('() => "classA"'); | ||
}); | ||
|
||
it(`function`, async () => { | ||
const testFn = function () { | ||
return "classA"; | ||
}; | ||
|
||
expect(stringifyLocal(testFn)).toBe( | ||
'function () {\n return "classA";\n }' | ||
); | ||
}); | ||
}); |