Skip to content

Fix Sass node module resolution #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
}
},
"jest": {
"clearMocks": true,
"collectCoverageFrom": [
"src/**/*.{ts}"
],
Expand Down Expand Up @@ -82,13 +83,14 @@
"@types/jest": "^24.0.23",
"@types/less": "^3.0.1",
"@types/lodash.camelcase": "^4.3.6",
"@types/node": "^12.12.14",
"@types/node": "^12.12.17",
"@types/postcss-load-config": "^2.0.1",
"@types/postcss-nested": "^4.1.0",
"@types/reserved-words": "^0.1.0",
"@types/sass": "^1.16.0",
"@typescript-eslint/eslint-plugin": "^2.9.0",
"@typescript-eslint/parser": "^2.9.0",
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"bootstrap": "4.4.1",
"eslint": "^6.7.2",
"eslint-config-prettier": "^6.7.0",
"husky": "^3.1.0",
Expand All @@ -97,7 +99,7 @@
"postcss-import-sync2": "^1.1.0",
"prettier": "^1.19.1",
"ts-jest": "^24.2.0",
"typescript": "^3.7.2"
"typescript": "^3.7.3"
},
"peerDependencies": {
"typescript": "^3.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`utils / cssSnapshots with a Bootstrap import should find external files 1`] = `"bootstrap-module__test---WRms9"`;

exports[`utils / cssSnapshots with a custom renderer should process a file and log 1`] = `
Object {
"exampleFileContents": "exampleFileContents__exampleFileContents---e3Nf2",
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/__tests__/fixtures/bootstrap.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import '~bootstrap/scss/bootstrap';

.test {
color: $blue;
}
32 changes: 25 additions & 7 deletions src/helpers/__tests__/getDtsSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,37 @@ describe('utils / cssSnapshots', () => {
});
});

describe('with a Bootstrap import', () => {
const fullFileName = join(__dirname, 'fixtures', 'bootstrap.module.scss');
const testFile = readFileSync(fullFileName, 'utf8');

it('should find external files', () => {
const classes = getClasses(
processor,
testFile,
fullFileName,
mockOptions,
mockLogger,
);

expect(classes.test).toMatchSnapshot();
});
});

describe('with a custom renderer', () => {
const fullFileName = 'exampleFileContents';
const testFile = 'exampleFileName';
const customRenderer = join(__dirname, 'fixtures', 'customRenderer.js');
const classes = getClasses(
processor,
testFile,
fullFileName,
{ customRenderer },
mockLogger,
);

it('should process a file and log', () => {
const classes = getClasses(
processor,
testFile,
fullFileName,
{ customRenderer },
mockLogger,
);

expect(classes).toMatchSnapshot();
expect(mockLogger.log).toHaveBeenCalledWith('Example log');
});
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/getClasses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const getClasses = (

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

return processedCss.root ? extractICSS(processedCss.root).icssExports : {};
} catch (e) {
console.log(e);
logger.error(e);
return {};
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function init({ typescript: ts }: { typescript: typeof tsModule }) {
const logger = createLogger(info);
const directory = info.project.getCurrentDirectory();

// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
process.chdir(directory);

// User options for plugin.
const options: Options = info.config.options || {};
logger.log(`options: ${JSON.stringify(options)}`);
Expand Down
61 changes: 33 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.2.tgz#3452a24edf9fea138b48fad4a0a028a683da1e40"
integrity sha512-5tabW/i+9mhrfEOUcLDu2xBPsHJ+X5Orqy9FKpale3SjDA17j5AEpYq5vfy3oAeAHGcvANRCO3NV3d2D6q3NiA==

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

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
Expand Down Expand Up @@ -499,40 +499,40 @@
dependencies:
"@types/yargs-parser" "*"

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

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

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

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

[email protected]:
version "4.4.1"
resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-4.4.1.tgz#8582960eea0c5cd2bede84d8b0baf3789c3e8b01"
integrity sha512-tbx5cHubwE6e2ZG7nqM3g/FZ5PQEDMWmMGNrCUBVRPHXTJaH7CBDdsLeu3eCh3B1tzAxTnAbtmrzvWEvT2NNEA==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -4985,10 +4990,10 @@ type-fest@^0.8.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

typescript@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.2.tgz#27e489b95fa5909445e9fef5ee48d81697ad18fb"
integrity sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==
typescript@^3.7.3:
version "3.7.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.3.tgz#b36840668a16458a7025b9eabfad11b66ab85c69"
integrity sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==

uglify-js@^3.1.4:
version "3.6.0"
Expand Down