Skip to content

Commit

Permalink
bug fixes and better test coverage for import/export rewriting
Browse files Browse the repository at this point in the history
added react example
  • Loading branch information
okwolf committed Apr 20, 2019
1 parent 80504d5 commit 5eeb815
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 30 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "React example",
"program": "${workspaceFolder}/src/index.js",
"cwd": "${workspaceFolder}/examples/react",
"args": [],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
Expand Down
1 change: 1 addition & 0 deletions examples/react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="module" src="index.js"></script>
12 changes: 12 additions & 0 deletions examples/react/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { React, ReactDOM } from "es-react";
import htm from "htm";
const html = htm.bind(React.createElement);

ReactDOM.render(
html`
<main>
<h1>Hello from es-react</h1>
</main>
`,
document.body
);
13 changes: 13 additions & 0 deletions examples/react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "srvs-example",
"version": "0.0.1",
"private": true,
"scripts": {
"prestart": "npm i",
"start": "node ../../src"
},
"dependencies": {
"es-react": "=16.8.30",
"htm": "=2.1.1"
}
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "srvs",
"version": "0.5.0",
"description": "Zero dependency dev derver",
"version": "0.5.1",
"description": "Zero dependency dev server",
"files": [
"src"
],
Expand All @@ -10,14 +10,15 @@
"srvs": "./src/index.js"
},
"devDependencies": {
"eslint": "=5.15.3"
"eslint": "=5.16.0",
"tead": "=0.5.1"
},
"scripts": {
"clean": "npx rimraf coverage node_modules",
"format": "npx prettier --write '{src,examples,!(node_modules|coverage)/}/**/*.js'",
"format:check": "npx prettier --list-different '{src,examples,!(node_modules|coverage)/}/**/*.js'",
"lint": "eslint src/**/*.js",
"test": "npx tead --coverage",
"test": "tead --coverage",
"check": "npm run format:check && npm run lint && npm t",
"prepare": "npm run check",
"release": "./pre-flight-tests && npm run clean && npm i && git tag $npm_package_version && git push && git push --tags && npm publish",
Expand Down
10 changes: 8 additions & 2 deletions src/server/getImportInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = ({ importPath, searchPath }) => {
const moduleVersion = (projectPackage.dependencies || {})[moduleName];
const installedModulePath = path.resolve(nodeModulesPath, moduleName);
const isInstalled = fs.existsSync(installedModulePath);
let relativeModulePath = "";
let resolvedImportPath;

if (moduleName === importPath) {
Expand All @@ -22,8 +23,12 @@ module.exports = ({ importPath, searchPath }) => {
"package.json"
);
delete require.cache[modulePackagePath];
const { module } = require(modulePackagePath);
resolvedImportPath = path.resolve(installedModulePath, module);
const { module, main } = require(modulePackagePath);
relativeModulePath = module || main || "";
resolvedImportPath = path.resolve(
installedModulePath,
relativeModulePath
);
} catch (e) {
// not installed
}
Expand All @@ -37,6 +42,7 @@ module.exports = ({ importPath, searchPath }) => {
moduleName,
moduleVersion,
installedModulePath,
relativeModulePath,
resolvedImportPath,
isInstalled
};
Expand Down
1 change: 1 addition & 0 deletions src/server/mimeLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
".htm": "text/html",
".html": "text/html",
".js": "application/javascript",
".mjs": "application/javascript",
".css": "text/css",
".gif": "image/gif",
".jpeg": "image/jpeg",
Expand Down
14 changes: 11 additions & 3 deletions src/server/rewriteImportsAndExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ module.exports = ({ contents = "", searchPath = "", importContext = "" }) =>
if (module.startsWith(".")) {
return match;
}
const { isInstalled, moduleName, moduleVersion } = getImportInfo({
const {
isInstalled,
moduleName,
moduleVersion,
relativeModulePath
} = getImportInfo({
importPath: module,
searchPath
});
if (isInstalled) {
return `${imports} "/node_modules/${module}"`;
return `${imports} "/node_modules/${path.join(
module,
relativeModulePath
)}"`;
}
return `${imports} "https://unpkg.com/${moduleName}${
moduleVersion ? `@${moduleVersion}` : ""
Expand All @@ -28,6 +36,6 @@ module.exports = ({ contents = "", searchPath = "", importContext = "" }) =>
!path.basename(module).includes(".")
? normalizePath(path.join(importContext, module))
: module;
const importPrefix = resolvedRelativeImport.startsWith(".") ? "" : ".";
const importPrefix = resolvedRelativeImport.startsWith(".") ? "" : "./";
return `${exports} "${importPrefix}${resolvedRelativeImport}"`;
});
8 changes: 1 addition & 7 deletions src/server/streamPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,13 @@ const streamFile = ({
};

const streamModule = ({ importPath, searchPath, resolve, reject }) => {
const { resolvedImportPath, installedModulePath } = getImportInfo({
const { resolvedImportPath } = getImportInfo({
importPath,
searchPath
});
const installedModuleParentPath = path.dirname(installedModulePath);
const resolvedRelativeImport = resolvedImportPath.substring(
installedModuleParentPath.length
);
const importContext = path.dirname(resolvedRelativeImport);
streamFile({
filePath: resolvedImportPath,
searchPath,
importContext,
resolve,
reject
});
Expand Down

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

Empty file.

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

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

7 changes: 5 additions & 2 deletions test/server/fake-project/installed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"fake-package": "1.0.0"
"@fake/package": "1.0.0",
"fake-package": "1.0.0",
"main-package": "1.0.0",
"module-package": "1.0.0"
}
}
}
72 changes: 60 additions & 12 deletions test/server/rewriteImportsAndExports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ const rewriteImportsAndExports = require("../../src/server/rewriteImportsAndExpo
export default {
rewriteImportsAndExports: {
"should be a function": [typeof rewriteImportsAndExports, "function"],
"should handle missing contents": [rewriteImportsAndExports({}), ""],
imports: {
"should not modify relative imports": [
rewriteImportsAndExports({
contents: `import something from "./somewhere"`
}),
`import something from "./somewhere"`
],
relative: {
"should not modify relative imports": [
rewriteImportsAndExports({
contents: `import something from "./somewhere"`
}),
`import something from "./somewhere"`
],
"should not modify relative subfolder imports": [
rewriteImportsAndExports({
contents: `import stuff from "./sub/folder"`
}),
`import stuff from "./sub/folder"`
]
},
absolute: {
"not installed": {
"without version should rewrite to default unpkg": [
Expand All @@ -36,15 +45,54 @@ export default {
)
}),
`import { app, h } from "https://unpkg.com/[email protected]?module"`
],
"should rewrite org packages to 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"`
]
},
installed: {
"with version should rewrite to local node_modules": [
"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"`
],
"should rewrite org packages 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"`
],
"subfolder should rewrite to local node_modules": [
rewriteImportsAndExports({
contents: `import { stuff } from "fake-package/subfolder"`,
searchPath: path.join(fakeProjectPath, "installed")
}),
`import { stuff } from "/node_modules/fake-package/subfolder"`
],
"module should rewrite to local node_modules": [
rewriteImportsAndExports({
contents: `import something from "module-package"`,
searchPath: path.join(fakeProjectPath, "installed")
}),
`import something from "/node_modules/module-package/some/path/module.js"`
],
"main should rewrite to local node_modules": [
rewriteImportsAndExports({
contents: `import something from "main-package"`,
searchPath: path.join(fakeProjectPath, "installed")
}),
`import something from "/node_modules/main-package/other/path/main.js"`
]
}
}
Expand All @@ -53,35 +101,35 @@ export default {
"should resolve relative subfolder to index without extension": [
rewriteImportsAndExports({
contents: `export { default } from "./subfolder/index"`,
importContext: "/folder"
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"
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"
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"
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"
importContext: "folder"
}),
`export { default } from "./name.js"`
]
Expand Down

0 comments on commit 5eeb815

Please sign in to comment.