Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 9b9cd8d

Browse files
joseprioevilebottnawi
authored andcommitted
feat: new options flag to output ES2015 modules (#340)
1 parent ba0fd4c commit 9b9cd8d

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,35 @@ module.exports = {
375375

376376
> ℹ️ If `[0]` is used, it will be replaced by the entire tested string, whereas `[1]` will contain the first capturing parenthesis of your regex and so on...
377377
378+
### `esModules`
379+
380+
Type: `Boolean`
381+
Default: `false`
382+
383+
By default, `file-loader` generates JS modules that use the CommonJS syntax. However, there are some cases in which using ES2015 modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
384+
385+
**webpack.config.js**
386+
387+
```js
388+
module.exports = {
389+
module: {
390+
rules: [
391+
{
392+
test: /\.css$/,
393+
use: [
394+
{
395+
loader: 'file-loader',
396+
options: {
397+
esModules: true,
398+
},
399+
},
400+
],
401+
},
402+
],
403+
},
404+
};
405+
```
406+
378407
## Placeholders
379408

380409
Full information about placeholders you can find [here](https://github.com/webpack/loader-utils#interpolatename).

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ export default function loader(content) {
5959
this.emitFile(outputPath, content);
6060
}
6161

62+
const esModules =
63+
typeof options.esModules === 'boolean' && options.esModules === true;
64+
6265
// TODO revert to ES2015 Module export, when new CSS Pipeline is in place
63-
return `module.exports = ${publicPath};`;
66+
return `${esModules ? 'export default' : 'module.exports ='} ${publicPath};`;
6467
}
6568

6669
export const raw = true;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`when applied with \`esModules\` option matches snapshot for \`false\` value 1`] = `
4+
Object {
5+
"assets": Array [
6+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
7+
],
8+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
11+
12+
exports[`when applied with \`esModules\` option matches snapshot for \`true\` value 1`] = `
13+
Object {
14+
"assets": Array [
15+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "export default __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
21+
exports[`when applied with \`esModules\` option matches snapshot without value 1`] = `
22+
Object {
23+
"assets": Array [
24+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
25+
],
26+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
27+
}
28+
`;

test/esModules-option.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import webpack from './helpers/compiler';
2+
3+
describe('when applied with `esModules` option', () => {
4+
it('matches snapshot without value', async () => {
5+
const config = {
6+
loader: {
7+
test: /(png|jpg|svg)/,
8+
},
9+
};
10+
11+
const stats = await webpack('fixture.js', config);
12+
const [module] = stats.toJson().modules;
13+
const { assets, source } = module;
14+
15+
expect({ assets, source }).toMatchSnapshot();
16+
});
17+
18+
it('matches snapshot for `true` value', async () => {
19+
const config = {
20+
loader: {
21+
test: /(png|jpg|svg)/,
22+
options: {
23+
esModules: true,
24+
},
25+
},
26+
};
27+
28+
const stats = await webpack('fixture.js', config);
29+
const [module] = stats.toJson().modules;
30+
const { assets, source } = module;
31+
32+
expect({ assets, source }).toMatchSnapshot();
33+
});
34+
35+
it('matches snapshot for `false` value', async () => {
36+
const config = {
37+
loader: {
38+
test: /(png|jpg|svg)/,
39+
options: {
40+
esModules: false,
41+
},
42+
},
43+
};
44+
45+
const stats = await webpack('fixture.js', config);
46+
const [module] = stats.toJson().modules;
47+
const { assets, source } = module;
48+
49+
expect({ assets, source }).toMatchSnapshot();
50+
});
51+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy