Skip to content

Commit

Permalink
WIP: dev mode & simple object proxy output
Browse files Browse the repository at this point in the history
  • Loading branch information
tivac committed Sep 20, 2018
1 parent 18c157a commit fbdfd3d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/rollup/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
const path = require("path");

const { keyword } = require("esutils");

const utils = require("rollup-pluginutils");
const utils = require("rollup-pluginutils");

const Processor = require("@modular-css/processor");
const output = require("@modular-css/processor/lib/output.js");
Expand Down Expand Up @@ -33,11 +32,12 @@ module.exports = function(opts) {
include : "**/*.css",
namedExports : true,
styleExport : false,
dev : false,
}, opts);

const filter = utils.createFilter(options.include, options.exclude);

const { styleExport, done, map } = options;
const { styleExport, done, map, dev } = options;

if(typeof map === "undefined") {
// Sourcemaps don't make much sense in styleExport mode
Expand Down Expand Up @@ -83,6 +83,21 @@ module.exports = function(opts) {
const exported = output.join(exports);

const out = [
dev ? `
const data = ${JSON.stringify(exported, null, 4)};
export default new Proxy(data, {
get(tgt, key) {
if(key in tgt) {
return tgt[key];
}
throw new Error(
key + " is not exported by " + ${JSON.stringify(path.relative(process.cwd(), id))}
);
}
})
` :
`export default ${JSON.stringify(exported, null, 4)};`,
];

Expand Down
22 changes: 22 additions & 0 deletions packages/rollup/test/__snapshots__/rollup.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ exports[`/rollup.js should not output sourcemaps when they are disabled 1`] = `
"
`;
exports[`/rollup.js should output a proxy in dev mode 1`] = `
"const data = {
\\"str\\": \\"\\\\\\"string\\\\\\"\\",
\\"fooga\\": \\"fooga\\"
};
var css = new Proxy(data, {
get(tgt, key) {
if(key in tgt) {
return tgt[key];
}
throw new Error(
key + \\" is not exported by \\" + \\"packages\\\\\\\\rollup\\\\\\\\test\\\\\\\\specimens\\\\\\\\simple.css\\"
);
}
});
console.log(css);
"
`;
exports[`/rollup.js should provide named exports 1`] = `
"var str = \\"\\\\\\"string\\\\\\"\\";
var num = \\"10\\";
Expand Down
16 changes: 16 additions & 0 deletions packages/rollup/test/rollup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,22 @@ describe("/rollup.js", () => {
expect(read("./rollup/repeated-references/assets/repeated-references.css")).toMatchSnapshot();
});

it("should output a proxy in dev mode", async () => {
const bundle = await rollup({
input : require.resolve("./specimens/simple.js"),
plugins : [
plugin({
namer,
dev : true,
}),
],
});

const result = await bundle.generate({ format });

expect(result.code).toMatchSnapshot();
});

describe("errors", () => {
function checkError(err) {
expect(err.toString()).toMatch("error-plugin:");
Expand Down

0 comments on commit fbdfd3d

Please sign in to comment.