Skip to content

Commit

Permalink
Fix: update to latest inline-js-core
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Jul 22, 2018
1 parent 5655203 commit d549475
Show file tree
Hide file tree
Showing 4 changed files with 638 additions and 1,465 deletions.
71 changes: 60 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// const {init: inline} = require("inline-js");
const path = require("path");

const {createInliner} = require("inline-js-core");
const {RESOURCES, PATH_LIKE} = require("inline-js-default-resources");
const {TRANSFORMS} = require("inline-js-default-transforms");
const {createConfigLocator} = require("config-locator");
const {parsePipes, pipesToString} = require("inline-js-core/lib/parser");

function createPlugin({
stringify = JSON.stringify,
inlineOptions
} = {}) {
const inliner = createInliner(inlineOptions);
RESOURCES.forEach(inliner.resource.add);
TRANSFORMS.forEach(inliner.transformer.add);

const configLocator = createConfigLocator({config: ".inline.js"});

return {
name: "rollup-plugin-inline-js",
load: id => {
Expand All @@ -26,16 +36,55 @@ function createPlugin({
name: pipes[0].name.replace(/^inline-?/, "") || "file",
args: pipes[0].args
};
const options = {source, target, transforms: pipes.slice(1)};
Object.assign(options, inlineOptions);
return inline(options)
.then(content => {
content = stringify(content);
if (typeof content !== "string") {
throw new Error(`The content loaded from ${id} is not a string`);
}
return `export default ${content};`;
});
return loadConfig()
.then(() => inliner.inline(target, source))
.then(({content, children}) =>
inliner.transformer.transform(
{
source,
inlineTarget: target
},
content,
pipes.slice(1)
)
.then(content => {
const dependencies = new Set;
extractTarget(children);
const code = stringify(content);
if (typeof code !== "string") {
throw new Error(`The content loaded from ${id} is not a string`);
}
return {
code: `export default ${code};`,
dependencies: [...dependencies]
};

function extractTarget(results) {
for (const {target, children} of results) {
if (!PATH_LIKE.has(target.name)) {
continue;
}
dependencies.add(target.args[0]);
extractTarget(children);
}
}
})
);

function loadConfig() {
if (PATH_LIKE.has(target.name)) {
const file = source ?
path.resolve(source.args[0], target.args[0]) :
target.args[0];
return configLocator.findConfig(file)
.then(result => {
if (result) {
inliner.useConfig(result.config);
}
});
}
return Promise.resolve();
}
},
resolveId: (importee, importer) => {
if (!/^inline[\w-]*:/.test(importee)) {
Expand Down

0 comments on commit d549475

Please sign in to comment.