aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/index.js
blob: 249f4723a249e540c13656fbb8245413081bee03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict'

const debug = require('debug')('purs-loader')
const loaderUtils = require('loader-utils')
const Promise = require('bluebird')
const fs = Promise.promisifyAll(require('fs'))
const path = require('path')
const jsStringEscape = require('js-string-escape')
const PsModuleMap = require('./PsModuleMap');
const Psc = require('./Psc');
const PscIde = require('./PscIde');
const dargs = require('./dargs');
const spawn = require('cross-spawn').sync
const eol = require('os').EOL

const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g

module.exports = function purescriptLoader(source, map) {
  const callback = this.async()
  const config = this.options
  const query = loaderUtils.parseQuery(this.query)
  const webpackOptions = this.options.purescriptLoader || {}

  const depsPaths = (pscPackage => {
    if (pscPackage) {
      debug('calling psc-package...')

      return spawn('psc-package', ['sources']).stdout.toString().split(eol).filter(v => v != '')
    }
    else {
      return [ path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ]
    }
  })

  let options = Object.assign(webpackOptions, query)

  const defaultDeps = depsPaths(options.pscPackage)
  const defaultOptions = {
    context: config.context,
    psc: 'psc',
    pscArgs: {},
    pscBundle: 'psc-bundle',
    pscBundleArgs: {},
    pscIde: false,
    pscIdeColors: options.psc === 'psa',
    pscIdeArgs: {},
    pscPackage: false,
    bundleOutput: 'output/bundle.js',
    bundleNamespace: 'PS',
    bundle: false,
    warnings: true,
    output: 'output',
    src: [
      path.join('src', '**', '*.purs'),
      ...defaultDeps
    ]
  }

  this.cacheable && this.cacheable()

  let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || {
    rebuild: false,
    deferred: [],
    bundleModules: [],
    warnings: [],
    errors: []
  }

  if (options.pscPackage && options.src) {
    options.src = options.src.concat(defaultDeps) // append psc-package-provided source paths with users'
  }

  options = Object.assign(defaultOptions, options)

  if (!config.purescriptLoaderInstalled) {
    config.purescriptLoaderInstalled = true

    // invalidate loader cache when bundle is marked as invalid (in watch mode)
    this._compiler.plugin('invalid', () => {
      debug('invalidating loader cache');

      cache = config.purescriptLoaderCache = {
        rebuild: options.pscIde,
        deferred: [],
        bundleModules: [],
        ideServer: cache.ideServer,
        psModuleMap: cache.psModuleMap,
        warnings: [],
        errors: []
      }
    });

    // add psc warnings to webpack compilation warnings
    this._compiler.plugin('after-compile', (compilation, callback) => {
      cache.warnings.forEach(warning => {
        compilation.warnings.push(warning);
      });

      cache.errors.forEach(error => {
        compilation.errors.push(error);
      });

      callback()
    });
  }

  const psModuleName = PsModuleMap.match(source)
  const psModule = {
    name: psModuleName,
    load: js => callback(null, js),
    reject: error => callback(error),
    srcPath: this.resourcePath,
    srcDir: path.dirname(this.resourcePath),
    jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
    options: options,
    cache: cache,
    emitWarning: warning => {
      if (options.warnings && warning.length) {
        cache.warnings.push(warning);
      }
    },
    emitError: error => {
      if (error.length) {
        cache.errors.push(error);
      }
    }
  }

  debug('loader called', psModule.name)

  if (options.bundle) {
    cache.bundleModules.push(psModule.name)
  }

  if (cache.rebuild) {
    return PscIde.connect(psModule)
      .then(PscIde.rebuild)
      .then(toJavaScript)
      .then(psModule.load)
      .catch(psModule.reject)
  }

  if (cache.compilationFinished) {
    return toJavaScript(psModule).then(psModule.load).catch(psModule.reject)
  }

  // We need to wait for compilation to finish before the loaders run so that
  // references to compiled output are valid.
  cache.deferred.push(psModule)

  if (!cache.compilationStarted) {
    return Psc.compile(psModule)
       .then(() => PsModuleMap.makeMap(options.src).then(map => {
         debug('rebuilt module map');
         cache.psModuleMap = map;
       }))
      .then(() => Promise.map(cache.deferred, psModule => {
        if (typeof cache.ideServer === 'object') cache.ideServer.kill()
        return toJavaScript(psModule).then(psModule.load)
      }))
      .catch(error => {
        cache.deferred[0].reject(error)
        cache.deferred.slice(1).forEach(psModule => psModule.reject(new Error('purs-loader failed')))
      })
  }
}

function updatePsModuleMap(psModule) {
  const options = psModule.options
  const cache = psModule.cache
  const filePurs = psModule.srcPath
  if (!cache.psModuleMap) {
    debug('module mapping does not exist');
    return PsModuleMap.makeMap(options.src).then(map => {
      cache.psModuleMap = map;
      return cache.psModuleMap;
    });
  }
  else {
    return PsModuleMap.makeMapEntry(filePurs).then(result => {
      const map = Object.assign(cache.psModuleMap, result)
      cache.psModuleMap = map;
      return cache.psModuleMap;
    });
  }
}

// The actual loader is executed *after* purescript compilation.
function toJavaScript(psModule) {
  const options = psModule.options
  const cache = psModule.cache
  const bundlePath = path.resolve(options.bundleOutput)
  const jsPath = cache.bundle ? bundlePath : psModule.jsPath

  debug('loading JavaScript for', psModule.name)

  return Promise.props({
    js: fs.readFileAsync(jsPath, 'utf8'),
    psModuleMap: updatePsModuleMap(psModule)
  }).then(result => {
    let js = ''

    if (options.bundle) {
      // if bundling, return a reference to the bundle
      js = 'module.exports = require("'
             + jsStringEscape(path.relative(psModule.srcDir, options.bundleOutput))
             + '")["' + psModule.name + '"]'
    } else {
      // replace require paths to output files generated by psc with paths
      // to purescript sources, which are then also run through this loader.
      js = result.js
        .replace(requireRegex, (m, p1) => {
          return 'require("' + jsStringEscape(result.psModuleMap[p1].src) + '")'
        })
        .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => {
          return 'require("' + jsStringEscape(result.psModuleMap[psModule.name].ffi) + '")'
        })
    }

    return js
  })
}