Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: support webpack imports (#2)
  • Loading branch information
stipsan committed Aug 26, 2020
1 parent a68d80b commit d069b57
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/plugin.js
Expand Up @@ -87,6 +87,9 @@ export default postcss.plugin(
key = decl.params.replace(/["']/g, '');
}

// Webpack interop
key = key.replace(/^~/, '');

if (mapping.has(key)) {
// eslint-disable-next-line no-param-reassign
decl.params = `'${mapping.get(key)}'`;
Expand Down
9 changes: 9 additions & 0 deletions tap-snapshots/test-plugin.js-TAP.test.js
Expand Up @@ -24,3 +24,12 @@ exports[`test/plugin.js TAP plugin() - simple module - should replace normalize.
@import 'https://unpkg.com/normalize.css@8/normalize.css';
`

exports[`test/plugin.js TAP plugin() - webpack module - should replace normalize.css with CDN URL > webpack example 1`] = `
@import 'https://unpkg.com/normalize.css@8/normalize.css';
@import 'https://unpkg.com/normalize.css@8/normalize.css';
@import 'https://unpkg.com/normalize.css@8/normalize.css';
@import 'https://unpkg.com/normalize.css@8/normalize.css';
`
22 changes: 22 additions & 0 deletions test/plugin.js
Expand Up @@ -10,6 +10,12 @@ const advanced = `
@import url("normalize.css");
@import "normalize.css";
`;
const webpack = `
@import '~normalize.css';
@import url(~normalize.css);
@import url("~normalize.css");
@import "~normalize.css";
`;

/*
* When running tests on Windows, the output code get some extra \r on each line.
Expand Down Expand Up @@ -66,6 +72,22 @@ tap.test(
}
);

tap.test(
'plugin() - webpack module - should replace normalize.css with CDN URL',
async (t) => {
const { css } = await postcss(
plugin({
imports: {
'normalize.css': 'https://unpkg.com/normalize.css@8/normalize.css',
},
})
).process(webpack, { from: undefined });

t.matchSnapshot(clean(css), 'webpack example');
t.end();
}
);

tap.test(
'plugin() - import values is an Array - should use the first entry in the Array',
async (t) => {
Expand Down

0 comments on commit d069b57

Please sign in to comment.