|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @fileoverview Rewrites import statements such that: |
| 19 | + * |
| 20 | + * ```js |
| 21 | + * import [<SPECIFIERS> from] '@material/$PKG[/files...]'; |
| 22 | + * ``` |
| 23 | + * becomes |
| 24 | + * ```js |
| 25 | + * import [<SPECIFIERS> from] 'mdc-$PKG/<RESOLVED_FILE_PATH>'; |
| 26 | + * ``` |
| 27 | + * The RESOLVED_FILE_PATH is the file that node's module resolution algorithm would have resolved the import |
| 28 | + * source to. |
| 29 | + */ |
| 30 | + |
| 31 | +const fs = require('fs'); |
| 32 | +const path = require('path'); |
| 33 | + |
| 34 | +const {parse, stringify} = require('scss-parser'); |
| 35 | +const createQueryWrapper = require('query-ast'); |
| 36 | +const glob = require('glob'); |
| 37 | + |
| 38 | +main(process.argv); |
| 39 | + |
| 40 | +function main(argv) { |
| 41 | + if (argv.length < 3) { |
| 42 | + console.error('Missing root directory path'); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + const rootDir = path.resolve(process.argv[2]); |
| 47 | + const srcFiles = glob.sync(`${rootDir}/**/*.scss`); |
| 48 | + srcFiles.forEach((srcFile) => transform(srcFile, rootDir)); |
| 49 | +} |
| 50 | + |
| 51 | +function transform(srcFile, rootDir) { |
| 52 | + const src = fs.readFileSync(srcFile, 'utf8'); |
| 53 | + const ast = parse(src); |
| 54 | + |
| 55 | + const $ = createQueryWrapper(ast); |
| 56 | + $('atrule').has('atkeyword').find('string_double').replace((n) => { |
| 57 | + if (n.parent.children[0].node.value === 'import') { |
| 58 | + return { |
| 59 | + type: 'string_double', |
| 60 | + value: rewriteImportDeclaration(n.node.value, srcFile, rootDir), |
| 61 | + }; |
| 62 | + } |
| 63 | + return n.node; |
| 64 | + }); |
| 65 | + |
| 66 | + const scss = stringify($().get(0)); |
| 67 | + |
| 68 | + fs.writeFileSync(srcFile, scss, 'utf8'); |
| 69 | + console.log(`[rewrite] ${srcFile}`); |
| 70 | +} |
| 71 | + |
| 72 | +function rewriteImportDeclaration(importSource, srcFile, rootDir) { |
| 73 | + const pathParts = importSource.split('/'); |
| 74 | + const isMDCImport = pathParts[0] === '@material'; |
| 75 | + if (isMDCImport) { |
| 76 | + const modName = pathParts[1]; // @material/<modName> |
| 77 | + const atMaterialReplacementPath = `${rootDir}/${modName}`; |
| 78 | + const rewrittenImportSource = [atMaterialReplacementPath].concat(pathParts.slice(2)).join('/'); |
| 79 | + importSource = rewrittenImportSource; |
| 80 | + } |
| 81 | + |
| 82 | + let resolvedImportSource = importSource; |
| 83 | + const needsClosureModuleRootResolution = path.isAbsolute(importSource); |
| 84 | + if (needsClosureModuleRootResolution) { |
| 85 | + const pathToImport = importSource.replace('@material', rootDir); |
| 86 | + resolvedImportSource = path.relative(path.dirname(srcFile), pathToImport); |
| 87 | + } |
| 88 | + return resolvedImportSource; |
| 89 | +} |
0 commit comments