Skip to content

fix: enforce esm #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export default (name, stats) => {
  const { modules } = stats.toJson({ source: true });
  const module = modules.find((m) => m.name === name);
  let { source } = module;

  return source;
};

Just snapshot module using name

Copy link
Contributor Author

@vankop vankop Jun 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, let me clean up and squash commit..

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to PR fix, I just wanted to look on output when was testing locally and found a bug with outputFileSystem option..


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;
}