Skip to content

Commit 62a7dd6

Browse files
committed
Add file filter loader
1 parent 86fa1a5 commit 62a7dd6

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

lib/generators/lightning_cad/templates/app/javascript/helpers/hooks.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path'
22
import * as glob from 'glob'
3+
import filterFile from '../../../config/webpack/loaders/filter-file.js'
34

45
const shorthandRoot = path.join(process.cwd(), 'app/javascript')
56
const config = {
@@ -19,8 +20,18 @@ export function resolve(specifier, context, nextResolve) {
1920
return nextResolve(finalSpecifier, context)
2021
}
2122

22-
export function load(specifier, context, nextLoad) {
23-
if (!specifier.includes('*')) return nextLoad(specifier, context)
23+
export async function load(specifier, context, nextLoad) {
24+
if (!specifier.includes('*')) {
25+
const result = await nextLoad(specifier, context)
26+
if (!result.source) return result
27+
28+
const source = filterFile(result.source, 'server')
29+
30+
return {
31+
...result,
32+
source
33+
}
34+
}
2435

2536
const pattern = specifier.replace('file://', '')
2637
const paths = glob.globSync(pattern, { nodir: true })
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Webpack loader to filter server-only or client-only sections of code out of
3+
* a file, based on the current target (e.g., 'web' or 'node')
4+
*/
5+
export default function (source, target = this.target) {
6+
let begin, end
7+
if (target === 'web') {
8+
begin = '// @begin-server-only'
9+
end = '// @end-server-only'
10+
} else {
11+
begin = '// @begin-client-only'
12+
end = '// @end-client-only'
13+
}
14+
15+
let filteredSource = source
16+
let beginIndex = filteredSource.indexOf(begin)
17+
let endIndex = filteredSource.indexOf(end)
18+
while (beginIndex !== -1) {
19+
const afterExcludedBlock = endIndex === -1 ? '' : filteredSource.slice(endIndex + end.length)
20+
filteredSource = filteredSource.slice(0, beginIndex) + afterExcludedBlock
21+
beginIndex = filteredSource.indexOf(begin)
22+
endIndex = filteredSource.indexOf(end)
23+
}
24+
25+
return filteredSource
26+
}

lib/generators/lightning_cad/webpack_generator.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ def add_resolve_aliases
5555
def add_loaders
5656
say 'Adding loaders to the config'
5757

58+
copy_file 'config/webpack/loaders/filter-file.js'
59+
5860
loaders = <<-JS
61+
,
62+
{
63+
test: /\.jsx?$/,
64+
use: path.resolve('./config/webpack/loaders/filter-file.js')
65+
},
5966
{
6067
test: /\.(mjs|cjs|js|jsx)$/,
6168
loader: 'import-glob'
@@ -66,7 +73,7 @@ def add_loaders
6673
},
6774
JS
6875

69-
insert_into_file 'webpack.config.js', loaders, after: "rules: [\n"
76+
insert_into_file 'webpack.config.js', loaders, after: "'postcss-loader'\n ]\n }"
7077
end
7178

7279
def add_terser_plugin_options

0 commit comments

Comments
 (0)