Skip to content

Commit

Permalink
feat(loader): support es modules (#9)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires file-loader >=5.0.1
  • Loading branch information
adrienharnay authored Nov 28, 2019
1 parent 8775de5 commit a69c3f6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// https://prettier.io/docs/en/options.html

module.exports = {
// Enables semicolons at the end of statements
semi: true,
// Formats strings with single quotes ('') instead of quotes ("")
singleQuote: true,
// Adds a trailing comma at the end of all lists (including function arguments)
trailingComma: 'all',
// Adds parentheses around all functions arguments lists
arrowParens: 'always',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"semantic-release": "15.13.21"
},
"peerDependencies": {
"file-loader": "^2.0.0 || ^3.0.0",
"file-loader": "^5.0.1",
"webpack": "^4.0.0"
}
}
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ const readFileAsync = (context, filename, warnOnMissingSrcset) =>
const getSource = (context, contentBuffer) => {
let content = contentBuffer.toString('utf8');

const contentIsUrlExport = /^module.exports = "data:[^;]*;base64,/.test(
const contentIsUrlExport = /^export default "data:[^;]*;base64,/.test(
content,
);
const contentIsFileExport = content.startsWith('module.exports = ');
const contentIsFileExport = content.startsWith('export default ');
let source = '';

if (contentIsUrlExport) {
// eslint-disable-next-line prefer-destructuring
source = content.match(/^module.exports = (.*)/)[1];
source = content.match(/^export default (.*)/)[1];
} else {
if (!contentIsFileExport) {
content = fileLoader.call(context, contentBuffer);
}
// eslint-disable-next-line prefer-destructuring
source = content.match(/^module.exports = (.*);/)[1];
source = content.match(/^export default (.*);/)[1];
}

return source;
Expand All @@ -69,7 +69,7 @@ const hash = str => xxHash.h32(fastStableStringify(str), 0).toString(16);

const processOtherFormats = (context, contentBuffer) => {
const callback = context.async();
const result = `module.exports = ${getSource(context, contentBuffer)}`;
const result = `export default ${getSource(context, contentBuffer)}`;

callback(null, result);
};
Expand Down Expand Up @@ -163,7 +163,7 @@ const processJPGPNG = (context, contentBuffer) => {
};
});

const result = `module.exports = {${srcset
const result = `export default {${srcset
.map((data, index) =>
data
? `x${index + 1}: { ${Object.entries(data)
Expand Down Expand Up @@ -219,7 +219,7 @@ const processSVG = (context, contentBuffer) => {
}),
})
.then((data) => {
const result = `module.exports = ${getSource(context, data)}`;
const result = `export default ${getSource(context, data)}`;

callback(null, result);
})
Expand Down
36 changes: 2 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -4205,7 +4205,7 @@ import-lazy@^3.1.0:
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-3.1.0.tgz#891279202c8a2280fdbd6674dbd8da1a1dfc67cc"
integrity sha512-8/gvXvX2JMn0F+CDlSC4l6kOmVaLOO3XLkksI7CI3Ud95KDYJuYur2b9P/PUt/i/pDAMd/DulQsNbbbmRRsDIQ==

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5130,11 +5130,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5143,33 +5138,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -5220,11 +5193,6 @@ lodash.merge@^4.6.0:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down

0 comments on commit a69c3f6

Please sign in to comment.