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

Commit 0ee2b99

Browse files
feat: new esModules option to output ES modules
1 parent cbd1950 commit 0ee2b99

File tree

10 files changed

+1334
-738
lines changed

10 files changed

+1334
-738
lines changed

package-lock.json

Lines changed: 1051 additions & 722 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
"dependencies": {
4646
"loader-utils": "^1.2.3",
4747
"mime": "^2.4.4",
48-
"schema-utils": "^2.4.1"
48+
"schema-utils": "^2.5.0"
4949
},
5050
"devDependencies": {
51-
"@babel/cli": "^7.6.2",
52-
"@babel/core": "^7.6.2",
53-
"@babel/preset-env": "^7.6.2",
51+
"@babel/cli": "^7.7.0",
52+
"@babel/core": "^7.7.2",
53+
"@babel/preset-env": "^7.7.1",
5454
"@commitlint/cli": "^8.2.0",
5555
"@commitlint/config-conventional": "^8.2.0",
5656
"@webpack-contrib/defaults": "^5.0.2",
@@ -60,19 +60,19 @@
6060
"cross-env": "^6.0.3",
6161
"del": "^5.1.0",
6262
"del-cli": "^3.0.0",
63-
"eslint": "^6.5.1",
64-
"eslint-config-prettier": "^6.3.0",
63+
"eslint": "^6.6.0",
64+
"eslint-config-prettier": "^6.7.0",
6565
"eslint-plugin-import": "^2.18.2",
6666
"file-loader": "^4.2.0",
67-
"husky": "^3.0.8",
67+
"husky": "^3.1.0",
6868
"jest": "^24.9.0",
69-
"jest-junit": "^8.0.0",
70-
"lint-staged": "^9.4.1",
71-
"memory-fs": "^0.4.1",
69+
"jest-junit": "^9.0.0",
70+
"lint-staged": "^9.4.3",
71+
"memory-fs": "^0.5.0",
7272
"npm-run-all": "^4.1.5",
73-
"prettier": "^1.18.2",
74-
"standard-version": "^7.0.0",
75-
"webpack": "^4.41.0"
73+
"prettier": "^1.19.1",
74+
"standard-version": "^7.0.1",
75+
"webpack": "^4.41.2"
7676
},
7777
"keywords": [
7878
"webpack"

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export default function loader(src) {
4646
src = Buffer.from(src);
4747
}
4848

49-
return `module.exports = ${JSON.stringify(
49+
return `${
50+
options.esModules ? 'export default' : 'module.exports ='
51+
} ${JSON.stringify(
5052
`data:${mimetype || ''};base64,${src.toString('base64')}`
5153
)}`;
5254
}

src/options.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"type": "object"
3838
}
3939
]
40+
},
41+
"esModules": {
42+
"type": "boolean"
4043
}
4144
},
4245
"additionalProperties": true

test/__snapshots__/loader.test.js.snap

Lines changed: 212 additions & 0 deletions
Large diffs are not rendered by default.

test/__snapshots__/validate-options.test.js.snap renamed to test/__snapshots__/validation-options.test.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ exports[`validate options 3`] = `
2323
* options.fallback should be an object:
2424
object { loader?, options? }"
2525
`;
26+
27+
exports[`validate options 4`] = `
28+
"Invalid options object. URL Loader has been initialised using an options object that does not match the API schema.
29+
- options.esModules should be a boolean."
30+
`;

test/esModules-options.js

Whitespace-only changes.

test/helpers/compiler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ const output = (config) => {
3030
__dirname,
3131
`../outputs/${config.output ? config.output : ''}`
3232
),
33-
filename: '[name].js',
33+
filename: '[name].bundle.js',
3434
chunkFilename: '[name].chunk.js',
3535
};
3636
};
3737

3838
export default function(fixture, config, options) {
3939
// eslint-disable-next-line no-param-reassign
4040
config = {
41-
mode: 'development',
41+
mode: config.mode || 'development',
4242
devtool: config.devtool || 'sourcemap',
4343
context: path.resolve(__dirname, '..', 'fixtures'),
4444
entry: `./${fixture}`,
4545
output: output(config),
4646
module: modules(config),
4747
plugins: plugins(config),
48+
optimization: {
49+
minimize: false,
50+
runtimeChunk: false,
51+
},
4852
};
4953

5054
// eslint-disable-next-line no-param-reassign

test/loader.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,41 @@ describe('Loader', () => {
3636
expect(errors).toMatchSnapshot('errors');
3737
expect(warnings).toMatchSnapshot('warnings');
3838
});
39+
40+
it('should work with ModuleConcatenationPlugin', async () => {
41+
const config = {
42+
mode: 'production',
43+
loader: {
44+
test: /(png|jpg|svg)/,
45+
options: {
46+
esModules: true,
47+
},
48+
},
49+
};
50+
51+
const stats = await webpack('fixture.js', config);
52+
53+
expect(
54+
stats.compilation.assets['main.bundle.js'].source()
55+
).toMatchSnapshot();
56+
});
57+
58+
it('should work with ModuleConcatenationPlugin with fallback', async () => {
59+
const config = {
60+
mode: 'production',
61+
loader: {
62+
test: /(png|jpg|svg)/,
63+
options: {
64+
esModules: true,
65+
limit: 10,
66+
},
67+
},
68+
};
69+
70+
const stats = await webpack('fixture.js', config);
71+
72+
expect(
73+
stats.compilation.assets['main.bundle.js'].source()
74+
).toMatchSnapshot();
75+
});
3976
});

test/validate-options.test.js renamed to test/validation-options.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ it('validate options', async () => {
4242
})
4343
).not.toThrow();
4444
expect(() => validate({ fallback: true })).toThrowErrorMatchingSnapshot();
45+
46+
expect(() => validate({ esModules: true })).not.toThrow();
47+
expect(() => validate({ esModules: false })).not.toThrow();
48+
expect(() => validate({ esModules: 'true' })).toThrowErrorMatchingSnapshot();
4549
});

0 commit comments

Comments
 (0)