]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/index.js
Ensure that all imported files are watched
[github/fretlink/purs-loader.git] / src / index.js
1 'use strict'
2
3 const debug = require('debug')('purs-loader')
4 const loaderUtils = require('loader-utils')
5 const Promise = require('bluebird')
6 const path = require('path')
7 const PsModuleMap = require('./PsModuleMap');
8 const Psc = require('./Psc');
9 const PscIde = require('./PscIde');
10 const toJavaScript = require('./to-javascript');
11 const dargs = require('./dargs');
12 const spawn = require('cross-spawn').sync
13 const eol = require('os').EOL
14
15 module.exports = function purescriptLoader(source, map) {
16 const callback = this.async()
17 const config = this.options
18 const query = loaderUtils.parseQuery(this.query)
19 const webpackOptions = this.options.purescriptLoader || {}
20
21 const depsPaths = (pscPackage => {
22 if (pscPackage) {
23 debug('calling psc-package...')
24
25 return spawn('psc-package', ['sources']).stdout.toString().split(eol).filter(v => v != '')
26 }
27 else {
28 return [ path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ]
29 }
30 })
31
32 let options = Object.assign(webpackOptions, query)
33
34 const defaultDeps = depsPaths(options.pscPackage)
35 const defaultOptions = {
36 context: config.context,
37 psc: 'psc',
38 pscArgs: {},
39 pscBundle: 'psc-bundle',
40 pscBundleArgs: {},
41 pscIde: false,
42 pscIdeColors: options.psc === 'psa',
43 pscIdeArgs: {},
44 pscPackage: false,
45 bundleOutput: 'output/bundle.js',
46 bundleNamespace: 'PS',
47 bundle: false,
48 warnings: true,
49 output: 'output',
50 src: [
51 path.join('src', '**', '*.purs'),
52 ...defaultDeps
53 ]
54 }
55
56 this.cacheable && this.cacheable()
57
58 let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || {
59 rebuild: false,
60 deferred: [],
61 bundleModules: [],
62 warnings: [],
63 errors: []
64 }
65
66 if (options.pscPackage && options.src) {
67 options.src = options.src.concat(defaultDeps) // append psc-package-provided source paths with users'
68 }
69
70 options = Object.assign(defaultOptions, options)
71
72 if (!config.purescriptLoaderInstalled) {
73 config.purescriptLoaderInstalled = true
74
75 // invalidate loader cache when bundle is marked as invalid (in watch mode)
76 this._compiler.plugin('invalid', () => {
77 debug('invalidating loader cache');
78
79 cache = config.purescriptLoaderCache = {
80 rebuild: options.pscIde,
81 deferred: [],
82 bundleModules: [],
83 ideServer: cache.ideServer,
84 psModuleMap: cache.psModuleMap,
85 warnings: [],
86 errors: []
87 }
88 });
89
90 // add psc warnings to webpack compilation warnings
91 this._compiler.plugin('after-compile', (compilation, callback) => {
92 cache.warnings.forEach(warning => {
93 compilation.warnings.push(warning);
94 });
95
96 cache.errors.forEach(error => {
97 compilation.errors.push(error);
98 });
99
100 callback()
101 });
102 }
103
104 const psModuleName = PsModuleMap.match(source)
105 const psModule = {
106 name: psModuleName,
107 load: js => callback(null, js),
108 reject: error => callback(error),
109 srcPath: this.resourcePath,
110 srcDir: path.dirname(this.resourcePath),
111 jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
112 options: options,
113 cache: cache,
114 emitWarning: warning => {
115 if (options.warnings && warning.length) {
116 cache.warnings.push(warning);
117 }
118 },
119 emitError: error => {
120 if (error.length) {
121 cache.errors.push(error);
122 }
123 }
124 }
125
126 debug('loader called', psModule.name)
127
128 if (options.bundle) {
129 cache.bundleModules.push(psModule.name)
130 }
131
132 if (cache.rebuild) {
133 return PscIde.connect(psModule)
134 .then(PscIde.rebuild)
135 .then(toJavaScript)
136 .then(psModule.load)
137 .catch(psModule.reject)
138 }
139
140 if (cache.compilationFinished) {
141 return toJavaScript(psModule).then(psModule.load).catch(psModule.reject)
142 }
143
144 // We need to wait for compilation to finish before the loaders run so that
145 // references to compiled output are valid.
146 cache.deferred.push(psModule)
147
148 if (!cache.compilationStarted) {
149 return Psc.compile(psModule)
150 .then(() => PsModuleMap.makeMap(options.src).then(map => {
151 debug('rebuilt module map');
152 cache.psModuleMap = map;
153 }))
154 .then(() => Promise.map(cache.deferred, psModule => {
155 if (typeof cache.ideServer === 'object') cache.ideServer.kill()
156 return toJavaScript(psModule).then(psModule.load)
157 }))
158 .catch(error => {
159 cache.deferred[0].reject(error)
160 cache.deferred.slice(1).forEach(psModule => psModule.reject(new Error('purs-loader failed')))
161 })
162 }
163 }