Skip to content

Commit b6d39f5

Browse files
refactor: removed ref and unref api for useable loader (#409)
BREAKING CHANGE: removed `ref`/`unref` api were removed for useable loader, please use `use`/`unuse` api
1 parent 7c693d6 commit b6d39f5

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,17 @@ Possible values:
9797
- `linkTag`
9898

9999
When you `lazyStyleTag` or `lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
100-
It is named `Reference Counter API`.
101100

102101
**component.js**
103102

104103
```js
105104
import style from './file.css';
106105

107-
style.use(); // = style.ref();
108-
style.unuse(); // = style.unref();
106+
style.use();
107+
style.unuse();
109108
```
110109

111-
By convention the `Reference Counter API` should be bound to `.useable.css` and the `.css` should be loaded with basic `style-loader` usage.(similar to other file types, i.e. `.useable.less` and `.less`).
110+
We recommend following `.lazy.css` naming convention for lazy styles and the `.css` for basic `style-loader` usage (similar to other file types, i.e. `.lazy.less` and `.less`).
112111

113112
**webpack.config.js**
114113

@@ -118,11 +117,11 @@ module.exports = {
118117
rules: [
119118
{
120119
test: /\.css$/i,
121-
exclude: /\.useable\.css$/i,
120+
exclude: /\.lazy\.css$/i,
122121
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
123122
},
124123
{
125-
test: /\.useable\.css$/i,
124+
test: /\.lazy\.css$/i,
126125
use: [
127126
{
128127
loader: 'style-loader',
@@ -139,9 +138,9 @@ module.exports = {
139138
};
140139
```
141140

142-
Styles are not added on `import/require()`, but instead on call to `use`/`ref`. Styles are removed from page if `unuse`/`unref` is called exactly as often as `use`/`ref`.
141+
Styles are not added on `import/require()`, but instead on call to `use`. Styles are removed from page if `unuse` is called exactly as often as `use`.
143142

144-
> ⚠️ Behavior is undefined when `unuse`/`unref` is called more often than `use`/`ref`. Don't do that.
143+
> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
145144
146145
#### `styleTag`
147146

@@ -243,7 +242,7 @@ module.exports = {
243242
module: {
244243
rules: [
245244
{
246-
test: /\.useable\.css$/i,
245+
test: /\.lazy\.css$/i,
247246
use: [
248247
{ loader: 'style-loader', options: { injectType: 'lazyStyleTag' } },
249248
'css-loader',
@@ -286,7 +285,7 @@ module.exports = {
286285
module: {
287286
rules: [
288287
{
289-
test: /\.useable\.css$/i,
288+
test: /\.lazy\.css$/i,
290289
use: [
291290
{
292291
loader: 'style-loader',

src/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ module.exports.pitch = function loader(request) {
3636
case 'linkTag': {
3737
const hmrCode = this.hot
3838
? `
39-
if (module.hot) {
39+
if (module.hot) {
4040
module.hot.accept(
41-
${loaderUtils.stringifyRequest(this, `!!${request}`)},
41+
${loaderUtils.stringifyRequest(this, `!!${request}`)},
4242
function() {
4343
update(require(${loaderUtils.stringifyRequest(this, `!!${request}`)}));
4444
}
4545
);
46-
47-
module.hot.dispose(function() {
48-
update();
46+
47+
module.hot.dispose(function() {
48+
update();
4949
});
5050
}`
5151
: '';
@@ -67,22 +67,22 @@ if (module.hot) {
6767
const hmrCode = this.hot
6868
? `
6969
if (module.hot) {
70-
var lastRefs = module.hot.data && module.hot.data.refs || 0;
70+
var lastRefs = module.hot.data && module.hot.data.refs || 0;
7171
7272
if (lastRefs) {
7373
exports.ref();
7474
if (!content.locals) {
7575
refs = lastRefs;
7676
}
7777
}
78-
78+
7979
if (!content.locals) {
8080
module.hot.accept();
81-
}
82-
81+
}
82+
8383
module.hot.dispose(function(data) {
8484
data.refs = content.locals ? 0 : refs;
85-
85+
8686
if (dispose) {
8787
dispose();
8888
}
@@ -97,11 +97,11 @@ var options = ${JSON.stringify(options)};
9797
9898
options.insertInto = ${insertInto};
9999
options.singleton = ${isSingleton};
100-
100+
101101
if (typeof content === 'string') content = [[module.id, content, '']];
102102
if (content.locals) exports.locals = content.locals;
103103
104-
exports.use = exports.ref = function() {
104+
exports.use = function() {
105105
if (!(refs++)) {
106106
dispose = require(${loaderUtils.stringifyRequest(
107107
this,
@@ -112,7 +112,7 @@ exports.use = exports.ref = function() {
112112
return exports;
113113
};
114114
115-
exports.unuse = exports.unref = function() {
115+
exports.unuse = function() {
116116
if (refs > 0 && !--refs) {
117117
dispose();
118118
dispose = null;
@@ -131,18 +131,18 @@ ${hmrCode}
131131
? `
132132
if (module.hot) {
133133
module.hot.accept(
134-
${loaderUtils.stringifyRequest(this, `!!${request}`)},
134+
${loaderUtils.stringifyRequest(this, `!!${request}`)},
135135
function() {
136136
var newContent = require(${loaderUtils.stringifyRequest(
137137
this,
138138
`!!${request}`
139139
)});
140140
141-
if (typeof newContent === 'string')
141+
if (typeof newContent === 'string')
142142
newContent = [[module.id, newContent, '']];
143143
144144
var locals = (function(a, b) {
145-
var key,
145+
var key,
146146
idx = 0;
147147
148148
for (key in a) {
@@ -155,15 +155,15 @@ if (module.hot) {
155155
return idx === 0;
156156
}(content.locals, newContent.locals));
157157
158-
if (!locals)
158+
if (!locals)
159159
throw new Error('Aborting CSS HMR due to changed css-modules locals.');
160160
161161
update(newContent);
162162
}
163163
);
164164
165165
module.hot.dispose(function() {
166-
update();
166+
update();
167167
});
168168
}`
169169
: '';

0 commit comments

Comments
 (0)