From 88d5329ad7fd23272017941a513e2fb674b12761 Mon Sep 17 00:00:00 2001 From: "Pryke, Benjamin" Date: Wed, 28 Jun 2023 23:20:09 +0100 Subject: [PATCH 1/2] feat: add CORS plugin to modular serve --- packages/modular-scripts/package.json | 2 ++ packages/modular-scripts/src/serve.ts | 2 ++ yarn.lock | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/modular-scripts/package.json b/packages/modular-scripts/package.json index 58674cc44..752ab3f75 100644 --- a/packages/modular-scripts/package.json +++ b/packages/modular-scripts/package.json @@ -57,6 +57,7 @@ "chalk": "4.1.2", "change-case": "4.1.2", "commander": "9.4.0", + "cors": "^2.8.5", "cosmiconfig": "^8.0.0", "cross-spawn": "7.0.3", "css-loader": "6.7.1", @@ -161,6 +162,7 @@ "@modular-scripts/modular-types": "1.2.1", "@schemastore/package": "0.0.6", "@schemastore/tsconfig": "0.0.9", + "@types/cors": "^2.8.13", "@types/js-yaml": "^4.0.5", "@types/loader-utils": "^2.0.3", "@types/validate-npm-package-name": "^4.0.0", diff --git a/packages/modular-scripts/src/serve.ts b/packages/modular-scripts/src/serve.ts index aa9afc80d..aeea31412 100644 --- a/packages/modular-scripts/src/serve.ts +++ b/packages/modular-scripts/src/serve.ts @@ -1,5 +1,6 @@ import express from 'express'; import * as fs from 'fs-extra'; +import cors from 'cors'; import determineTargetPaths from './build-scripts/common-scripts/determineTargetPaths'; import actionPreflightCheck from './utils/actionPreflightCheck'; import getWorkspaceLocation from './utils/getLocation'; @@ -17,6 +18,7 @@ async function serve(target: string, port = 3000): Promise { if (fs.existsSync(paths.appBuild)) { const app = express(); + app.use(cors()); app.use(express.static(paths.appBuild)); app.listen(port, () => { logger.log(`Serving ${target} on ${port}`); diff --git a/yarn.lock b/yarn.lock index deea1c01b..556ac7ea8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2833,6 +2833,13 @@ dependencies: "@types/node" "*" +"@types/cors@^2.8.13": + version "2.8.13" + resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.13.tgz#b8ade22ba455a1b8cb3b5d3f35910fd204f84f94" + integrity sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA== + dependencies: + "@types/node" "*" + "@types/cross-spawn@^6.0.2": version "6.0.2" resolved "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.2.tgz" @@ -4961,6 +4968,14 @@ core-util-is@~1.0.0: resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz" integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + cosmiconfig@^7.0.0, cosmiconfig@^7.0.1: version "7.1.0" resolved "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-7.1.0.tgz" @@ -9180,7 +9195,7 @@ nwsapi@^2.2.2: resolved "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.2.tgz" integrity sha512-90yv+6538zuvUMnN+zCr8LuV6bPFdq50304114vJYJ8RDyK8D5O9Phpbd6SZWgI7PwzmmfN1upeOJlvybDSgCw== -object-assign@^4.1.0, object-assign@^4.1.1: +object-assign@^4, object-assign@^4.1.0, object-assign@^4.1.1: version "4.1.1" resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz" integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== @@ -12330,7 +12345,7 @@ validate-npm-package-name@^5.0.0: dependencies: builtins "^5.0.0" -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz" integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== From c5eea5f5064c6ab362142dc6eee4ef17c167513a Mon Sep 17 00:00:00 2001 From: "Pryke, Benjamin" Date: Wed, 28 Jun 2023 23:29:21 +0100 Subject: [PATCH 2/2] chore: add changeset --- .changeset/polite-adults-roll.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/polite-adults-roll.md diff --git a/.changeset/polite-adults-roll.md b/.changeset/polite-adults-roll.md new file mode 100644 index 000000000..a57a73831 --- /dev/null +++ b/.changeset/polite-adults-roll.md @@ -0,0 +1,6 @@ +--- +'modular-scripts': patch +--- + +Allow assets served locally via `modular serve` to be loaded cross-domain via +the cors plugin