Skip to content

Commit

Permalink
fix: enforce esm (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Jun 30, 2020
1 parent 6efb133 commit b146549
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export function pitch(request) {
? `\n${esModule ? 'export default' : 'module.exports ='} ${JSON.stringify(
locals
)};`
: esModule
? `\nexport {};`
: '';

let resultSource = `// extracted by ${pluginName}`;
Expand Down
36 changes: 36 additions & 0 deletions test/enforce-esm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getCompiler, source, compile } from './helpers';

it('should enforce esm for empty module with options.esModule', async (done) => {
const compiler = getCompiler(
'./esm.js',
{ esModule: true },
{
mode: 'production',
optimization: { minimize: false },
}
);
const stats = await compile(compiler);
expect(stats.hasErrors()).toBe(false);
expect(source('./simple.css', stats)).toMatchInlineSnapshot(`
"// extracted by mini-css-extract-plugin
export {};"
`);
done();
});

it('should keep empty module without options.esModule', async (done) => {
const compiler = getCompiler(
'./esm.js',
{},
{
mode: 'production',
optimization: { minimize: false },
}
);
const stats = await compile(compiler);
expect(stats.hasErrors()).toBe(false);
expect(source('./simple.css', stats)).toMatchInlineSnapshot(
`"// extracted by mini-css-extract-plugin"`
);
done();
});
1 change: 1 addition & 0 deletions test/fixtures/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./simple.css";
14 changes: 9 additions & 5 deletions test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { createFsFromVolume, Volume } from 'memfs';
import MiniCssExtractPlugin from '../../src';

export default (fixture, loaderOptions = {}, config = {}) => {
const { outputFileSystem, ...cnfg } = config;

const fullConfig = {
mode: 'development',
devtool: config.devtool || false,
devtool: cnfg.devtool || false,
context: path.resolve(__dirname, '../fixtures'),
entry: path.resolve(__dirname, '../fixtures', fixture),
output: {
Expand Down Expand Up @@ -40,16 +42,18 @@ export default (fixture, loaderOptions = {}, config = {}) => {
chunkFilename: '[id].css',
}),
],
...config,
...cnfg,
};

const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = createFsFromVolume(new Volume());
if (!outputFileSystem) {
const outputFS = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);
outputFS.join = path.join.bind(path);

compiler.outputFileSystem = outputFS;
} else {
compiler.outputFileSystem = outputFileSystem;
}

Expand Down
3 changes: 2 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import compile from './compile';
import getCompiler from './getCompiler';
import source from './source';

export { compile, getCompiler };
export { source, compile, getCompiler };
19 changes: 19 additions & 0 deletions test/helpers/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function getSource(name, stats) {
const { modules } = stats.toJson({ source: true });

for (let i = 0; i < modules.length; i++) {
const module = modules[i];

if (module.modules && module.modules.length > 0) {
for (let j = 0; j < module.modules.length; j++) {
if (module.modules[j].name === name) {
return module.modules[j].source;
}
}
} else if (module.name === name) {
return module.source;
}
}

return undefined;
}

0 comments on commit b146549

Please sign in to comment.