From 0ff6d8dda19300e13d9356494abace910f83f4d7 Mon Sep 17 00:00:00 2001 From: kirby ellingson Date: Tue, 30 Aug 2022 17:12:19 -0500 Subject: [PATCH 1/6] issue#5852 added windows fix for nodeFileTrace --- packages/internal/src/build/api.ts | 10 +++++++++- packages/internal/src/build/web.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/internal/src/build/api.ts b/packages/internal/src/build/api.ts index bb9a8c0de59b..7a475977f7bf 100644 --- a/packages/internal/src/build/api.ts +++ b/packages/internal/src/build/api.ts @@ -50,8 +50,16 @@ export const prebuildApiFiles = (srcFiles: string[]) => { return undefined } + // Fixes nodeFileTrace errors on windows when building + let nodeFileTraceCompliantCode: string = result.code + if (process.platform === 'win32') { + nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( + /\\\\/g, + '/' + ) + } fs.mkdirSync(path.dirname(dstPath), { recursive: true }) - fs.writeFileSync(dstPath, result.code) + fs.writeFileSync(dstPath, nodeFileTraceCompliantCode) return dstPath }) diff --git a/packages/internal/src/build/web.ts b/packages/internal/src/build/web.ts index d5c9282ad802..02347c898f38 100644 --- a/packages/internal/src/build/web.ts +++ b/packages/internal/src/build/web.ts @@ -35,8 +35,16 @@ export const prebuildWebFiles = (srcFiles: string[], flags?: Flags) => { return undefined } + // Fixes nodeFileTrace errors on windows when building + let nodeFileTraceCompliantCode: string = result.code + if (process.platform === 'win32') { + nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( + /\\\\/g, + '/' + ) + } fs.mkdirSync(path.dirname(dstPath), { recursive: true }) - fs.writeFileSync(dstPath, result.code) + fs.writeFileSync(dstPath, nodeFileTraceCompliantCode) return dstPath }) From 0a7d666a33fb6f95364df61918fbbe9559d7b347 Mon Sep 17 00:00:00 2001 From: Kirby Douglas Ellingson Date: Tue, 30 Aug 2022 17:55:36 -0500 Subject: [PATCH 2/6] Update packages/internal/src/build/api.ts unregexed. Co-authored-by: Tobbe Lundberg --- packages/internal/src/build/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/internal/src/build/api.ts b/packages/internal/src/build/api.ts index 7a475977f7bf..f0d8fc3e8982 100644 --- a/packages/internal/src/build/api.ts +++ b/packages/internal/src/build/api.ts @@ -54,7 +54,7 @@ export const prebuildApiFiles = (srcFiles: string[]) => { let nodeFileTraceCompliantCode: string = result.code if (process.platform === 'win32') { nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( - /\\\\/g, + '\\\\', '/' ) } From 97686f0e1da91075a9d5533d435f989a3541892a Mon Sep 17 00:00:00 2001 From: Kirby Douglas Ellingson Date: Tue, 30 Aug 2022 17:55:52 -0500 Subject: [PATCH 3/6] Update packages/internal/src/build/web.ts unregexed Co-authored-by: Tobbe Lundberg --- packages/internal/src/build/web.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/internal/src/build/web.ts b/packages/internal/src/build/web.ts index 02347c898f38..f1d1e9242c39 100644 --- a/packages/internal/src/build/web.ts +++ b/packages/internal/src/build/web.ts @@ -39,7 +39,7 @@ export const prebuildWebFiles = (srcFiles: string[], flags?: Flags) => { let nodeFileTraceCompliantCode: string = result.code if (process.platform === 'win32') { nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( - /\\\\/g, + '\\\\', '/' ) } From b229777e56e3a0099b456de8fc9546dd0dbd131f Mon Sep 17 00:00:00 2001 From: kirby ellingson Date: Wed, 31 Aug 2022 12:17:45 -0500 Subject: [PATCH 4/6] issue# 5852 now only the import files are changed. --- packages/internal/src/build/api.ts | 10 +--------- .../babelPlugins/babel-plugin-redwood-src-alias.ts | 4 ++++ packages/internal/src/build/web.ts | 10 +--------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/packages/internal/src/build/api.ts b/packages/internal/src/build/api.ts index f0d8fc3e8982..bb9a8c0de59b 100644 --- a/packages/internal/src/build/api.ts +++ b/packages/internal/src/build/api.ts @@ -50,16 +50,8 @@ export const prebuildApiFiles = (srcFiles: string[]) => { return undefined } - // Fixes nodeFileTrace errors on windows when building - let nodeFileTraceCompliantCode: string = result.code - if (process.platform === 'win32') { - nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( - '\\\\', - '/' - ) - } fs.mkdirSync(path.dirname(dstPath), { recursive: true }) - fs.writeFileSync(dstPath, nodeFileTraceCompliantCode) + fs.writeFileSync(dstPath, result.code) return dstPath }) diff --git a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts index 49817ca0ccc6..d2bf721771ce 100644 --- a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts +++ b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts @@ -32,6 +32,10 @@ export default function ( if (newImport.indexOf('.') !== 0) { newImport = './' + newImport } + // Changes windows pathing to be compliant with nodeFileTrace after build. + if (process.platform === 'win32') { + newImport = newImport.replaceAll('\\', '/') + } const newSource = t.stringLiteral(newImport) p.node.source = newSource diff --git a/packages/internal/src/build/web.ts b/packages/internal/src/build/web.ts index f1d1e9242c39..d5c9282ad802 100644 --- a/packages/internal/src/build/web.ts +++ b/packages/internal/src/build/web.ts @@ -35,16 +35,8 @@ export const prebuildWebFiles = (srcFiles: string[], flags?: Flags) => { return undefined } - // Fixes nodeFileTrace errors on windows when building - let nodeFileTraceCompliantCode: string = result.code - if (process.platform === 'win32') { - nodeFileTraceCompliantCode = nodeFileTraceCompliantCode.replaceAll( - '\\\\', - '/' - ) - } fs.mkdirSync(path.dirname(dstPath), { recursive: true }) - fs.writeFileSync(dstPath, nodeFileTraceCompliantCode) + fs.writeFileSync(dstPath, result.code) return dstPath }) From 284cac47953d2428e85321e1ba51521b125bbd71 Mon Sep 17 00:00:00 2001 From: kirby ellingson Date: Wed, 31 Aug 2022 12:40:38 -0500 Subject: [PATCH 5/6] issue #5852 only import files changed. --- .../build/babelPlugins/babel-plugin-redwood-src-alias.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts index d2bf721771ce..8d82f36a72f6 100644 --- a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts +++ b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts @@ -29,13 +29,13 @@ export default function ( // remove `src/` and create an absolute path const absPath = path.join(options.srcAbsPath, value.substr(4)) let newImport = path.relative(path.dirname(filename), absPath) - if (newImport.indexOf('.') !== 0) { - newImport = './' + newImport - } // Changes windows pathing to be compliant with nodeFileTrace after build. if (process.platform === 'win32') { newImport = newImport.replaceAll('\\', '/') } + if (newImport.indexOf('.') !== 0) { + newImport = './' + newImport + } const newSource = t.stringLiteral(newImport) p.node.source = newSource From 6db43cce56112c70629da2d8235cea5041a6ce15 Mon Sep 17 00:00:00 2001 From: Daniel Choudhury Date: Mon, 5 Sep 2022 18:20:47 +0100 Subject: [PATCH 6/6] Adds test for src-alias plugin - unlikely to work on Windows --- .../babel-plugin-redwood-src-alias.test.ts | 87 +++++++++++++++++++ .../babel-plugin-redwood-src-alias.ts | 1 + 2 files changed, 88 insertions(+) create mode 100644 packages/internal/src/build/babelPlugins/__tests__/babel-plugin-redwood-src-alias.test.ts diff --git a/packages/internal/src/build/babelPlugins/__tests__/babel-plugin-redwood-src-alias.test.ts b/packages/internal/src/build/babelPlugins/__tests__/babel-plugin-redwood-src-alias.test.ts new file mode 100644 index 000000000000..ce6aac400c98 --- /dev/null +++ b/packages/internal/src/build/babelPlugins/__tests__/babel-plugin-redwood-src-alias.test.ts @@ -0,0 +1,87 @@ +import path from 'path' + +import pluginTester from 'babel-plugin-tester' + +import plugin from '../babel-plugin-redwood-src-alias' + +const FIXTURE_PATH = path.resolve( + __dirname, + '../../../../../../__fixtures__/empty-project' +) + +describe('babel plugin redwood import dir - graphql function', () => { + pluginTester({ + plugin, + pluginOptions: { + srcAbsPath: path.join(FIXTURE_PATH, 'api/src'), + }, + pluginName: 'babel-plugin-redwood-src-alias', + babelOptions: { + // We need to set the filename so that state.file.opts.filename is set + // See https://github.com/babel-utils/babel-plugin-tester/issues/87 + filename: path.join(FIXTURE_PATH, 'api/src/functions/graphql.ts'), + }, + tests: { + 'transforms auth imports': { + code: "import { getCurrentUser } from 'src/lib/auth'", + output: "import { getCurrentUser } from '../lib/auth'", + }, + 'imports prisma instance correctly': { + code: "import { db } from 'src/lib/db'", + output: "import { db } from '../lib/db'", + }, + 'kitten utils are correctly found': { + code: "import cuddles from 'src/lib/kittens/utils'", + output: "import cuddles from '../lib/kittens/utils'", + }, + }, + }) +}) + +describe('Handles import statements from a service too', () => { + pluginTester({ + plugin, + pluginOptions: { + srcAbsPath: path.join(FIXTURE_PATH, 'api/src'), + }, + pluginName: 'babel-plugin-redwood-src-alias', + babelOptions: { + // As if the import statement is in another service + filename: path.join(FIXTURE_PATH, 'api/src/services/bazinga/bazinga.ts'), + }, + tests: { + 'transforms auth imports from service': { + code: "import { requireAuth } from 'src/lib/auth'", + output: "import { requireAuth } from '../../lib/auth'", + }, + 'imports from another service': { + code: "import posts from 'src/services/posts'", + output: "import posts from '../posts'", + }, + }, + }) +}) + +describe('Handles typical web imports', () => { + pluginTester({ + plugin, + pluginOptions: { + srcAbsPath: path.join(FIXTURE_PATH, 'web/src'), + }, + pluginName: 'babel-plugin-redwood-src-alias', + babelOptions: { + // As if the import statement is in another service + filename: path.join(FIXTURE_PATH, 'web/src/components/Posts/Post.tsx'), + }, + tests: { + 'handles imports from another component': { + code: "import { QUERY } from 'src/components/Posts/PostsCell'", + output: "import { QUERY } from './PostsCell'", + }, + 'handles imports from utils': { + code: "import { cuddles } from 'src/helpers/kittens'", + output: "import { cuddles } from '../../helpers/kittens'", + }, + }, + }) +}) diff --git a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts index 8d82f36a72f6..472aa6b7a9b7 100644 --- a/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts +++ b/packages/internal/src/build/babelPlugins/babel-plugin-redwood-src-alias.ts @@ -29,6 +29,7 @@ export default function ( // remove `src/` and create an absolute path const absPath = path.join(options.srcAbsPath, value.substr(4)) let newImport = path.relative(path.dirname(filename), absPath) + // Changes windows pathing to be compliant with nodeFileTrace after build. if (process.platform === 'win32') { newImport = newImport.replaceAll('\\', '/')