Skip to content
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

feat(processor): add processor.resolve() #681

Merged
merged 6 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"prepublishOnly": "npm run parsers",
"pretest": "npm run parsers",
"test": "jest",
"posttest": "npm run lint",
"watch": "jest --watch",
"www:build": "lerna run --stream build packages/www",
"www:start": "lerna run --stream start packages/www",
Expand Down
20 changes: 12 additions & 8 deletions packages/processor/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const postcss = require("postcss");
const slug = require("unique-slug");
const mapValues = require("lodash/mapValues");

const output = require("./lib/output.js");
const message = require("./lib/message.js");
const relative = require("./lib/relative.js");
const tiered = require("./lib/graph-tiers.js");
const resolve = require("./lib/resolve.js");
const normalize = require("./lib/normalize.js");
const output = require("./lib/output.js");
const message = require("./lib/message.js");
const relative = require("./lib/relative.js");
const tiered = require("./lib/graph-tiers.js");
const normalize = require("./lib/normalize.js");
const { resolvers } = require("./lib/resolve.js");

const noop = () => true;

Expand Down Expand Up @@ -70,8 +70,7 @@ class Processor {
// eslint-disable-next-line no-empty-function
() => {};

this._resolve = resolve.resolvers(options.resolvers);

this._resolve = resolvers(options.resolvers);
this._normalize = normalize.bind(null, this._options.cwd);

this._files = Object.create(null);
Expand Down Expand Up @@ -155,6 +154,11 @@ class Processor {
return this._normalize(file);
}

// Resolve a file from a src using the configured resolvers
resolve(src, file) {
return this._resolve(src, file);
}

// Check if a file exists in the currently-processed set
has(input) {
const file = this._normalize(input);
Expand Down
4 changes: 2 additions & 2 deletions packages/processor/test/__snapshots__/api.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`/processor.js API ._resolve() should fall back to a default resolver 1`] = `
exports[`/processor.js API .resolve() should fall back to a default resolver 1`] = `
Array [
"packages/processor/test/specimens/local.css",
]
`;

exports[`/processor.js API ._resolve() should run resolvers until a match is found 1`] = `
exports[`/processor.js API .resolve() should run resolvers until a match is found 1`] = `
Array [
"packages/processor/test/specimens/local.css",
]
Expand Down
6 changes: 3 additions & 3 deletions packages/processor/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe("/processor.js", () => {
});
});

describe("._resolve()", () => {
describe(".resolve()", () => {
it("should run resolvers until a match is found", () => {
let ran = false;

Expand All @@ -350,7 +350,7 @@ describe("/processor.js", () => {

expect(
relative([
processor._resolve(
processor.resolve(
require.resolve("./specimens/start.css"),
"./local.css"
),
Expand All @@ -370,7 +370,7 @@ describe("/processor.js", () => {

expect(
relative([
processor._resolve(
processor.resolve(
require.resolve("./specimens/start.css"),
"./local.css"
),
Expand Down
3 changes: 1 addition & 2 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"dependencies": {
"@modular-css/processor": "file:../processor",
"escape-string-regexp": "^2.0.0",
"is-url": "^1.2.4",
"resolve-from": "^5.0.0"
"is-url": "^1.2.4"
},
"peerDependencies": {
"svelte": ">1"
Expand Down
3 changes: 1 addition & 2 deletions packages/svelte/svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const path = require("path");

const resolve = require("resolve-from");
const isUrl = require("is-url");
const escape = require("escape-string-regexp");

Expand Down Expand Up @@ -143,7 +142,7 @@ module.exports = (config = false) => {
// Assign to file for later usage in logging
css = href;

const external = resolve(path.dirname(html), css);
const external = processor.resolve(html, css);

log("extract <link>", external);

Expand Down
13 changes: 13 additions & 0 deletions packages/svelte/test/__snapshots__/svelte.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,19 @@ exports[`/svelte.js should use an already-created processor 2`] = `
}"
`;

exports[`/svelte.js should use modular-css's file resolver 1`] = `
"<div class=\\"fooga\\">fooga</div>
<script>import css from \\"./does-not-exist.css\\";</script>"
`;

exports[`/svelte.js should use modular-css's file resolver 2`] = `
"/* packages/svelte/test/specimens/simple.css */
.fooga {
color: red;
}
"
`;

exports[`/svelte.js should wait for files to finish 1`] = `
Array [
"<style>/* replaced by modular-css */</style>
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/test/specimens/link-resolving.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<link rel="stylesheet" href="./does-not-exist.css" />

<div class="{css.fooga}">fooga</div>
26 changes: 26 additions & 0 deletions packages/svelte/test/svelte.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,30 @@ describe("/svelte.js", () => {

expect(results.map((result) => result.toString())).toMatchSnapshot();
});

it("should use modular-css's file resolver", async () => {
const processor = new Processor({
namer,
resolvers : [
// Force all paths to resolve to a different file
() => require.resolve("./specimens/simple.css"),
],
});

const filename = require.resolve("./specimens/link-resolving.html");
const { preprocess } = plugin({
processor,
});

const processed = await svelte.preprocess(
fs.readFileSync(filename, "utf8"),
Object.assign({}, preprocess, { filename })
);

expect(processed.toString()).toMatchSnapshot();

const output = await processor.output();

expect(output.css).toMatchSnapshot();
});
});
15 changes: 15 additions & 0 deletions packages/www/src/api/usage-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@ Remove files from the `Processor` instance. Accepts a single file or array of fi

Returns an array of file paths. Accepts a single file argument to get the dependencies for, will return entire dependency graph in order if argument is omitted.

##### `.invalidate(file)`

Marks a file as stale, if that file is re-added either directly or as a dependency it will be reloaded from disk instead of the Processor cache.

##### `.has(file)`

Checks if the Processor instance knows about a file.

##### `.normalize(file)`

Uses the built-in path normalization settings to normalize the case of a file path.

##### `.resolve(src, file)`

Resolves `file` from `src`, using any of the specified [`resolvers`](#resolvers).