Skip to content

Commit

Permalink
Add the exportGlobals option
Browse files Browse the repository at this point in the history
  • Loading branch information
madyankin committed Jun 23, 2020
1 parent fea101c commit 00cfa15
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
43 changes: 27 additions & 16 deletions README.md
Expand Up @@ -84,13 +84,13 @@ use the `getJSON` callback. For example, save data about classes into a correspo
```js
postcss([
require("postcss-modules")({
getJSON: function(cssFileName, json, outputFileName) {
getJSON: function (cssFileName, json, outputFileName) {
var path = require("path");
var cssName = path.basename(cssFileName, ".css");
var jsonFileName = path.resolve("./build/" + cssName + ".json");
fs.writeFileSync(jsonFileName, JSON.stringify(json));
}
})
},
}),
]);
```

Expand All @@ -104,8 +104,8 @@ this behaviour using the `scopeBehaviour` option:
```js
postcss([
require("postcss-modules")({
scopeBehaviour: "global" // can be 'global' or 'local',
})
scopeBehaviour: "global", // can be 'global' or 'local',
}),
]);
```

Expand All @@ -125,15 +125,15 @@ To generate custom classes, use the `generateScopedName` callback:
```js
postcss([
require("postcss-modules")({
generateScopedName: function(name, filename, css) {
generateScopedName: function (name, filename, css) {
var path = require("path");
var i = css.indexOf("." + name);
var line = css.substr(0, i).split(/[\r\n]/).length;
var file = path.basename(filename, ".css");

return "_" + file + "_" + line + "_" + name;
}
})
},
}),
]);
```

Expand All @@ -143,8 +143,8 @@ Or just pass an interpolated string to the `generateScopedName` option
```js
postcss([
require("postcss-modules")({
generateScopedName: "[name]__[local]___[hash:base64:5]"
})
generateScopedName: "[name]__[local]___[hash:base64:5]",
}),
]);
```

Expand All @@ -154,22 +154,33 @@ It's possible to add custom hash to generate more unique classes using the `hash
postcss([
require("postcss-modules")({
generateScopedName: "[name]__[local]___[hash:base64:5]",
hashPrefix: "prefix"
})
hashPrefix: "prefix",
}),
]);
```

## Exporting globals

If you need to export global names via the JSON object along with the local ones, add the `exportGlobals` option:

````js
postcss([
require("postcss-modules")({
exportGlobals: true,
}),
]);

### Loading source files

If you need, you can pass a custom loader (see [FileSystemLoader] for example):

```js
postcss([
require("postcss-modules")({
Loader: CustomLoader
})
Loader: CustomLoader,
}),
]);
```
````
You can also pass any needed root path:
Expand Down Expand Up @@ -264,7 +275,7 @@ var template = fs.readFileSync("./about.html", "utf8");

posthtml([posthtmlCssModules("./cssModules.json")])
.process(template)
.then(function(result) {
.then(function (result) {
console.log(result.html);
});
```
Expand Down
8 changes: 6 additions & 2 deletions src/behaviours.js
Expand Up @@ -8,8 +8,12 @@ export const behaviours = {
GLOBAL: "global",
};

export function getDefaultPlugins(behaviour, generateScopedName) {
const scope = modulesScope({ generateScopedName });
export function getDefaultPlugins({
behaviour,
generateScopedName,
exportGlobals,
}) {
const scope = modulesScope({ generateScopedName, exportGlobals });

const plugins = {
[behaviours.LOCAL]: [values, localByDefault, extractImports, scope],
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Expand Up @@ -42,14 +42,23 @@ function isGlobalModule(globalModules, inputFile) {

function getDefaultPluginsList(opts, inputFile) {
const globalModulesList = opts.globalModulePaths || null;
const exportGlobals = opts.exportGlobals || false;
const defaultBehaviour = getDefaultScopeBehaviour(opts);
const generateName = getScopedNameGenerator(opts);
const generateScopedName = getScopedNameGenerator(opts);

if (globalModulesList && isGlobalModule(globalModulesList, inputFile)) {
return getDefaultPlugins(behaviours.GLOBAL, generateName);
return getDefaultPlugins({
behaviour: behaviours.GLOBAL,
generateScopedName,
exportGlobals,
});
}

return getDefaultPlugins(defaultBehaviour, generateName);
return getDefaultPlugins({
behaviour: defaultBehaviour,
generateScopedName,
exportGlobals,
});
}

function isResultPlugin(plugin) {
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Expand Up @@ -265,3 +265,25 @@ it("exposes export tokens for other plugins", async () => {
"exposes export tokens for other plugins"
);
});

it("processes exportGlobals option", async () => {
const sourceFile = path.join(fixturesPath, "in", "classes.css");
const source = fs.readFileSync(sourceFile).toString();
let json;

await postcss([
plugin({
generateScopedName,
exportGlobals: true,
getJSON: (_, result) => {
json = result;
},
}),
]).process(source, { from: sourceFile });

expect(json).toMatchObject({
page: "page",
title: "_classes_title",
article: "_classes_article",
});
});

0 comments on commit 00cfa15

Please sign in to comment.