Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support webpack style imports #2

Merged
merged 1 commit into from Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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