Skip to content

Commit

Permalink
feat: Use package paths in transpiled
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Aug 2, 2019
1 parent cef7edd commit c9a36d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
13 changes: 12 additions & 1 deletion babel.config.js
@@ -1,3 +1,5 @@
const path = require('path')

const plugins = [
[
'css-modules-transform',
Expand All @@ -20,7 +22,16 @@ module.exports = {
],
env: {
transpilation: {
plugins: plugins,
plugins: [
...plugins,
[
'./scripts/babel-transform-relative-paths-plugin.js',
{
from: path.resolve(__dirname, './react'),
to: 'cozy-ui/transpiled/react'
}
]
],
ignore: ['**/*.spec.jsx', '**/*.spec.js']
},
test: {
Expand Down
30 changes: 30 additions & 0 deletions scripts/babel-transform-relative-paths-plugin.js
@@ -0,0 +1,30 @@
/** Used to resolve relative paths in imports and search/replace in the resolved path */

const { declare } = require('@babel/helper-plugin-utils')
const { types: t } = require('@babel/core')
const path = require('path')

module.exports = declare((api, options) => {
api.assertVersion(7)
return {
name: 'transform-relative-paths',

visitor: {
ImportDeclaration: {
exit({ node }, state) {
if (node.source.value.startsWith('.')) {
const sourcePath = path.dirname(state.file.opts.filename)
const relativePath = node.source.value
const absolutePath = path.resolve(sourcePath, relativePath)
const replaced = absolutePath.replace(options.from, options.to)

// Do not allow absolute paths
if (!replaced.startsWith('/')) {
node.source.value = replaced
}
}
}
}
}
}
})

0 comments on commit c9a36d2

Please sign in to comment.