From 1520c635a1fd2e2762995bf97dbaeb5eb065b3ce Mon Sep 17 00:00:00 2001 From: Carl Hopf Date: Tue, 5 Sep 2017 05:42:56 +0200 Subject: [PATCH] preserve-symlinks: default to false, to be consistent with nodejs (#28) read more: https://github.com/browserify/browserify/pull/1742 https://github.com/nodejs/node/issues/3402 --- lib/resolveModule.js | 14 +++- package.json | 7 +- readme.md | 6 ++ test/case/opts-preserve-symlink/.gitignore | 1 + .../__snapshots__/test.js.snap | 67 +++++++++++++++++++ .../lib/button/index.css | 3 + .../opts-preserve-symlink/node_modules/button | 1 + test/case/opts-preserve-symlink/source.css | 5 ++ test/case/opts-preserve-symlink/test.js | 49 ++++++++++++++ 9 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 test/case/opts-preserve-symlink/.gitignore create mode 100644 test/case/opts-preserve-symlink/__snapshots__/test.js.snap create mode 100644 test/case/opts-preserve-symlink/lib/button/index.css create mode 120000 test/case/opts-preserve-symlink/node_modules/button create mode 100644 test/case/opts-preserve-symlink/source.css create mode 100644 test/case/opts-preserve-symlink/test.js diff --git a/lib/resolveModule.js b/lib/resolveModule.js index b9fbf47..0a9083d 100644 --- a/lib/resolveModule.js +++ b/lib/resolveModule.js @@ -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; @@ -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)) @@ -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); } } diff --git a/package.json b/package.json index 2d15641..07d3aa1 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/readme.md b/readme.md index 1be7214..e2d7c82 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/case/opts-preserve-symlink/.gitignore b/test/case/opts-preserve-symlink/.gitignore new file mode 100644 index 0000000..cf4bab9 --- /dev/null +++ b/test/case/opts-preserve-symlink/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/test/case/opts-preserve-symlink/__snapshots__/test.js.snap b/test/case/opts-preserve-symlink/__snapshots__/test.js.snap new file mode 100644 index 0000000..6c6b2af --- /dev/null +++ b/test/case/opts-preserve-symlink/__snapshots__/test.js.snap @@ -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", +} +`; diff --git a/test/case/opts-preserve-symlink/lib/button/index.css b/test/case/opts-preserve-symlink/lib/button/index.css new file mode 100644 index 0000000..2546f2b --- /dev/null +++ b/test/case/opts-preserve-symlink/lib/button/index.css @@ -0,0 +1,3 @@ +.button { + color: green; +} diff --git a/test/case/opts-preserve-symlink/node_modules/button b/test/case/opts-preserve-symlink/node_modules/button new file mode 120000 index 0000000..2da431d --- /dev/null +++ b/test/case/opts-preserve-symlink/node_modules/button @@ -0,0 +1 @@ +../lib/button/ \ No newline at end of file diff --git a/test/case/opts-preserve-symlink/source.css b/test/case/opts-preserve-symlink/source.css new file mode 100644 index 0000000..422a3c8 --- /dev/null +++ b/test/case/opts-preserve-symlink/source.css @@ -0,0 +1,5 @@ +.continueButton +{ + composes: button from 'button'; + color: green; +} diff --git a/test/case/opts-preserve-symlink/test.js b/test/case/opts-preserve-symlink/test.js new file mode 100644 index 0000000..f1392cc --- /dev/null +++ b/test/case/opts-preserve-symlink/test.js @@ -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(); +});