Skip to content
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

Revert "Add support for tsconfig paths. (fixes #150)" #171

Merged
merged 8 commits into from
Dec 17, 2018
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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ dist/**/*.js
!test
!test/integration
!test/integration/*.js
!test/integration/*.ts
!test/unit
!test/unit/**
!dist/
!dist/ncc/
!dist/buildin/
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@
"terser": "^3.11.0",
"the-answer": "^1.0.0",
"ts-loader": "^5.3.1",
"tsconfig-paths": "^3.7.0",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"twilio": "^3.23.2",
"typescript": "^3.2.2",
"vue": "^2.5.17",
Expand Down
46 changes: 4 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const MemoryFS = require("memory-fs");
const WebpackParser = require("webpack/lib/Parser");
const webpackParse = WebpackParser.parse;
const terser = require("terser");
const tsconfigPaths = require("tsconfig-paths");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const shebangRegEx = require('./utils/shebang');

// overload the webpack parser so that we can make
Expand All @@ -23,7 +21,7 @@ WebpackParser.parse = function(source, opts = {}) {

const SUPPORTED_EXTENSIONS = [".ts", ".tsx", ".js", ".mjs", ".json", ".node"];

function resolveModule(matchPath, context, request, callback, forcedExternals = []) {
function resolveModule(context, request, callback, forcedExternals = []) {
const resolveOptions = {
basedir: context,
preserveSymlinks: true,
Expand All @@ -37,12 +35,6 @@ function resolveModule(matchPath, context, request, callback, forcedExternals =

resolve(request, resolveOptions, err => {
if (err) {
// check tsconfig paths before erroring
if (matchPath && matchPath(request, undefined, undefined, SUPPORTED_EXTENSIONS)) {
callback();
return;
}

console.error(
`ncc: Module directory "${context}" attempted to require "${request}" but could not be resolved, assuming external.`
);
Expand All @@ -66,20 +58,6 @@ module.exports = async (
const mfs = new MemoryFS();
const assetNames = Object.create(null);
const assets = Object.create(null);
const resolvePlugins = [];
let tsconfigMatchPath;
// add TsconfigPathsPlugin to support `paths` resolution in tsconfig
// we need to catch here because the plugin will
// error if there's no tsconfig in the working directory
try {
resolvePlugins.push(new TsconfigPathsPlugin({ silent: true }));

const tsconfig = tsconfigPaths.loadConfig();
if (tsconfig.resultType === "success") {
tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths);
}
} catch (e) {}

const compiler = webpack({
entry,
optimization: {
Expand All @@ -98,12 +76,11 @@ module.exports = async (
extensions: SUPPORTED_EXTENSIONS,
// webpack defaults to `module` and `main`, but that's
// not really what node.js supports, so we reset it
mainFields: ["main"],
plugins: resolvePlugins
mainFields: ["main"]
},
// https://github.com/zeit/ncc/pull/29#pullrequestreview-177152175
node: false,
externals: (...args) => resolveModule(tsconfigMatchPath, ...[...args, externals]),
externals: (...args) => resolveModule(...[...args, externals]),
module: {
rules: [
{
Expand Down Expand Up @@ -163,7 +140,7 @@ module.exports = async (
"var e = new Error",
`if (typeof req === 'number' && __webpack_require__.m[req])\n` +
` return __webpack_require__(req);\n` +
`try { return require(req) }\n` +
`try { return require(req) }\n` +
`catch (e) { if (e.code !== 'MODULE_NOT_FOUND') throw e }\n` +
`var e = new Error`
);
Expand Down Expand Up @@ -199,21 +176,6 @@ module.exports = async (
});
compiler.inputFileSystem = fs;
compiler.outputFileSystem = mfs;
// tsconfig-paths-webpack-plugin requires a readJson method on the filesystem
compiler.inputFileSystem.readJson = (path, callback) => {
compiler.inputFileSystem.readFile(path, (err, data) => {
if (err) {
callback(err);
return;
}

try {
callback(null, JSON.parse(data));
} catch (e) {
callback(e);
}
});
};
compiler.resolvers.normal.fileSystem = mfs;
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
Expand Down
12 changes: 3 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@ const { dirname } = require("path");

for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
const testDir = `${__dirname}/unit/${unitTest}`;
const expected = fs
.readFileSync(`${testDir}/output.js`)
.readFileSync(`${__dirname}/unit/${unitTest}/output.js`)
.toString()
.trim()
// Windows support
.replace(/\r/g, "");

// set env variable so tsconfig-paths can find the config
process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`;
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`, { minify: false }).then(
await ncc(`${__dirname}/unit/${unitTest}/input.js`, { minify: false }).then(
async ({ code, assets }) => {
// very simple asset validation in unit tests
if (unitTest.startsWith("asset-")) {
Expand All @@ -33,7 +27,7 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
expect(actual).toBe(expected);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${testDir}/actual.js`, actual);
fs.writeFileSync(`${__dirname}/unit/${unitTest}/actual.js`, actual);
throw e;
}
}
Expand Down
6 changes: 0 additions & 6 deletions test/unit/tsconfig-paths-conflicting-external/input.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/tsconfig-paths-conflicting-external/module.ts

This file was deleted.

110 changes: 0 additions & 110 deletions test/unit/tsconfig-paths-conflicting-external/output.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/unit/tsconfig-paths-conflicting-external/tsconfig.json

This file was deleted.

3 changes: 0 additions & 3 deletions test/unit/tsconfig-paths/input.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/tsconfig-paths/module.ts

This file was deleted.

110 changes: 0 additions & 110 deletions test/unit/tsconfig-paths/output.js

This file was deleted.

Loading