Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit d570a36

Browse files
committed
feat(infrastructure): Create script for that rewrites .scss imports (#831)
1 parent 548b4bc commit d570a36

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"recast": "^0.12.3",
7676
"resolve": "^1.3.2",
7777
"sass-loader": "^6.0.4",
78+
"scss-parser": "^1.0.0",
7879
"semver": "^5.3.0",
7980
"standard-changelog": "0.0.1",
8081
"style-loader": "^0.18.0",
@@ -86,6 +87,7 @@
8687
"stylelint-selector-bem-pattern": "^1.0.0",
8788
"testdouble": "3.0.0",
8889
"to-slug-case": "^1.0.0",
90+
"query-ast": "^1.0.1",
8991
"validate-commit-msg": "^2.6.1",
9092
"webpack": "^2.2.1",
9193
"webpack-dev-server": "^2.4.3"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

scripts/sass-closure-rewriter.sh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
# Rewrites our .scss files to be compatible with closure-stylesheets
4+
# in our internal Blaze infrastructure.
5+
6+
##
7+
# Copyright 2017 Google Inc. All Rights Reserved.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
set -e
23+
24+
function log() {
25+
echo -e "\033[36m[closure-rewriter]\033[0m" "$@"
26+
}
27+
28+
CLOSURE_TMP=.closure-tmp
29+
CLOSURE_PKGDIR=$CLOSURE_TMP/packages
30+
31+
log "Prepping packages for rewrite"
32+
33+
rm -fr $CLOSURE_TMP/**
34+
mkdir -p $CLOSURE_PKGDIR
35+
PACKAGE_NAMES=$(ls packages)
36+
for pkg in $PACKAGE_NAMES ; do
37+
if [[ $pkg != *"mdc-"* ]]; then
38+
continue
39+
fi
40+
cp -r "packages/$pkg" $CLOSURE_PKGDIR
41+
done
42+
rm -fr $CLOSURE_PKGDIR/**/{node_modules,dist}
43+
44+
log "Rewriting all import statements to be closure compatible"
45+
node scripts/rewrite-sass-import-statements-for-closure.js $CLOSURE_PKGDIR

0 commit comments

Comments
 (0)