Skip to content

Commit

Permalink
feat: return template
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 8, 2020
1 parent 8e20736 commit 062b574
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ require('postcss-custom-properties-transformer')({
if (ctx.local.startsWith('theme-')) {
return ctx.local;
}
return ctx.hash('local', 4);

// You can return a template
return `[hash:filepath:4]-[hash:4]`;

// Alternatively, you can use the functions
return ctx.hash('filepath', 4) + '-' + ctx.hash(4);
}
})
```
Expand Down
19 changes: 7 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ const interpolater = require('./interpolater');
const generateContext = require('./generate-context');

const transformerWrapper = (
transformerFn,
transformer,
manifest = new PropertyNameManifest(),
) => context => {
let newPropertyName = transformerFn(context);
let newPropertyName = typeof transformer === 'function' ? transformer(context) : transformer;

newPropertyName = interpolater(newPropertyName, context);

newPropertyName = sanitizePropertyName(newPropertyName);

manifest.mark(newPropertyName, context.local);
Expand All @@ -25,17 +28,9 @@ const customPropertiesRegExp = /(^|[^\w-])var\([\W\w]+\)/;

const customPropertiesTransformer = postcss.plugin('postcss-custom-properties-transformer', options => {
assert(options && options.transformer, '[postcss-custom-properties] a transformer must be passed in');
assert(['function', 'string'].includes(typeof options.transformer), `[postcss-custom-properties] Unsupported transformer type "${typeof options.transformer}"`);

let {transformer} = options;

if (typeof transformer === 'string') {
const template = transformer;
transformer = context => interpolater(template, context);
} else if (typeof transformer !== 'function') {
throw new TypeError(`[postcss-custom-properties] Unsupported transformer type "${typeof options.transformer}"`);
}

transformer = transformerWrapper(transformer);
const transformer = transformerWrapper(options.transformer);

return root => {
const contextMaker = generateContext(root.source.input);
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,15 @@ exports[`transformer function namespace 1`] = `
}
"
`;

exports[`transformer function return template 1`] = `
"
.className {
--_dir_src_file_css-color: red;
--_dir_src_file_css-font-size: 24px;
background: var(--_dir_src_file_css-color);
border-color: var(--_dir_src_file_css-color);
font-size: var(--_dir_src_file_css-font-size);
}
"
`;
10 changes: 10 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ describe('transformer function', () => {
expect(output).toMatchSnapshot();
});

test('return template', async () => {
const output = await transform(fixtures.basicVar, {
transformer() {
return '[filepath]-[local]';
},
});

expect(output).toMatchSnapshot();
});

test('collision warning', async () => {
const warn = jest.spyOn(global.console, 'warn').mockImplementation();

Expand Down

0 comments on commit 062b574

Please sign in to comment.