Skip to content

Commit

Permalink
fix: '../' syntax in component files, close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Nov 4, 2019
1 parent 1b237c2 commit ce423a5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/loaders/common-js.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { readFileSync } from 'fs'
import { extname, dirname, resolve } from 'path'
import { isRelativePath } from '../parsers/dependency-resolver'
import debugFactory from 'debug'

const debug = debugFactory('san-ssr:common-js')

export class Module {
public filepath: string
Expand Down Expand Up @@ -30,7 +33,9 @@ export class CommonJS {
}

require (filepath: string) {
debug('global require called with', filepath)
if (!this.cache.has(filepath)) {
debug('cache miss, reading', filepath)
const fileContent = this.readFile(filepath) ||
this.readFile(filepath + '.ts') ||
this.readFile(filepath + '.js')
Expand All @@ -40,6 +45,7 @@ export class CommonJS {
// eslint-disable-next-line
const fn = new Function('module', 'exports', 'require', mod.content)
fn(mod, mod.exports, path => {
debug('local require called with', path)
if (isRelativePath(path)) {
path = resolve(dirname(filepath), path)
return this.require(path)
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/dependency-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export function shouldInline (decl: ImportDeclaration) {
}

export function isRelativePath (importLiteralValue: string) {
return /^\.\//.test(importLiteralValue)
return /^\.+\//.test(importLiteralValue)
}
4 changes: 2 additions & 2 deletions src/utils/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import debugFactory from 'debug'

process.env.SAN_SSR_PACKAGE_NAME = '../../..'

const jsSSRUnables = ['multi-files']
const jsSSRUnables = ['multi-files', 'import-files-from-parent-directory']
const debug = debugFactory('case')
const caseRoot = resolve(__dirname, '../../test/cases')
const tsConfigFilePath = resolve(__dirname, '../../test/tsconfig.json')
const cases = readdirSync(caseRoot)
const sanProject = new SanProject({ tsConfigFilePath })
const multiFileCases = ['multi-component-files', 'multi-files']
const multiFileCases = ['multi-component-files', 'multi-files', 'import-files-from-parent-directory']

export function supportJSSSR (caseName) {
return !jsSSRUnables.includes(caseName)
Expand Down
10 changes: 10 additions & 0 deletions test/cases/import-files-from-parent-directory/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from 'san'
import { sum } from '../../stub/sum'

export default class MyComponent extends Component {
static template = '<u>result {{ sum(a, b) }}</u>'

sum (a, b) {
return sum(a, b)
}
}
4 changes: 4 additions & 0 deletions test/cases/import-files-from-parent-directory/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a": 2,
"b": 3
}
1 change: 1 addition & 0 deletions test/cases/import-files-from-parent-directory/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<u><!--s-data:{"a":2,"b":3}-->result 5</u>
3 changes: 3 additions & 0 deletions test/stub/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum(a: number, b: number) {
return a + b
}

0 comments on commit ce423a5

Please sign in to comment.