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

New options flag to output ES2015 modules #340

Merged
merged 2 commits into from Nov 21, 2019
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
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -375,6 +375,35 @@ module.exports = {

> ℹ️ 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...

### `esModules`

Type: `Boolean`
Default: `false`

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/).

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: 'file-loader',
options: {
esModules: true,
},
},
],
},
],
},
};
```

## Placeholders

Full information about placeholders you can find [here](https://github.com/webpack/loader-utils#interpolatename).
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Expand Up @@ -59,8 +59,11 @@ export default function loader(content) {
this.emitFile(outputPath, content);
}

const esModules =
typeof options.esModules === 'boolean' && options.esModules === true;

// TODO revert to ES2015 Module export, when new CSS Pipeline is in place
return `module.exports = ${publicPath};`;
return `${esModules ? 'export default' : 'module.exports ='} ${publicPath};`;
}

export const raw = true;
28 changes: 28 additions & 0 deletions test/__snapshots__/esModules-option.test.js.snap
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with \`esModules\` option matches snapshot for \`false\` value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`when applied with \`esModules\` option matches snapshot for \`true\` value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "export default __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`when applied with \`esModules\` option matches snapshot without value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
51 changes: 51 additions & 0 deletions test/esModules-option.test.js
@@ -0,0 +1,51 @@
import webpack from './helpers/compiler';

describe('when applied with `esModules` option', () => {
it('matches snapshot without value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { assets, source } = module;

expect({ assets, source }).toMatchSnapshot();
});

it('matches snapshot for `true` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
esModules: true,
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { assets, source } = module;

expect({ assets, source }).toMatchSnapshot();
});

it('matches snapshot for `false` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
esModules: false,
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { assets, source } = module;

expect({ assets, source }).toMatchSnapshot();
});
});