-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split up import/export rewrites and add some tests for logic
fix a bug too in the process
- Loading branch information
Showing
12 changed files
with
217 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
module.exports = ({ importPath, searchPath }) => { | ||
const nodeModulesPath = path.resolve(searchPath, "node_modules"); | ||
const moduleName = importPath | ||
.split("/") | ||
.slice(0, importPath.startsWith("@") ? 2 : 1) | ||
.join("/"); | ||
const projectPackagePath = path.resolve(searchPath, "package.json"); | ||
delete require.cache[projectPackagePath]; | ||
const projectPackage = require(projectPackagePath); | ||
const moduleVersion = (projectPackage.dependencies || {})[moduleName]; | ||
const installedModulePath = path.resolve(nodeModulesPath, moduleName); | ||
const isInstalled = fs.existsSync(installedModulePath); | ||
let resolvedImportPath; | ||
|
||
if (moduleName === importPath) { | ||
try { | ||
const modulePackagePath = path.resolve( | ||
installedModulePath, | ||
"package.json" | ||
); | ||
delete require.cache[modulePackagePath]; | ||
const { module } = require(modulePackagePath); | ||
resolvedImportPath = path.resolve(installedModulePath, module); | ||
} catch (e) { | ||
// not installed | ||
} | ||
} else { | ||
resolvedImportPath = require.resolve(importPath, { | ||
paths: [nodeModulesPath] | ||
}); | ||
} | ||
|
||
return { | ||
moduleName, | ||
moduleVersion, | ||
installedModulePath, | ||
resolvedImportPath, | ||
isInstalled | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const path = require("path"); | ||
const getImportInfo = require("./getImportInfo"); | ||
const normalizePath = require("../normalizePath"); | ||
|
||
const ES6_IMPORT_REGEX = /(import[\s\S]+?from)\s+?['"]([^"']+)["']?;?/g; | ||
const ES6_EXPORT_REGEX = /(export[\s\S]+?from)\s+?['"]([^"']+)["']?;?/g; | ||
|
||
module.exports = ({ contents = "", searchPath = "", importContext = "" }) => | ||
contents | ||
.replace(ES6_IMPORT_REGEX, (match, imports, module) => { | ||
if (module.startsWith(".")) { | ||
return match; | ||
} | ||
const { isInstalled, moduleName, moduleVersion } = getImportInfo({ | ||
importPath: module, | ||
searchPath | ||
}); | ||
if (isInstalled) { | ||
return `${imports} "/node_modules/${module}"`; | ||
} | ||
return `${imports} "https://unpkg.com/${moduleName}${ | ||
moduleVersion ? `@${moduleVersion}` : "" | ||
}?module"`; | ||
}) | ||
.replace(ES6_EXPORT_REGEX, (_, exports, module) => { | ||
const resolvedRelativeImport = | ||
(importContext && path.dirname(module) !== ".") || | ||
!path.basename(module).includes(".") | ||
? normalizePath(path.join(importContext, module)) | ||
: module; | ||
const importPrefix = resolvedRelativeImport.startsWith(".") ? "" : "."; | ||
return `${exports} "${importPrefix}${resolvedRelativeImport}"`; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!node_modules |
5 changes: 5 additions & 0 deletions
5
test/server/fake-project/installed/node_modules/fake-package/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "fake-project", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"fake-package": "1.0.0" | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/server/fake-project/not-installed/no-version/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "fake-project", | ||
"version": "0.0.0", | ||
"private": true | ||
} |
8 changes: 8 additions & 0 deletions
8
test/server/fake-project/not-installed/with-version/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "fake-project", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"fake-package": "1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const path = require("path"); | ||
|
||
const fakeProjectPath = path.join(__dirname, "fake-project"); | ||
|
||
const rewriteImportsAndExports = require("../../src/server/rewriteImportsAndExports"); | ||
export default { | ||
rewriteImportsAndExports: { | ||
"should be a function": [typeof rewriteImportsAndExports, "function"], | ||
imports: { | ||
"should not modify relative imports": [ | ||
rewriteImportsAndExports({ | ||
contents: `import something from "./somewhere"` | ||
}), | ||
`import something from "./somewhere"` | ||
], | ||
absolute: { | ||
"not installed": { | ||
"without version should rewrite to default unpkg": [ | ||
rewriteImportsAndExports({ | ||
contents: `import { app, h } from "fake-package"`, | ||
searchPath: path.join( | ||
fakeProjectPath, | ||
"not-installed", | ||
"no-version" | ||
) | ||
}), | ||
`import { app, h } from "https://unpkg.com/fake-package?module"` | ||
], | ||
"with version should rewrite to unpkg with version": [ | ||
rewriteImportsAndExports({ | ||
contents: `import { app, h } from "fake-package"`, | ||
searchPath: path.join( | ||
fakeProjectPath, | ||
"not-installed", | ||
"with-version" | ||
) | ||
}), | ||
`import { app, h } from "https://unpkg.com/fake-package@1.0.0?module"` | ||
] | ||
}, | ||
installed: { | ||
"with version should rewrite to local node_modules": [ | ||
rewriteImportsAndExports({ | ||
contents: `import { app, h } from "fake-package"`, | ||
searchPath: path.join(fakeProjectPath, "installed") | ||
}), | ||
`import { app, h } from "/node_modules/fake-package"` | ||
] | ||
} | ||
} | ||
}, | ||
exports: { | ||
"should resolve relative subfolder to index without extension": [ | ||
rewriteImportsAndExports({ | ||
contents: `export { default } from "./subfolder/index"`, | ||
importContext: "/folder" | ||
}), | ||
`export { default } from "./folder/subfolder/index"` | ||
], | ||
"should resolve relative subfolder to index with extension": [ | ||
rewriteImportsAndExports({ | ||
contents: `export { default } from "./subfolder/index.js"`, | ||
importContext: "/folder" | ||
}), | ||
`export { default } from "./folder/subfolder/index.js"` | ||
], | ||
"should resolve relative subfolder to non index file with extension": [ | ||
rewriteImportsAndExports({ | ||
contents: `export { default } from "./somewhere/name.js"`, | ||
importContext: "/test" | ||
}), | ||
`export { default } from "./test/somewhere/name.js"` | ||
], | ||
"should resolve relative root to non index file without extension": [ | ||
rewriteImportsAndExports({ | ||
contents: `export { default } from "./name"`, | ||
importContext: "/folder" | ||
}), | ||
`export { default } from "./folder/name"` | ||
], | ||
"should resolve relative root to non index file with extension": [ | ||
rewriteImportsAndExports({ | ||
contents: `export { default } from "./name.js"`, | ||
importContext: "/folder" | ||
}), | ||
`export { default } from "./name.js"` | ||
] | ||
} | ||
} | ||
}; |