Skip to content

Commit

Permalink
fix: only replace imports once
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 29, 2020
1 parent 567ca3e commit 3c52867
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ module.exports = ({ path, urls, imports } = {}) => {
prepare() {
// Avoid parsing things more than necessary
const processed = new WeakMap();
// Only replace once per url
const replaced = new Set();
// Eagerly start resolving
const mapFetch = populateImportMap({ path, urls, imports });
// Reused replace logic
Expand All @@ -107,9 +109,12 @@ module.exports = ({ path, urls, imports } = {}) => {
// Webpack interop
key = key.replace(/^~/, '');

if (mapping.has(key)) {
if (replaced.has(key)) {
decl.remove();
} else if (mapping.has(key)) {
// eslint-disable-next-line no-param-reassign
decl.params = `'${mapping.get(key)}'`;
replaced.add(key);
}

// Cache we've processed this
Expand Down
5 changes: 0 additions & 5 deletions tap-snapshots/test-plugin.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
exports[`test/plugin.js TAP plugin() - advanced module - should replace normalize.css with CDN URL > advanced 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';
`

Expand All @@ -28,8 +26,5 @@ exports[`test/plugin.js TAP plugin() - simple module - should replace normalize.
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';
`

0 comments on commit 3c52867

Please sign in to comment.