Skip to content

Commit

Permalink
Merge pull request #37 from snowyu/bug/local-file-missing-ext
Browse files Browse the repository at this point in the history
fix: the babel-plugin should add the js extension name for importing local file.
  • Loading branch information
FredKSchott committed Jul 25, 2019
2 parents 75cb18c + c7c19d7 commit 5fc6306
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions assets/babel-plugin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const path = require('path');

// A lame copy-paste from src/index.ts
function getWebDependencyName(dep) {
return dep.replace(/\.js$/, '');
}

function rewriteBareModuleImport(imp) {
function rewriteBareModuleImport(imp, addMissingJsExtensions) {
const extname = addMissingJsExtensions && '.js';
if (imp.startsWith('/') || imp.startsWith('.')|| imp.startsWith('\\')) {
if (extname && !path.extname(imp)) imp += extname;
return imp;
}
return `/web_modules/${getWebDependencyName(imp)}.js`;
}

module.exports = function pikaWebBabelTransform({types: t}) {
module.exports = function pikaWebBabelTransform({types: t}, options) {
const addMissingJsExtensions = options.addMissingJsExtensions;
const rewriteBareModuleName = options.rewriteBareModuleName || rewriteBareModuleImport;
return {
visitor: {
CallExpression(path, {file, opts}) {
Expand All @@ -26,7 +32,7 @@ module.exports = function pikaWebBabelTransform({types: t}) {


source.replaceWith(
t.stringLiteral(rewriteBareModuleImport(source.node.value)),
t.stringLiteral(rewriteBareModuleName(source.node.value, addMissingJsExtensions)),
);
},
'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(path, {file, opts}) {
Expand All @@ -38,7 +44,7 @@ module.exports = function pikaWebBabelTransform({types: t}) {
}

source.replaceWith(
t.stringLiteral(rewriteBareModuleImport(source.node.value)),
t.stringLiteral(rewriteBareModuleName(source.node.value, addMissingJsExtensions)),
);
},
},
Expand Down

0 comments on commit 5fc6306

Please sign in to comment.