Skip to content

Commit

Permalink
preserve-symlinks: default to false, to be consistent with nodejs (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Hopf authored and sullenor committed Sep 5, 2017
1 parent 7b700b7 commit 1520c63
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 5 deletions.
14 changes: 12 additions & 2 deletions lib/resolveModule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
'use strict';

const {join, parse, resolve} = require('path');
const {readFileSync, statSync} = require('fs');
const {readFileSync, statSync, realpathSync} = require('fs');
const minimist = require('minimist');

const argv = minimist(process.argv.slice(2), {
boolean: ['preserve-symlinks'],
});

const PRESERVE_SYMLINKS = argv['preserve-symlinks'] ||
String(process.env.NODE_PRESERVE_SYMLINKS) === '1';

exports.applyAliases = applyAliases;
exports.isDirectory = isDirectory;
Expand Down Expand Up @@ -91,6 +99,8 @@ function resolveAsFile(filepath, extensions = []) {
}

function resolveModule(filepath, {cwd, resolve: resolvecfg = {}}) {
const preserveSymlinks = resolvecfg.preserveSymlinks !== undefined
? Boolean(resolvecfg.preserveSymlinks) : PRESERVE_SYMLINKS;
const file = applyAliases(filepath, resolvecfg.alias);
const dirs = isNodeModule(file)
? (resolvecfg.modules || []).concat(nodeModulesPaths(cwd))
Expand All @@ -105,6 +115,6 @@ function resolveModule(filepath, {cwd, resolve: resolvecfg = {}}) {
const result = resolveAsFile(abspath, resolvecfg.extensions) ||
resolveAsDir(abspath, resolvecfg.mainFile);

if (result) return result;
if (result) return preserveSymlinks ? result : realpathSync(result);
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
"eslint": "^3",
"jest": "^20.0.3",
"postcss": "^6.0.1",
"postcss-icss-values": "2.0.1",
"postcss-modules-extract-imports": "^1.1.0",
"postcss-modules-local-by-default": "^1.2.0",
"postcss-modules-scope": "^1.1.0",
"postcss-icss-values": "2.0.1"
"postcss-modules-scope": "^1.1.0"
},
"dependencies": {
"css-selector-tokenizer": "^0.7.0",
"icss-utils": "^3.0.1"
"icss-utils": "^3.0.1",
"minimist": "^1.2.0"
}
}
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ Provide additional directories to check the modules in. Should be absolute paths
Specifies the default filename to be used while resolving directories. Default: `index.css`.


`resolve.preserveSymlinks` `boolean`

Wether to resolve symlinks in paths. Defaults to nodejs behaviour: `false`,
(parsed from `--preserve-symlinks` or environment variable `PRESERVE_SYMLINKS`).


## Reference Guides

- Interoperable CSS: https://github.com/css-modules/icss
Expand Down
1 change: 1 addition & 0 deletions test/case/opts-preserve-symlink/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!node_modules
67 changes: 67 additions & 0 deletions test/case/opts-preserve-symlink/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`opts-preserve-symlinks default false 1`] = `
"/* imported from lib/button/index.css */
._index_button {
color: green;
}
/* imported from source.css */
._source_continueButton
{
color: green;
}
"
`;

exports[`opts-preserve-symlinks default false 2`] = `
Object {
"continueButton": "_source_continueButton _index_button",
}
`;

exports[`opts-preserve-symlinks false 1`] = `
"/* imported from lib/button/index.css */
._index_button {
color: green;
}
/* imported from source.css */
._source_continueButton
{
color: green;
}
"
`;

exports[`opts-preserve-symlinks false 2`] = `
Object {
"continueButton": "_source_continueButton _index_button",
}
`;

exports[`opts-preserve-symlinks true 1`] = `
"/* imported from node_modules/button/index.css */
._index_button {
color: green;
}
/* imported from source.css */
._source_continueButton
{
color: green;
}
"
`;

exports[`opts-preserve-symlinks true 2`] = `
Object {
"continueButton": "_source_continueButton _index_button",
}
`;
3 changes: 3 additions & 0 deletions test/case/opts-preserve-symlink/lib/button/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.button {
color: green;
}
1 change: 1 addition & 0 deletions test/case/opts-preserve-symlink/node_modules/button

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/case/opts-preserve-symlink/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.continueButton
{
composes: button from 'button';
color: green;
}
49 changes: 49 additions & 0 deletions test/case/opts-preserve-symlink/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const resolveImports = require('../../../index');
const setup = require('../../setup');

test('opts-preserve-symlinks true', () => {
const {resulting, exports: tokens} = setup(
'local-by-default',
'extract-imports',
'scope',
resolveImports({
resolve: {
preserveSymlinks: true,
},
})
)(__dirname);

expect(resulting).toMatchSnapshot();
expect(tokens).toMatchSnapshot();
});

test('opts-preserve-symlinks false', () => {
const {resulting, exports: tokens} = setup(
'local-by-default',
'extract-imports',
'scope',
resolveImports({
resolve: {
preserveSymlinks: false,
},
})
)(__dirname);

expect(resulting).toMatchSnapshot();
expect(tokens).toMatchSnapshot();
});

test('opts-preserve-symlinks default false', () => {
const {resulting, exports: tokens} = setup(
'local-by-default',
'extract-imports',
'scope',
// nodejs behaviour: defaults to false
resolveImports({})
)(__dirname);

expect(resulting).toMatchSnapshot();
expect(tokens).toMatchSnapshot();
});

0 comments on commit 1520c63

Please sign in to comment.