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

Commit

Permalink
fix: fix module import of local css files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Fatme committed Nov 21, 2019
1 parent 0c51911 commit 2c0a36e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions css2json-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("css2jsonLoader", () => {

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

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

done();
Expand Down
4 changes: 2 additions & 2 deletions css2json-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse, Import, Stylesheet } from "css";
import { loader } from "webpack";
import { getOptions } from "loader-utils";
import { getOptions, urlToRequest } from "loader-utils";

const betweenQuotesPattern = /('|")(.*?)\1/;
const unpackUrlPattern = /url\(([^\)]+)\)/;
Expand Down Expand Up @@ -53,7 +53,7 @@ function extractUrlFromRule(importRule: Import): string {
function createRequireUri(uri): { uri: string, requireURI: string } {
return {
uri: uri,
requireURI: uri[0] === "~" && uri[1] !== "/" ? uri.substr(1) : uri
requireURI: urlToRequest(uri)
};
}

Expand Down

0 comments on commit 2c0a36e

Please sign in to comment.