diff --git a/server/build/babel/plugins/handle-import.js b/server/build/babel/plugins/handle-import.js index 930effa75803ef32a823f1f63c63c7141a33f4ad..164ddd5728700b5a59bb5a9f771a08018480fb63 100644 --- a/server/build/babel/plugins/handle-import.js +++ b/server/build/babel/plugins/handle-import.js @@ -2,7 +2,8 @@ // We've added support for SSR with this version import template from 'babel-template' import syntax from 'babel-plugin-syntax-dynamic-import' -import UUID from 'uuid' +import { dirname, resolve, sep } from 'path' +import Crypto from 'crypto' const TYPE_IMPORT = 'Import' @@ -37,14 +38,33 @@ const buildImport = (args) => (template(` ) `)) +export function getModulePath (sourceFilename, moduleName) { + // resolve only if it's a local module + const modulePath = (moduleName[0] === '.') + ? resolve(dirname(sourceFilename), moduleName) : moduleName + + const cleanedModulePath = modulePath + .replace(/(index){0,1}\.js$/, '') // remove .js, index.js + .replace(/[/\\]$/, '') // remove end slash + + return cleanedModulePath +} + export default () => ({ inherits: syntax, visitor: { - CallExpression (path) { + CallExpression (path, state) { if (path.node.callee.type === TYPE_IMPORT) { const moduleName = path.node.arguments[0].value - const name = `${moduleName.replace(/[^\w]/g, '-')}-${UUID.v4()}` + const sourceFilename = state.file.opts.filename + + const modulePath = getModulePath(sourceFilename, moduleName) + const modulePathHash = Crypto.createHash('md5').update(modulePath).digest('hex') + + const relativeModulePath = modulePath.replace(`${process.cwd()}${sep}`, '') + const name = `${relativeModulePath.replace(/[^\w]/g, '-')}-${modulePathHash}` + const newImport = buildImport({ name })({ diff --git a/test/unit/handle-import-babel-plugin.test.js b/test/unit/handle-import-babel-plugin.test.js new file mode 100644 index 0000000000000000000000000000000000000000..48fb8a58b177af1a4ffcfe32e9eb406f4cabd575 --- /dev/null +++ b/test/unit/handle-import-babel-plugin.test.js @@ -0,0 +1,42 @@ +/* global describe, it, expect */ +import { getModulePath } from '../../dist/server/build/babel/plugins/handle-import' + +function cleanPath (mPath) { + return mPath + .replace(/\\/g, '/') + .replace(/^.*:/, '') +} + +describe('handle-import-babel-plugin', () => { + describe('getModulePath', () => { + it('should not do anything to NPM modules', () => { + const mPath = getModulePath('/abc/pages/about.js', 'cool-module') + expect(mPath).toBe('cool-module') + }) + + it('should not do anything to private NPM modules', () => { + const mPath = getModulePath('/abc/pages/about.js', '@zeithq/cool-module') + expect(mPath).toBe('@zeithq/cool-module') + }) + + it('should resolve local modules', () => { + const mPath = getModulePath('/abc/pages/about.js', '../components/hello.js') + expect(cleanPath(mPath)).toBe('/abc/components/hello') + }) + + it('should remove index.js', () => { + const mPath = getModulePath('/abc/pages/about.js', '../components/c1/index.js') + expect(cleanPath(mPath)).toBe('/abc/components/c1') + }) + + it('should remove .js', () => { + const mPath = getModulePath('/abc/pages/about.js', '../components/bb.js') + expect(cleanPath(mPath)).toBe('/abc/components/bb') + }) + + it('should remove end slash', () => { + const mPath = getModulePath('/abc/pages/about.js', '../components/bb/') + expect(cleanPath(mPath)).toBe('/abc/components/bb') + }) + }) +})