Skip to content

Commit a9e0cc7

Browse files
refactor: code
1 parent 3007493 commit a9e0cc7

File tree

11 files changed

+61
-41
lines changed

11 files changed

+61
-41
lines changed

README.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ module.exports = {
5050
};
5151
```
5252

53-
**Only for webpack v4:**
54-
55-
Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
56-
5753
And run `webpack` via your preferred method.
5854

5955
### `toString`
@@ -1212,30 +1208,6 @@ module.exports = {
12121208
};
12131209
```
12141210

1215-
**For webpack v4:**
1216-
1217-
**webpack.config.js**
1218-
1219-
```js
1220-
module.exports = {
1221-
module: {
1222-
rules: [
1223-
{
1224-
test: /\.css$/i,
1225-
use: ["style-loader", "css-loader"],
1226-
},
1227-
{
1228-
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1229-
loader: "url-loader",
1230-
options: {
1231-
limit: 8192,
1232-
},
1233-
},
1234-
],
1235-
},
1236-
};
1237-
```
1238-
12391211
### Extract
12401212

12411213
For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
@@ -1285,14 +1257,6 @@ module.exports = {
12851257
// More information here https://webpack.js.org/guides/asset-modules/
12861258
type: "asset",
12871259
},
1288-
// For webpack v4
1289-
// {
1290-
// test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1291-
// loader: "url-loader",
1292-
// options: {
1293-
// limit: 8192,
1294-
// },
1295-
// },
12961260
],
12971261
},
12981262
};

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,14 @@ export default async function loader(content, map, meta) {
187187

188188
if (options.modules.exportOnlyLocals !== true) {
189189
imports.unshift({
190+
type: "api_import",
190191
importName: "___CSS_LOADER_API_IMPORT___",
191192
url: stringifyRequest(this, require.resolve("./runtime/api")),
192193
});
193194

194195
if (options.sourceMap) {
195196
imports.unshift({
197+
type: "api_sourcemap_import",
196198
importName: "___CSS_LOADER_API_SOURCEMAP_IMPORT___",
197199
url: stringifyRequest(
198200
this,

src/plugins/postcss-icss-parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const plugin = (options = {}) => {
7070
imports.set(importKey, importName);
7171

7272
options.imports.push({
73+
type: "icss_import",
7374
importName,
7475
url: options.urlHandler(newUrl),
7576
icss: true,

src/plugins/postcss-import-parser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ const plugin = (options = {}) => {
223223
urlToNameMap.set(newUrl, importName);
224224

225225
options.imports.push({
226+
type: "rule_import",
226227
importName,
227228
url: options.urlHandler(newUrl),
228229
index,

src/plugins/postcss-url-parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ const plugin = (options = {}) => {
337337

338338
if (!hasUrlImportHelper) {
339339
options.imports.push({
340+
type: "get_url_import",
340341
importName: "___CSS_LOADER_GET_URL_IMPORT___",
341342
url: options.urlHandler(
342343
require.resolve("../runtime/getUrl.js")
@@ -356,7 +357,7 @@ const plugin = (options = {}) => {
356357
urlToNameMap.set(newUrl, importName);
357358

358359
options.imports.push({
359-
type: "asset",
360+
type: "url",
360361
importName,
361362
url: options.urlHandler(newUrl),
362363
index,

src/runtime/getUrl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = (url, options) => {
44
options = {};
55
}
66

7-
if (!url || typeof url === "boolean") {
7+
if (!url) {
88
return url;
99
}
1010

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ function getImportCode(imports, options) {
598598
}* as ${importName}_NAMED___ from ${url};\n`;
599599
} else {
600600
code +=
601-
type === "asset"
601+
type === "url"
602602
? `var ${importName} = new URL(${url}, import.meta.url);\n`
603603
: `import ${importName} from ${url};\n`;
604604
}

test/fixtures/url/MCEP.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './url.css';

test/runtime/__snapshots__/getUrl.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`escape should escape url 1`] = `true`;
3+
exports[`escape should escape url 1`] = `"true"`;
44

55
exports[`escape should escape url 2`] = `null`;
66

test/sourceMap-option.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ describe('"sourceMap" option', () => {
498498
stats.compilation.assets
499499
).find((assetName) => /\.js$/.test(assetName));
500500

501-
expect(chunkName).toBe("main.fe645cef6147a34cde0b.bundle.js");
501+
expect(chunkName).toBe("main.6480a869998e0b381c90.bundle.js");
502502
expect(
503503
getModuleSource("fixtures/source-map/basic.css", stats)
504504
).toMatchSnapshot("module");

test/url-option.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import fs from "fs";
22
import path from "path";
33

4+
import MiniCssExtractPlugin from "mini-css-extract-plugin";
5+
46
import {
57
compile,
68
getCompiler,
79
getErrors,
810
getExecutedCode,
911
getModuleSource,
1012
getWarnings,
13+
readAsset,
1114
} from "./helpers/index";
1215

1316
describe('"url" option', () => {
@@ -295,4 +298,51 @@ describe('"url" option', () => {
295298
expect(getWarnings(stats)).toMatchSnapshot("warnings");
296299
expect(getErrors(stats, true)).toMatchSnapshot("errors");
297300
});
301+
302+
// Todo uncomment test when in MiniCssExtractPlugin will be applied fix for new URL syntax
303+
it.skip("should work with mini-css-extract-plugin", async () => {
304+
const compiler = getCompiler(
305+
"./url/MCEP.js",
306+
{},
307+
{
308+
module: {
309+
rules: [
310+
{
311+
test: /\.css$/i,
312+
use: [
313+
{
314+
loader: MiniCssExtractPlugin.loader,
315+
options: {
316+
esModule: true,
317+
},
318+
},
319+
{
320+
loader: path.resolve(__dirname, "../src"),
321+
options: {
322+
esModule: true,
323+
},
324+
},
325+
],
326+
},
327+
{
328+
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
329+
type: "asset/resource",
330+
},
331+
],
332+
},
333+
plugins: [
334+
new MiniCssExtractPlugin({
335+
filename: "[name].css",
336+
chunkFilename: "[id].css",
337+
}),
338+
],
339+
}
340+
);
341+
const stats = await compile(compiler);
342+
343+
expect(readAsset("main.css", compiler, stats)).toMatchSnapshot("css");
344+
expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
345+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
346+
expect(getErrors(stats)).toMatchSnapshot("errors");
347+
});
298348
});

0 commit comments

Comments
 (0)