-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[draft] feat: resolve style field in package json #388
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Burhanuddin Udaipurwala @burhanuday | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const path = require("path"); | ||
const DescriptionFileUtils = require("./DescriptionFileUtils"); | ||
|
||
/** @typedef {import("./Resolver")} Resolver */ | ||
/** @typedef {import("./Resolver").JsonObject} JsonObject */ | ||
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */ | ||
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */ | ||
|
||
const alreadyTriedStyleField = Symbol("alreadyTriedStyleField"); | ||
|
||
module.exports = class StyleFieldPlugin { | ||
/** | ||
* @param {string | ResolveStepHook} source source | ||
* @param {string} name fieldName | ||
* @param {string | ResolveStepHook} target target | ||
*/ | ||
constructor(source, name, target) { | ||
this.source = source; | ||
this.name = name; | ||
this.target = target; | ||
} | ||
|
||
/** | ||
* @param {Resolver} resolver the resolver | ||
* @returns {void} | ||
*/ | ||
apply(resolver) { | ||
const target = resolver.ensureHook(this.target); | ||
resolver | ||
.getHook(this.source) | ||
.tapAsync("StyleFieldPlugin", (request, resolveContext, callback) => { | ||
// if request.context.issuer is not a css file, don't resolve to style field | ||
const isIssuerAStyleFile = | ||
request.context && | ||
// @ts-ignore | ||
request.context.issuer && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: check if type can be improved |
||
// @ts-ignore | ||
request.context.issuer.endsWith(".css"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: check for all known css extensions |
||
if ( | ||
!isIssuerAStyleFile || | ||
request.path !== request.descriptionFileRoot || | ||
/** @type {ResolveRequest & { [alreadyTriedStyleField]?: string }} */ | ||
(request)[alreadyTriedStyleField] === request.descriptionFilePath || | ||
!request.descriptionFilePath | ||
) | ||
return callback(); | ||
const filename = path.basename(request.descriptionFilePath); | ||
let stylePath = | ||
/** @type {string|null|undefined} */ | ||
( | ||
DescriptionFileUtils.getField( | ||
/** @type {JsonObject} */ (request.descriptionFileData), | ||
this.name | ||
) | ||
); | ||
|
||
if ( | ||
!stylePath || | ||
typeof stylePath !== "string" || | ||
stylePath === "." || | ||
stylePath === "./" | ||
) { | ||
return callback(); | ||
} | ||
if ( | ||
// this.options.forceRelative | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: add support for |
||
true && | ||
!/^\.\.?\//.test(stylePath) | ||
) | ||
stylePath = "./" + stylePath; | ||
/** @type {ResolveRequest & { [alreadyTriedStyleField]?: string }} */ | ||
const obj = { | ||
...request, | ||
request: stylePath, | ||
module: false, | ||
directory: stylePath.endsWith("/"), | ||
[alreadyTriedStyleField]: request.descriptionFilePath | ||
}; | ||
return resolver.doResolve( | ||
target, | ||
obj, | ||
"use " + stylePath + " from " + this.name + " in " + filename, | ||
resolveContext, | ||
callback | ||
); | ||
}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import "./style.css"; | ||
import "style-library"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"main": "index.js" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "style-library"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const ResolverFactory = require("../lib/ResolverFactory"); | ||
const CachedInputFileSystem = require("../lib/CachedInputFileSystem"); | ||
|
||
const fixture = path.resolve(__dirname, "fixtures", "style"); | ||
|
||
describe("StyleFieldPlugin", () => { | ||
const nodeFileSystem = new CachedInputFileSystem(fs, 4000); | ||
|
||
const resolver = ResolverFactory.createResolver({ | ||
fileSystem: nodeFileSystem | ||
}); | ||
|
||
it("should resolve style field", done => { | ||
resolver.resolve( | ||
{ | ||
issuer: path.resolve(fixture, "style.css") | ||
}, | ||
fixture, | ||
"style-library", | ||
{}, | ||
(err, result) => { | ||
console.log( | ||
"🚀 ~ file: styleField.test.js:17 ~ resolver.resolve ~ err, result:", | ||
err, | ||
result | ||
); | ||
if (err) return done(err); | ||
if (!result) return done(new Error("No result")); | ||
expect(result).toEqual( | ||
path.resolve(fixture, "node_modules/style-library/style.css") | ||
); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it("should not resolve to style field if request is not coming from a css file", done => { | ||
resolver.resolve( | ||
{ | ||
issuer: path.resolve(fixture, "index.js") | ||
}, | ||
fixture, | ||
"style-library", | ||
{}, | ||
(err, result) => { | ||
if (err) return done(err); | ||
if (!result) return done(new Error("No result")); | ||
expect(result).toEqual( | ||
path.resolve(fixture, "node_modules/style-library/main.js") | ||
); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: check if these are the correct
source
andtarget