diff --git a/lib/bundle/config.js b/lib/bundle/config.js index 7e020fa..449c633 100644 --- a/lib/bundle/config.js +++ b/lib/bundle/config.js @@ -88,8 +88,13 @@ function generateConfig({ externals, format, exports, // eslint-disable-next-lin plugins = plugins.concat([ nodeResolve({ extensions }), - commonjs({ include: "node_modules/**" }) + commonjs({ include: /node_modules/ }) ]); + + if(process.env.FAUCET_EXPERIMENTAL_SYMLINKS === "true") { + cfg.preserveSymlinks = true; + } + if(compact) { cfg.compact = true; plugins = plugins.concat(determineCompacting(compact)); @@ -111,7 +116,7 @@ function generateConfig({ externals, format, exports, // eslint-disable-next-lin } // distinguish between (roughly) read and write settings - let read = ["external", "plugins"]; + let read = ["external", "plugins", "preserveSymlinks"]; return Object.keys(cfg).reduce((memo, key) => { let type = read.includes(key) ? "readConfig" : "writeConfig"; memo[type][key] = cfg[key]; diff --git a/test/unit/fixtures/external/cjs-module/index.js b/test/unit/fixtures/external/cjs-module/index.js new file mode 100644 index 0000000..785a4fc --- /dev/null +++ b/test/unit/fixtures/external/cjs-module/index.js @@ -0,0 +1,3 @@ +exports.dummy = { + some: "dummy value" +}; diff --git a/test/unit/fixtures/src/import-symlinked.js b/test/unit/fixtures/src/import-symlinked.js new file mode 100644 index 0000000..08813b1 --- /dev/null +++ b/test/unit/fixtures/src/import-symlinked.js @@ -0,0 +1,3 @@ +import { dummy } from "some-cjs-module"; + +console.log(dummy); // eslint-disable-line no-console diff --git a/test/unit/test_bundling.js b/test/unit/test_bundling.js index c3f7d39..7da6d48 100644 --- a/test/unit/test_bundling.js +++ b/test/unit/test_bundling.js @@ -3,6 +3,7 @@ let { MockAssetManager, makeBundle, FIXTURES_DIR } = require("./util"); let faucetJS = require("../../lib").plugin; +const fs = require("fs"); let path = require("path"); let assert = require("assert"); @@ -412,4 +413,49 @@ console.log(\`[DUMMY] $\{util}\`); // eslint-disable-line no-console assetManager.assertWriteCount(1); }); }); + + describe("working with linked modules", () => { + // in case some set this env variable before + let initialFlagValue = process.env.FAUCET_EXPERIMENTAL_SYMLINKS; + + beforeEach(() => { + fs.symlinkSync( + path.resolve(FIXTURES_DIR, "./external/cjs-module"), + path.resolve(FIXTURES_DIR, "./node_modules/some-cjs-module"), + "dir" + ); + + process.env.FAUCET_EXPERIMENTAL_SYMLINKS = "true"; + }); + + afterEach(() => { + fs.unlinkSync(path.resolve(FIXTURES_DIR, "./node_modules/some-cjs-module")); + process.env.FAUCET_EXPERIMENTAL_SYMLINKS = initialFlagValue; + }); + + it("should preserve symlinks when env varialbe is set", () => { + let config = [{ + source: "./src/import-symlinked.js", + target: "./dist/bundle.js", + format: "CommonJS" + }]; + let assetManager = new MockAssetManager(FIXTURES_DIR); + + return faucetJS(config, assetManager, DEFAULT_OPTIONS)(). + then(() => { + assetManager.assertWrites([{ + filepath: path.resolve(FIXTURES_DIR, "./dist/bundle.js"), + content: makeBundle(` +'use strict'; + +var dummy = { + some: "dummy value" +}; + +console.log(dummy); // eslint-disable-line no-console +`) + }]); + }); + }); + }); });