Skip to content

Commit

Permalink
update babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Aug 5, 2019
1 parent 85442a4 commit 84d7429
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Additionally, @pika/web runs all dependencies through Babel via `@preset/env` to

```js
/* package.json - Recommended target for ESM-supporting browsers (default) */
"browserslist": ">0.5%, not ie 11, not op_mini all"
"browserslist": ">0.75%, not ie 11, not op_mini all"
```

## Options
Expand Down
11 changes: 6 additions & 5 deletions assets/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ function getWebDependencyName(dep) {
return dep.replace(/\.js$/, '');
}

function rewriteImport(imp, shouldAddMissingExtension) {
function rewriteImport(imp, dir, shouldAddMissingExtension) {
const isSourceImport = imp.startsWith('/') || imp.startsWith('.')|| imp.startsWith('\\');
dir = dir || 'web_modules';
if (!isSourceImport) {
return `/web_modules/${getWebDependencyName(imp)}.js`;
return path.posix.join('/', dir, `${getWebDependencyName(imp)}.js`);
}
if (shouldAddMissingExtension && !path.extname(imp)) {
return imp + '.js';
Expand All @@ -21,7 +22,7 @@ function rewriteImport(imp, shouldAddMissingExtension) {
* partial imports is missing in the browser and being phased out of Node.js, but
* this can be a useful option for migrating an old project to @pika/web.
*/
module.exports = function pikaWebBabelTransform({types: t}, {optionalExtensions}) {
module.exports = function pikaWebBabelTransform({types: t}, {optionalExtensions, dir}) {
return {
visitor: {
CallExpression(path, {file, opts}) {
Expand All @@ -37,7 +38,7 @@ module.exports = function pikaWebBabelTransform({types: t}, {optionalExtensions}


source.replaceWith(
t.stringLiteral(rewriteImport(source.node.value, optionalExtensions)),
t.stringLiteral(rewriteImport(source.node.value, dir, optionalExtensions)),
);
},
'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(path, {file, opts}) {
Expand All @@ -49,7 +50,7 @@ module.exports = function pikaWebBabelTransform({types: t}, {optionalExtensions}
}

source.replaceWith(
t.stringLiteral(rewriteImport(source.node.value, optionalExtensions)),
t.stringLiteral(rewriteImport(source.node.value, dir, optionalExtensions)),
);
},
},
Expand Down

0 comments on commit 84d7429

Please sign in to comment.