5858 * - https://github.com/google/closure-compiler/issues/2386
5959 *
6060 * Note that for third-party modules, they must be defined in closure_externs.js. See that file for more info.
61+ * Also note that this works on `export .... from ...` as well.
6162 */
6263
6364const assert = require ( 'assert' ) ;
@@ -94,8 +95,10 @@ function transform(srcFile, rootDir) {
9495 } ) ;
9596
9697 traverse ( ast , {
97- 'ImportDeclaration' ( { node} ) {
98- rewriteImportDeclaration ( node , srcFile , rootDir ) ;
98+ 'ImportDeclaration|ExportNamedDeclaration' ( { node} ) {
99+ if ( node . source ) {
100+ rewriteDeclarationSource ( node , srcFile , rootDir ) ;
101+ }
99102 } ,
100103 } ) ;
101104
@@ -113,38 +116,39 @@ function transform(srcFile, rootDir) {
113116 console . log ( `[rewrite] ${ srcFile } ` ) ;
114117}
115118
116- function rewriteImportDeclaration ( node , srcFile , rootDir ) {
117- let importSource = node . source . value ;
118- const pathParts = importSource . split ( '/' ) ;
119+ function rewriteDeclarationSource ( node , srcFile , rootDir ) {
120+ let source = node . source . value ;
121+ const pathParts = source . split ( '/' ) ;
119122 const isMDCImport = pathParts [ 0 ] === '@material' ;
120123 if ( isMDCImport ) {
121124 const modName = pathParts [ 1 ] ; // @material /<modName>
122125 const atMaterialReplacementPath = `${ rootDir } /mdc-${ modName } ` ;
123- const rewrittenImportSource = [ atMaterialReplacementPath ] . concat ( pathParts . slice ( 2 ) ) . join ( '/' ) ;
124- importSource = rewrittenImportSource ;
126+ const rewrittenSource = [ atMaterialReplacementPath ] . concat ( pathParts . slice ( 2 ) ) . join ( '/' ) ;
127+ source = rewrittenSource ;
125128 }
126129
127- patchNodeForImportSource ( importSource , srcFile , node ) ;
130+ patchNodeForDeclarationSource ( source , srcFile , rootDir , node ) ;
128131}
129132
130- function patchNodeForImportSource ( importSource , srcFile , node ) {
131- let resolvedImportSource = importSource ;
133+ function patchNodeForDeclarationSource ( source , srcFile , rootDir , node ) {
134+ let resolvedSource = source ;
132135 // See: https://nodejs.org/api/modules.html#modules_all_together (step 3)
133- const wouldLoadAsFileOrDir = [ './' , '/' , '../' ] . some ( ( s ) => importSource . indexOf ( s ) === 0 ) ;
136+ const wouldLoadAsFileOrDir = [ './' , '/' , '../' ] . some ( ( s ) => source . indexOf ( s ) === 0 ) ;
134137 const isThirdPartyModule = ! wouldLoadAsFileOrDir ;
135138 if ( isThirdPartyModule ) {
136- assert ( importSource . indexOf ( '@material' ) < 0 , '@material/* import sources should have already been rewritten' ) ;
139+ assert ( source . indexOf ( '@material' ) < 0 , '@material/* import sources should have already been rewritten' ) ;
137140 patchDefaultImportIfNeeded ( node ) ;
138- resolvedImportSource = `goog:mdc.thirdparty.${ camelCase ( importSource ) } ` ;
141+ resolvedSource = `goog:mdc.thirdparty.${ camelCase ( source ) } ` ;
139142 } else {
140- const needsClosureModuleRootResolution = path . isAbsolute ( importSource ) ;
143+ const normPath = path . normalize ( path . dirname ( srcFile ) , source ) ;
144+ const needsClosureModuleRootResolution = path . isAbsolute ( source ) || fs . statSync ( normPath ) . isDirectory ( ) ;
141145 if ( needsClosureModuleRootResolution ) {
142- resolvedImportSource = path . relative ( rootDir , resolve . sync ( importSource , {
146+ resolvedSource = path . relative ( rootDir , resolve . sync ( source , {
143147 basedir : path . dirname ( srcFile ) ,
144148 } ) ) ;
145149 }
146150 }
147- node . source = t . stringLiteral ( resolvedImportSource ) ;
151+ node . source = t . stringLiteral ( resolvedSource ) ;
148152}
149153
150154function patchDefaultImportIfNeeded ( node ) {
0 commit comments