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

Commit

Permalink
feat: new esModules option to output ES modules
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 21, 2019
1 parent cbd1950 commit 0ee2b99
Show file tree
Hide file tree
Showing 10 changed files with 1,334 additions and 738 deletions.
1,773 changes: 1,051 additions & 722 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
"dependencies": {
"loader-utils": "^1.2.3",
"mime": "^2.4.4",
"schema-utils": "^2.4.1"
"schema-utils": "^2.5.0"
},
"devDependencies": {
"@babel/cli": "^7.6.2",
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@webpack-contrib/defaults": "^5.0.2",
Expand All @@ -60,19 +60,19 @@
"cross-env": "^6.0.3",
"del": "^5.1.0",
"del-cli": "^3.0.0",
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.3.0",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.18.2",
"file-loader": "^4.2.0",
"husky": "^3.0.8",
"husky": "^3.1.0",
"jest": "^24.9.0",
"jest-junit": "^8.0.0",
"lint-staged": "^9.4.1",
"memory-fs": "^0.4.1",
"jest-junit": "^9.0.0",
"lint-staged": "^9.4.3",
"memory-fs": "^0.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"standard-version": "^7.0.0",
"webpack": "^4.41.0"
"prettier": "^1.19.1",
"standard-version": "^7.0.1",
"webpack": "^4.41.2"
},
"keywords": [
"webpack"
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default function loader(src) {
src = Buffer.from(src);
}

return `module.exports = ${JSON.stringify(
return `${
options.esModules ? 'export default' : 'module.exports ='
} ${JSON.stringify(
`data:${mimetype || ''};base64,${src.toString('base64')}`
)}`;
}
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"type": "object"
}
]
},
"esModules": {
"type": "boolean"
}
},
"additionalProperties": true
Expand Down
212 changes: 212 additions & 0 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ exports[`validate options 3`] = `
* options.fallback should be an object:
object { loader?, options? }"
`;
exports[`validate options 4`] = `
"Invalid options object. URL Loader has been initialised using an options object that does not match the API schema.
- options.esModules should be a boolean."
`;
Empty file added test/esModules-options.js
Empty file.
8 changes: 6 additions & 2 deletions test/helpers/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ const output = (config) => {
__dirname,
`../outputs/${config.output ? config.output : ''}`
),
filename: '[name].js',
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
};
};

export default function(fixture, config, options) {
// eslint-disable-next-line no-param-reassign
config = {
mode: 'development',
mode: config.mode || 'development',
devtool: config.devtool || 'sourcemap',
context: path.resolve(__dirname, '..', 'fixtures'),
entry: `./${fixture}`,
output: output(config),
module: modules(config),
plugins: plugins(config),
optimization: {
minimize: false,
runtimeChunk: false,
},
};

// eslint-disable-next-line no-param-reassign
Expand Down
37 changes: 37 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,41 @@ describe('Loader', () => {
expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
});

it('should work with ModuleConcatenationPlugin', async () => {
const config = {
mode: 'production',
loader: {
test: /(png|jpg|svg)/,
options: {
esModules: true,
},
},
};

const stats = await webpack('fixture.js', config);

expect(
stats.compilation.assets['main.bundle.js'].source()
).toMatchSnapshot();
});

it('should work with ModuleConcatenationPlugin with fallback', async () => {
const config = {
mode: 'production',
loader: {
test: /(png|jpg|svg)/,
options: {
esModules: true,
limit: 10,
},
},
};

const stats = await webpack('fixture.js', config);

expect(
stats.compilation.assets['main.bundle.js'].source()
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ it('validate options', async () => {
})
).not.toThrow();
expect(() => validate({ fallback: true })).toThrowErrorMatchingSnapshot();

expect(() => validate({ esModules: true })).not.toThrow();
expect(() => validate({ esModules: false })).not.toThrow();
expect(() => validate({ esModules: 'true' })).toThrowErrorMatchingSnapshot();
});

0 comments on commit 0ee2b99

Please sign in to comment.