Skip to content

Commit

Permalink
fix: compat windows (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 authored Oct 15, 2022
1 parent 5af471a commit 50e13ac
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ TODOs.md
*.tgz
vendors
.vite-inspect
.eslintcache
.eslintcache
package-lock.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"pnpm-install": "pnpm install",
"dev": "tsup --watch --format=cjs,esm",
"build": "NODE_ENV=production tsup --format=cjs,esm && tsx scripts/pre-bundle.cts",
"build": "cross-env NODE_ENV=production tsup --format=cjs,esm && tsx scripts/pre-bundle.cts",
"dev:docs": "node ./bin/island docs",
"build:docs": "node ./bin/island build docs && cp -r ./docs/.island/index.html ./docs/.island/dist",
"start:docs": "node ./bin/island start docs",
Expand Down Expand Up @@ -129,6 +129,7 @@
"@typescript-eslint/parser": "^5.36.1",
"commitlint": "^17.1.2",
"conventional-changelog-cli": "^2.2.2",
"cross-env": "^7.0.3",
"es-module-lexer": "^1.0.3",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion scripts/pre-bundle.cts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import resolve from 'resolve';
import { performance } from 'perf_hooks';
import { green } from 'picocolors';
import { remove, pathExists } from 'fs-extra';
import { normalizePath } from 'vite';

type PreBundleItem = string;

Expand Down Expand Up @@ -61,7 +62,7 @@ async function preBundle(deps: PreBundleItem[]) {
});

build.onLoad({ filter: /.*/, namespace: 'dep' }, async (args) => {
const entryPath = args.path;
const entryPath = normalizePath(args.path);
const code = await fs.readFile(entryPath, 'utf-8');
const [imports, exports] = await parse(code);
const proxyModule = [];
Expand Down
8 changes: 7 additions & 1 deletion src/node/__tests__/babel-plugin-island.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { expect, describe, test } from 'vitest';
import { transformAsync, TransformOptions } from '@babel/core';
import babelPluginIsland from '../babel-plugin-island';
import { MASK_SPLITTER } from '../constants';
import os from 'os';

const isWindows = os.platform() === 'win32';

describe('test babel-plugin-island', () => {
const ISLAND_PATH = '../Comp/index';
const IMPORTER_PATH = '/User/code/project/test.tsx';
const relativePath = 'project/test.tsx';
const prefix = isWindows ? 'C:/' : '/User/';
const IMPORTER_PATH = `${prefix}${relativePath}`;
const babelOptions: TransformOptions = {
filename: IMPORTER_PATH,
presets: ['@babel/preset-react'],
Expand All @@ -14,6 +19,7 @@ describe('test babel-plugin-island', () => {
test('Should compile jsx identifier', async () => {
const code = `import A from '${ISLAND_PATH}'; export default function App() { return <A __island>hello</A>; }`;
const result = await transformAsync(code, babelOptions);
console.log(result?.code);
expect(result?.code).toContain(
`__island: "${ISLAND_PATH}${MASK_SPLITTER}${IMPORTER_PATH}"`
);
Expand Down
6 changes: 5 additions & 1 deletion src/node/babel-plugin-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { type PluginPass, types as t } from '@babel/core';
import type { Visitor } from '@babel/traverse';
import {} from 'vite';
import { MASK_SPLITTER } from './constants';
import { normalizePath } from 'vite';

const ID = '__island';

Expand Down Expand Up @@ -46,8 +47,11 @@ export default declare((api) => {
for (let i = 0; i < attributes.length; i++) {
const name = (attributes[i] as t.JSXAttribute).name;
if (name?.name === ID) {
console.log(state.filename);
(attributes[i] as t.JSXAttribute).value = t.stringLiteral(
`${source.value}${MASK_SPLITTER}${state.filename}`
`${source.value}${MASK_SPLITTER}${normalizePath(
state.filename || ''
)}`
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { HelmetData } from 'react-helmet-async';
import createDebugger from 'debug';
import { performance } from 'perf_hooks';
import pc from 'picocolors';
import { pathToFileURL } from 'url';

const debug = createDebugger('island:build');
const islandInjectId = 'island:inject';
Expand Down Expand Up @@ -97,7 +98,7 @@ class SSGBuilder {

const serverEntryPath = join(this.#root, SERVER_OUTPUT_PATH);
const { render, routes } = (await dynamicImport(
serverEntryPath
pathToFileURL(serverEntryPath)
)) as ServerEntryExports;
return [render, routes];
} catch (e) {
Expand Down

1 comment on commit 50e13ac

@vercel
Copy link

@vercel vercel bot commented on 50e13ac Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.