Skip to content

Commit 8a37a08

Browse files
authored
Fix Sass node module resolution (#69)
1 parent 697f8cf commit 8a37a08

File tree

7 files changed

+79
-40
lines changed

7 files changed

+79
-40
lines changed

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
}
3737
},
3838
"jest": {
39+
"clearMocks": true,
3940
"collectCoverageFrom": [
4041
"src/**/*.{ts}"
4142
],
@@ -82,13 +83,14 @@
8283
"@types/jest": "^24.0.23",
8384
"@types/less": "^3.0.1",
8485
"@types/lodash.camelcase": "^4.3.6",
85-
"@types/node": "^12.12.14",
86+
"@types/node": "^12.12.17",
8687
"@types/postcss-load-config": "^2.0.1",
8788
"@types/postcss-nested": "^4.1.0",
8889
"@types/reserved-words": "^0.1.0",
8990
"@types/sass": "^1.16.0",
90-
"@typescript-eslint/eslint-plugin": "^2.9.0",
91-
"@typescript-eslint/parser": "^2.9.0",
91+
"@typescript-eslint/eslint-plugin": "^2.11.0",
92+
"@typescript-eslint/parser": "^2.11.0",
93+
"bootstrap": "4.4.1",
9294
"eslint": "^6.7.2",
9395
"eslint-config-prettier": "^6.7.0",
9496
"husky": "^3.1.0",
@@ -97,7 +99,7 @@
9799
"postcss-import-sync2": "^1.1.0",
98100
"prettier": "^1.19.1",
99101
"ts-jest": "^24.2.0",
100-
"typescript": "^3.7.2"
102+
"typescript": "^3.7.3"
101103
},
102104
"peerDependencies": {
103105
"typescript": "^3.0.0"

src/helpers/__tests__/__snapshots__/getDtsSnapshot.test.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`utils / cssSnapshots with a Bootstrap import should find external files 1`] = `"bootstrap-module__test---WRms9"`;
4+
35
exports[`utils / cssSnapshots with a custom renderer should process a file and log 1`] = `
46
Object {
57
"exampleFileContents": "exampleFileContents__exampleFileContents---e3Nf2",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import '~bootstrap/scss/bootstrap';
2+
3+
.test {
4+
color: $blue;
5+
}

src/helpers/__tests__/getDtsSnapshot.test.ts

+25-7
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,37 @@ describe('utils / cssSnapshots', () => {
6565
});
6666
});
6767

68+
describe('with a Bootstrap import', () => {
69+
const fullFileName = join(__dirname, 'fixtures', 'bootstrap.module.scss');
70+
const testFile = readFileSync(fullFileName, 'utf8');
71+
72+
it('should find external files', () => {
73+
const classes = getClasses(
74+
processor,
75+
testFile,
76+
fullFileName,
77+
mockOptions,
78+
mockLogger,
79+
);
80+
81+
expect(classes.test).toMatchSnapshot();
82+
});
83+
});
84+
6885
describe('with a custom renderer', () => {
6986
const fullFileName = 'exampleFileContents';
7087
const testFile = 'exampleFileName';
7188
const customRenderer = join(__dirname, 'fixtures', 'customRenderer.js');
72-
const classes = getClasses(
73-
processor,
74-
testFile,
75-
fullFileName,
76-
{ customRenderer },
77-
mockLogger,
78-
);
7989

8090
it('should process a file and log', () => {
91+
const classes = getClasses(
92+
processor,
93+
testFile,
94+
fullFileName,
95+
{ customRenderer },
96+
mockLogger,
97+
);
98+
8199
expect(classes).toMatchSnapshot();
82100
expect(mockLogger.log).toHaveBeenCalledWith('Example log');
83101
});

src/helpers/getClasses.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export const getClasses = (
5858

5959
transformedCss = sass
6060
.renderSync({
61-
data: css,
61+
// NOTE: Solves an issue where tilde imports fail.
62+
// https://github.com/sass/dart-sass/issues/801
63+
// Will strip `~` from imports, unless followed by a slash.
64+
data: css.replace(/(@import ['"])~(?!\/)/gm, '$1'),
6265
indentedSyntax: fileType === FileTypes.sass,
6366
includePaths: [filePath, 'node_modules', ...(includePaths || [])],
6467
...sassOptions,
@@ -74,6 +77,7 @@ export const getClasses = (
7477

7578
return processedCss.root ? extractICSS(processedCss.root).icssExports : {};
7679
} catch (e) {
80+
console.log(e);
7781
logger.error(e);
7882
return {};
7983
}

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ function init({ typescript: ts }: { typescript: typeof tsModule }) {
2727
const logger = createLogger(info);
2828
const directory = info.project.getCurrentDirectory();
2929

30+
// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
31+
process.chdir(directory);
32+
3033
// User options for plugin.
3134
const options: Options = info.config.options || {};
3235
logger.log(`options: ${JSON.stringify(options)}`);

yarn.lock

+33-28
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,10 @@
440440
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.2.tgz#3452a24edf9fea138b48fad4a0a028a683da1e40"
441441
integrity sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA==
442442

443-
"@types/node@^12.12.14":
444-
version "12.12.14"
445-
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2"
446-
integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA==
443+
"@types/node@^12.12.17":
444+
version "12.12.17"
445+
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.17.tgz#191b71e7f4c325ee0fb23bc4a996477d92b8c39b"
446+
integrity sha512-Is+l3mcHvs47sKy+afn2O1rV4ldZFU7W8101cNlOd+MRbjM4Onida8jSZnJdTe/0Pcf25g9BNIUsuugmE6puHA==
447447

448448
"@types/normalize-package-data@^2.4.0":
449449
version "2.4.0"
@@ -499,40 +499,40 @@
499499
dependencies:
500500
"@types/yargs-parser" "*"
501501

502-
"@typescript-eslint/eslint-plugin@^2.9.0":
503-
version "2.9.0"
504-
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.9.0.tgz#fa810282c0e45f6c2310b9c0dfcd25bff97ab7e9"
505-
integrity sha512-98rfOt3NYn5Gr9wekTB8TexxN6oM8ZRvYuphPs1Atfsy419SDLYCaE30aJkRiiTCwGEY98vOhFsEVm7Zs4toQQ==
502+
"@typescript-eslint/eslint-plugin@^2.11.0":
503+
version "2.11.0"
504+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.11.0.tgz#4477c33491ccf0a9a3f4a30ef84978fa0ea0cad2"
505+
integrity sha512-G2HHA1vpMN0EEbUuWubiCCfd0R3a30BB+UdvnFkxwZIxYEGOrWEXDv8tBFO9f44CWc47Xv9lLM3VSn4ORLI2bA==
506506
dependencies:
507-
"@typescript-eslint/experimental-utils" "2.9.0"
507+
"@typescript-eslint/experimental-utils" "2.11.0"
508508
eslint-utils "^1.4.3"
509509
functional-red-black-tree "^1.0.1"
510510
regexpp "^3.0.0"
511511
tsutils "^3.17.1"
512512

513-
"@typescript-eslint/experimental-utils@2.9.0":
514-
version "2.9.0"
515-
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.9.0.tgz#bbe99a8d9510240c055fc4b17230dd0192ba3c7f"
516-
integrity sha512-0lOLFdpdJsCMqMSZT7l7W2ta0+GX8A3iefG3FovJjrX+QR8y6htFlFdU7aOVPL6pDvt6XcsOb8fxk5sq+girTw==
513+
"@typescript-eslint/experimental-utils@2.11.0":
514+
version "2.11.0"
515+
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.11.0.tgz#cef18e6b122706c65248a5d8984a9779ed1e52ac"
516+
integrity sha512-YxcA/y0ZJaCc/fB/MClhcDxHI0nOBB7v2/WxBju2cOTanX7jO9ttQq6Fy4yW9UaY5bPd9xL3cun3lDVqk67sPQ==
517517
dependencies:
518518
"@types/json-schema" "^7.0.3"
519-
"@typescript-eslint/typescript-estree" "2.9.0"
519+
"@typescript-eslint/typescript-estree" "2.11.0"
520520
eslint-scope "^5.0.0"
521521

522-
"@typescript-eslint/parser@^2.9.0":
523-
version "2.9.0"
524-
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.9.0.tgz#2e9cf438de119b143f642a3a406e1e27eb70b7cd"
525-
integrity sha512-fJ+dNs3CCvEsJK2/Vg5c2ZjuQ860ySOAsodDPwBaVlrGvRN+iCNC8kUfLFL8cT49W4GSiLPa/bHiMjYXA7EhKQ==
522+
"@typescript-eslint/parser@^2.11.0":
523+
version "2.11.0"
524+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.11.0.tgz#cdcc3be73ee31cbef089af5ff97ccaa380ef6e8b"
525+
integrity sha512-DyGXeqhb3moMioEFZIHIp7oXBBh7dEfPTzGrlyP0Mi9ScCra4SWEGs3kPd18mG7Sy9Wy8z88zmrw5tSGL6r/6A==
526526
dependencies:
527527
"@types/eslint-visitor-keys" "^1.0.0"
528-
"@typescript-eslint/experimental-utils" "2.9.0"
529-
"@typescript-eslint/typescript-estree" "2.9.0"
528+
"@typescript-eslint/experimental-utils" "2.11.0"
529+
"@typescript-eslint/typescript-estree" "2.11.0"
530530
eslint-visitor-keys "^1.1.0"
531531

532-
"@typescript-eslint/typescript-estree@2.9.0":
533-
version "2.9.0"
534-
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.9.0.tgz#09138daf8f47d0e494ba7db9e77394e928803017"
535-
integrity sha512-v6btSPXEWCP594eZbM+JCXuFoXWXyF/z8kaSBSdCb83DF+Y7+xItW29SsKtSULgLemqJBT+LpT+0ZqdfH7QVmA==
532+
"@typescript-eslint/typescript-estree@2.11.0":
533+
version "2.11.0"
534+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.11.0.tgz#21ada6504274cd1644855926312c798fc697e9fb"
535+
integrity sha512-HGY4+d4MagO6cKMcKfIKaTMxcAv7dEVnji2Zi+vi5VV8uWAM631KjAB5GxFcexMYrwKT0EekRiiGK1/Sd7VFGA==
536536
dependencies:
537537
debug "^4.1.1"
538538
eslint-visitor-keys "^1.1.0"
@@ -843,6 +843,11 @@ binary-extensions@^2.0.0:
843843
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.0.0.tgz#23c0df14f6a88077f5f986c0d167ec03c3d5537c"
844844
integrity sha512-Phlt0plgpIIBOGTT/ehfFnbNlfsDEiqmzE2KRXoX1bLIlir4X/MR+zSyBEkL05ffWgnRSf/DXv+WrUAVr93/ow==
845845

846+
847+
version "4.4.1"
848+
resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-4.4.1.tgz#8582960eea0c5cd2bede84d8b0baf3789c3e8b01"
849+
integrity sha512-tbx5cHubwE6e2ZG7nqM3g/FZ5PQEDMWmMGNrCUBVRPHXTJaH7CBDdsLeu3eCh3B1tzAxTnAbtmrzvWEvT2NNEA==
850+
846851
brace-expansion@^1.1.7:
847852
version "1.1.11"
848853
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
@@ -4985,10 +4990,10 @@ type-fest@^0.8.1:
49854990
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
49864991
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==
49874992

4988-
typescript@^3.7.2:
4989-
version "3.7.2"
4990-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
4991-
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==
4993+
typescript@^3.7.3:
4994+
version "3.7.3"
4995+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.3.tgz#b36840668a16458a7025b9eabfad11b66ab85c69"
4996+
integrity sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==
49924997

49934998
uglify-js@^3.1.4:
49944999
version "3.6.0"

0 commit comments

Comments
 (0)