Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 2c0a36e

Browse files
committed
fix: fix module import of local css files
There is an issue when importing local css file as module as the `css2json-loader` handles it as a module instead of a local file. So, the webpack throws an error that it is not able to find the module. This PR fixes this issue as changing the method used to get the correct file uri with the method used by `css-loader` itself - https://github.com/webpack-contrib/css-loader/blob/967fb66da2545f04055eb0900a69f86e484dd842/src/utils.js#L220. Rel to: #1098
1 parent 0c51911 commit 2c0a36e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

css2json-loader.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("css2jsonLoader", () => {
3838

3939
const loaderContext = {
4040
callback: (error, source: string, map) => {
41-
expect(source).toContain(`global.registerModule("custom.css", () => require("custom.css"))`);
41+
expect(source).toContain(`global.registerModule("./custom.css", () => require("./custom.css"))`);
4242
expect(source).toContain(`{"type":"declaration","property":"background-color","value":"#7f9"}`);
4343

4444
done();
@@ -52,7 +52,7 @@ describe("css2jsonLoader", () => {
5252
it("inlines css2json loader in imports if option is provided", (done) => {
5353
const loaderContext = {
5454
callback: (error, source: string, map) => {
55-
expect(source).toContain(`global.registerModule("custom.css", () => require("!nativescript-dev-webpack/css2json-loader?useForImports!custom.css"))`);
55+
expect(source).toContain(`global.registerModule("./custom.css", () => require("!nativescript-dev-webpack/css2json-loader?useForImports!./custom.css"))`);
5656
expect(source).toContain(`{"type":"declaration","property":"background-color","value":"#7f9"}`);
5757

5858
done();

css2json-loader.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse, Import, Stylesheet } from "css";
22
import { loader } from "webpack";
3-
import { getOptions } from "loader-utils";
3+
import { getOptions, urlToRequest } from "loader-utils";
44

55
const betweenQuotesPattern = /('|")(.*?)\1/;
66
const unpackUrlPattern = /url\(([^\)]+)\)/;
@@ -53,7 +53,7 @@ function extractUrlFromRule(importRule: Import): string {
5353
function createRequireUri(uri): { uri: string, requireURI: string } {
5454
return {
5555
uri: uri,
56-
requireURI: uri[0] === "~" && uri[1] !== "/" ? uri.substr(1) : uri
56+
requireURI: urlToRequest(uri)
5757
};
5858
}
5959

0 commit comments

Comments
 (0)