Skip to content

Commit 5972c83

Browse files
use oc-vite for importing statics
1 parent 6059496 commit 5972c83

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const removeExtension = (path) => path.replace(/\.(j|t)sx?$/, '');
2+
3+
const higherOrderServerTemplate = ({
4+
serverPath,
5+
componentName,
6+
componentVersion,
7+
bundleHashKey
8+
}) => {
9+
return `
10+
import { data as dataProvider } from '${removeExtension(serverPath)}';
11+
12+
export const data = (context : any, callback : (error: any, data?: any) => void) => {
13+
dataProvider(context, (error: any, model: any) => {
14+
if (error) {
15+
return callback(error);
16+
}
17+
const props = Object.assign({}, model, {
18+
_staticPath: context.staticPath,
19+
_baseUrl: context.baseUrl,
20+
_componentName: "${componentName}",
21+
_componentVersion: "${componentVersion}"
22+
});
23+
const srcPathHasProtocol = context.staticPath.indexOf("http") === 0;
24+
const srcPath = srcPathHasProtocol ? context.staticPath : ("https:" + context.staticPath);
25+
return callback(null, Object.assign({}, {
26+
component: {
27+
key: "${bundleHashKey}",
28+
src: srcPath + "template.js",
29+
props
30+
}
31+
}));
32+
});
33+
}
34+
`;
35+
};
36+
37+
module.exports = higherOrderServerTemplate;

packages/oc-vite-compiler/lib/viteServer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const vite = require('vite');
44
const fs = require('fs-extra');
55
const coreModules = require('builtin-modules');
66
const hashBuilder = require('oc-hash-builder');
7+
const serverWrapper = require('./serverWrapper');
78

89
const nodeModuleMatcher = /^[a-z@][a-z\-/0-9.]+$/i;
910
const moduleWithPathMatcher = /^(?!@).*\//g;
10-
const removeExtension = (path) => path.replace(/\.(j|t)sx?$/, '');
1111

1212
async function compileServer(options) {
1313
const componentPath = options.componentPath;
@@ -23,7 +23,7 @@ async function compileServer(options) {
2323
const componentVersion = options.componentPackage.version;
2424
const production = !!options.production;
2525

26-
const wrapperFn = options.serverWrapper || (({ serverPath }) => `export { data } from "${removeExtension(serverPath)}"`)
26+
const wrapperFn = options.serverWrapper || serverWrapper;
2727
const higherOrderServerContent = wrapperFn({
2828
bundleHashKey: options.compiledViewInfo.bundle.hashKey,
2929
serverPath,

packages/oc-vite-compiler/lib/viteView.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ async function compileView(options) {
3333
}
3434

3535
const staticFiles = options.componentPackage.oc.files.static;
36-
const staticFolder = Array.isArray(staticFiles) ? staticFiles[0] : staticFiles;
36+
let staticFolder = Array.isArray(staticFiles) ? staticFiles[0] : staticFiles;
37+
staticFolder = staticFolder.replace(/^\//, '').replace(/\/$/, '');
3738
const viewFileName = options.componentPackage.oc.files.template.src;
3839
const componentPath = options.componentPath;
3940
const viewPath = processRelativePath(viewFileName);
@@ -81,6 +82,15 @@ async function compileView(options) {
8182
}
8283
}
8384
}
85+
, experimental: {
86+
renderBuiltUrl(filename, { hostType }) {
87+
if (hostType === 'js') {
88+
return { runtime: `__toOcStaticPathUrl(${JSON.stringify(filename)})` };
89+
} else {
90+
return { relative: true };
91+
}
92+
}
93+
}
8494
});
8595
const out = Array.isArray(result) ? result[0] : result;
8696
const bundle = out.output.find((x) => x.facadeModuleId.endsWith(viewWrapperName)).code;
@@ -106,8 +116,16 @@ async function compileView(options) {
106116
bundle: wrappedBundle,
107117
hash: bundleHash
108118
});
109-
const hash = hashBuilder.fromString(templateString);
110-
const view = ocViewWrapper(hash, templateString);
119+
const wrappedTemplateString = `function(model) {
120+
var __toOcStaticPathUrl = function(args) {
121+
return model.component.props._staticPath + '${staticFolder}/' + args;
122+
}
123+
var innerFn = ${templateString};
124+
return innerFn(model);
125+
}
126+
`;
127+
const hash = hashBuilder.fromString(wrappedTemplateString);
128+
const view = ocViewWrapper(hash, wrappedTemplateString);
111129

112130
await fs.unlink(viewWrapperPath);
113131
await fs.mkdir(publishPath, { recursive: true });

packages/oc-vite-compiler/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oc-vite-compiler",
3-
"version": "1.1.2",
3+
"version": "2.0.0-beta.0",
44
"description": "OC-Vite-Compiler",
55
"main": "index.js",
66
"repository": {
@@ -26,7 +26,7 @@
2626
"fs-extra": "^10.1.0",
2727
"oc-hash-builder": "^1.0.5",
2828
"oc-view-wrapper": "^1.0.6",
29-
"vite": "^4.3.1",
29+
"oc-vite": "^1.0.0",
3030
"vite-plugin-environment": "^1.1.3"
3131
},
3232
"files": [
@@ -35,4 +35,4 @@
3535
"LICENSE",
3636
"README.md"
3737
]
38-
}
38+
}

0 commit comments

Comments
 (0)