nadesiko3 3.3.13 → 3.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/wnako3.mjs CHANGED
@@ -53,17 +53,15 @@ class WebNakoCompiler extends NakoCompiler {
53
53
  // eslint-disable-next-line no-prototype-builtins
54
54
  if (localFiles.hasOwnProperty(filePath)) {
55
55
  return {
56
- sync: true,
57
- value: () => {
56
+ task: (async () => () => {
58
57
  // eslint-disable-next-line no-new-func
59
58
  Function(localFiles[filePath])()
60
59
  return {}
61
- }
60
+ })()
62
61
  }
63
62
  }
64
63
  return {
65
- sync: false,
66
- value: (async () => {
64
+ task: (async () => {
67
65
  const res = await fetch(filePath)
68
66
  if (!res.ok) {
69
67
  throw new NakoImportError(`ファイル『${filePath}』のダウンロードに失敗しました: ${res.status} ${res.statusText}`, token.file, token.line)
@@ -93,11 +91,10 @@ class WebNakoCompiler extends NakoCompiler {
93
91
  readNako3: (filePath, token) => {
94
92
  // eslint-disable-next-line no-prototype-builtins
95
93
  if (localFiles.hasOwnProperty(filePath)) {
96
- return { sync: true, value: localFiles[filePath] }
94
+ return {task: (async () => { return localFiles[filePath] })}
97
95
  }
98
96
  return {
99
- sync: false,
100
- value: (async () => {
97
+ task: (async () => {
101
98
  const res = await fetch(filePath)
102
99
  if (!res.ok) {
103
100
  throw new NakoImportError(`ファイル ${filePath} のダウンロードに失敗しました: ${res.status} ${res.statusText}`, token.file, token.line)
@@ -106,32 +103,42 @@ class WebNakoCompiler extends NakoCompiler {
106
103
  })()
107
104
  }
108
105
  },
109
- resolvePath: (name, token) => {
110
- // ローカルにファイルが存在するならそれを使う。そうでなければURLとして解釈する。
106
+ resolvePath: (name, token, fromFile) => {
111
107
  let pathname = name
112
- // eslint-disable-next-line no-prototype-builtins
113
- if (!localFiles.hasOwnProperty(name)) {
114
- try {
115
- pathname = new URL(name).pathname
116
- } catch (e) {
117
- // 単純にパスに変換できなければ、loccation.hrefを参考にパスを組み立てる
108
+ // http から始まっていれば解決は不要
109
+ if (pathname.startsWith('http://') || pathname.startsWith('https://')) {
110
+ // fullpath
111
+ } else {
112
+ // eslint-disable-next-line no-prototype-builtins
113
+ // ローカルにファイルが存在するならそれを使う。そうでなければURLとして解釈する。
114
+ if (!localFiles.hasOwnProperty(name)) {
118
115
  try {
119
- const href_a = window.location.href.split('/')
120
- const href_dir = href_a.splice(0, href_a.length - 1).join('/');
121
- const href = href_dir + '/' + name
122
- pathname = new URL(href).pathname
116
+ pathname = new URL(name).pathname
123
117
  } catch (e) {
124
- throw new NakoImportError(`取り込み文の引数でパスが解決できません。https:// か http:// で始まるアドレスを指定してください。\n${e}`, token.file, token.line)
118
+ // 単純にパスに変換できなければ、loccation.hrefを参考にパスを組み立てる
119
+ try {
120
+ let baseDir = dirname(fromFile)
121
+ if (baseDir === '') {
122
+ // https://2/3/4.html
123
+ const a = window.location.href.split('/')
124
+ baseDir = '/' + a.slice(3,a.length - 1).join('/')
125
+ }
126
+ pathname = resolveURL(baseDir, name)
127
+ } catch (e) {
128
+ throw new NakoImportError(`取り込み文の引数でパスが解決できません。https:// か http:// で始まるアドレスを指定してください。\n${e}`, token.file, token.line)
129
+ }
125
130
  }
131
+ } else {
132
+ pathname = localFiles[name]
126
133
  }
127
134
  }
128
135
  // .js および .mjs なら JSプラグイン
129
136
  if (pathname.endsWith('.js') || pathname.endsWith('.js.txt') || pathname.endsWith('.mjs') || pathname.endsWith('.mjs.txt')) {
130
- return { filePath: name, type: 'js' }
137
+ return { filePath: pathname, type: 'js' }
131
138
  }
132
139
  // .nako3 なら なでしこ3プラグイン
133
140
  if (pathname.endsWith('.nako3') || pathname.endsWith('.nako3.txt')) {
134
- return { filePath: name, type: 'nako3' }
141
+ return { filePath: pathname, type: 'nako3' }
135
142
  }
136
143
  // ファイル拡張子が未指定の場合
137
144
  throw new NakoImportError(`ファイル『${name}』は拡張子が(.nako3|.js|.js.txt|.mjs|.mjs.txt)以外なので取り込めません。`, token.file, token.line)
@@ -166,6 +173,29 @@ class WebNakoCompiler extends NakoCompiler {
166
173
  }
167
174
  }
168
175
 
176
+ function dirname(s) {
177
+ const a = s.split('/')
178
+ if (a && a.length > 1) {
179
+ return a.slice(0, a.length - 1).join('/')
180
+ }
181
+ return ''
182
+ }
183
+
184
+ function resolveURL(base, s) {
185
+ const baseA = base.split('/')
186
+ const sA = s.split('/')
187
+ for (let p of sA) {
188
+ if (p === '') {continue}
189
+ if (p === '.') {continue}
190
+ if (p === '..') {
191
+ baseA.pop()
192
+ continue
193
+ }
194
+ baseA.push(p)
195
+ }
196
+ return baseA.join('/')
197
+ }
198
+
169
199
  // ブラウザなら navigator.nako3 になでしこを登録
170
200
  if (typeof (navigator) === 'object' && !navigator.exportWNako3) {
171
201
  const nako3 = navigator.nako3 = new WebNakoCompiler()
@@ -9,7 +9,7 @@ const __filename = url.fileURLToPath(import.meta.url);
9
9
  const __dirname = path.dirname(__filename);
10
10
 
11
11
  const nakoHome = path.resolve(path.join(__dirname, '../../'))
12
- const cnako3 = path.resolve(path.join(nakoHome, 'src/cnako3.js'))
12
+ const cnako3 = path.resolve(path.join(nakoHome, 'src/cnako3.mjs'))
13
13
  const nako3edit = path.resolve(path.join(__dirname, 'index.nako3'))
14
14
 
15
15
  let proc = spawn('node', [cnako3, nako3edit])