Skip to content
Permalink
Browse files
feat(processor): add processor.resolve() (#681)
* feat(processor): expose processor.resolve()

And update the tests that were already using the `._resolve()` version.

* fix(svelte): use processor.resolve()

Fixes #679 by allowing the svelte preprocessor to use the same file resolution logic that the modular-css processor is using.
  • Loading branch information
tivac committed Oct 29, 2019
1 parent d9fb8be commit 2c5a51d736b2d5f0cf0e347e09a1cb5b9b24ddb9

Some generated files are not rendered by default. Learn more.

@@ -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",
@@ -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;

@@ -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);
@@ -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);
@@ -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",
]
@@ -336,7 +336,7 @@ describe("/processor.js", () => {
});
});

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

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

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

expect(
relative([
processor._resolve(
processor.resolve(
require.resolve("./specimens/start.css"),
"./local.css"
),
@@ -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"
@@ -2,7 +2,6 @@

const path = require("path");

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

@@ -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);

@@ -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>
@@ -0,0 +1,3 @@
<link rel="stylesheet" href="./does-not-exist.css" />

<div class="{css.fooga}">fooga</div>
@@ -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();
});
});
@@ -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).

0 comments on commit 2c5a51d

Please sign in to comment.