Skip to content
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

Add rudimentary ES2015 module transpilation #26

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"eslint-plugin-flowtype": "^2.33.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsdoc": "^3.1.0",
"file-loader": "^0.11.1",
"file-loader": "^1.0.0",
"html-loader": "^0.5.0",
"istanbul": "^0.4.2",
"mocha": "^3.4.1",
Expand Down
7 changes: 7 additions & 0 deletions src/extractLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function loadModule(request) {
* @returns {string}
*/
function runModule(src, filename, publicPath = "") {
// Rudimentary "transpilation" of ES 2015 modules (e.g file-loader 1.0.0+)
// TODO: Find a better way to "execute" ES 2015 modules in node context
// "Transpile" imports
src = src.replace(/^import\s+(.+)\s+from\s+([^;]+);?$/, "var $1 = require($2);");
// "Transpile" default export (other exports are not supported at the moment)
src = src.replace(/^\s*export\s+default\s*/, "module.exports = ");
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should do that. Replacing strings in the source code is imho too risky and can introduce strange bugs (from the user's perspective). Why not using babel? When there are no plugins, transpiling should be really cheap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree in general. On the other hand it might not be clear to users of extract-loader, that you need babel in order to use file-loader 1.0.0+. Imho we should at least document that problem in the readme or make the "transpilation" in this PR more specific to the file-loader. What do you think?


const script = new vm.Script(src, {
filename,
displayErrors: true,
Expand Down